-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInstaller.php
179 lines (151 loc) · 6.09 KB
/
Installer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* This code is licensed under AGPLv3 license or Afterlogic Software License
* if commercial version of the product was purchased.
* For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
*/
namespace Aurora;
use Aurora\System\Module\Settings;
use Composer\Script\Event;
/**
* @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
* @license https://afterlogic.com/products/common-licensing Afterlogic Software License
* @copyright Copyright (c) 2019, Afterlogic Corp.
*/
class Installer
{
/**
* NOTE! This method should be run from composer.
*/
public static function checkConfigs()
{
$RED = "\033[1;31m";
$YELLOW = "\033[1;33m";
$GREEN = "\033[1;32m";
$NC = "\033[0m";
$sBaseDir = dirname(dirname(__File__));
$sModulesDir = $sBaseDir . "/modules/";
$aModuleDirs = glob($sModulesDir . '*', GLOB_ONLYDIR);
$aModulesOk = [];
$aModulesHasError = [];
$aModulesNoConfig = [];
foreach ($aModuleDirs as $sModuleDir) {
$sModuleName = str_replace($sModulesDir, "", $sModuleDir);
$sConfigFile = $sModuleDir . "/config.json";
if (file_exists($sConfigFile)) {
if (json_decode(file_get_contents($sConfigFile))) {
$aModulesOk[] = $sModuleName;
} else {
$aModulesHasError[] = $sModuleName;
}
} else {
$aModulesNoConfig[] = $sModuleName;
}
}
echo $GREEN . count($aModuleDirs) . " modules were processed" . $NC . "\r\n\r\n";
if (count($aModulesHasError) > 0) {
echo $YELLOW . "The following modules have syntax problems in the config file (" . count($aModulesHasError) . "):" . $NC . "\r\n";
echo $RED . implode("\r\n", $aModulesHasError) . $NC;
echo "\r\n\r\n";
}
if (count($aModulesNoConfig) > 0) {
echo $YELLOW . "The following modules have no config files (" . count($aModulesNoConfig) . "):" . $NC . "\r\n";
echo $RED . implode("\r\n", $aModulesNoConfig) . $NC;
echo "\r\n\r\n";
}
if (count($aModulesOk) > 0) {
echo $GREEN . count($aModulesOk) . " modules have no problem with config files" . $NC;
echo "\r\n";
}
}
/**
* NOTE! This method should be run from composer.
*/
public static function updateConfigs()
{
$sBaseDir = dirname(__File__);
include_once $sBaseDir . '/autoload.php';
$sMessage = "Configuration was updated successfully";
if (!\Aurora\System\Api::UpdateSettings()) {
$sMessage = "An error occurred while updating the configuration";
}
echo $sMessage . "\r\n";
}
/**
* NOTE! This method should be run from composer.
*/
public static function preConfigForce($event)
{
self::preConfig($event);
}
/**
* NOTE! This method should be run from composer.
*/
public static function preConfigSafe($event)
{
$sBaseDir = dirname(__File__);
//Checking that configuration files already exist
if (count(glob(dirname($sBaseDir) . "/data/settings/modules/*")) !== 0) {
echo "The config files are already exist \r\n";
return;
} else {
self::preConfig($event);
}
}
/**
* NOTE! This method should be run from composer.
*/
private static function preConfig($event)
{
$sConfigFilename = 'pre-config.json';
$sBaseDir = dirname(__File__);
$sMessage = "Configuration was updated successfully";
$oExtra = $event->getComposer()->getPackage()->getExtra();
if ($oExtra && isset($oExtra['aurora-installer-pre-config'])) {
$sConfigFilename = $oExtra['aurora-installer-pre-config'];
}
$sConfigPath = dirname($sBaseDir) . '/' . $sConfigFilename;
if (file_exists($sConfigPath)) {
$sPreConfig = file_get_contents($sConfigPath);
$oPreConfig = json_decode($sPreConfig, true);
if ($oPreConfig) {
include_once $sBaseDir . '/autoload.php';
\Aurora\System\Api::Init();
if ($oPreConfig['modules']) {
$oModuleManager = \Aurora\System\Api::GetModuleManager();
foreach ($oPreConfig['modules'] as $sModuleName => $oModuleConfig) {
if (!empty($sModuleName)) {
$oSettings = $oModuleManager->getModuleSettings($sModuleName);
if ($oSettings instanceof Settings) {
$oSettings->Load();
foreach ($oModuleConfig as $sConfigName => $mConfigValue) {
//overriding modules default configuration with pre-configuration data
$oProp = $oSettings->GetSettingsProperty($sConfigName);
if ($oProp) {
if (!empty($oProp->SpecType)) {
$mConfigValue = \Aurora\System\Enums\EnumConvert::FromXml($mConfigValue, $oProp->SpecType);
}
$oSettings->SetValue($sConfigName, $mConfigValue);
}
}
$oSettings->Save();
}
}
}
}
if ($oPreConfig['system']) {
$oSettings = &\Aurora\System\Api::GetSettings();
foreach ($oPreConfig['system'] as $mKey => $mSett) {
$oSettings->{$mKey} = $mSett;
}
$oSettings->Save();
}
} else {
$sMessage = "Invalid config file";
}
} else {
$sMessage = "Config file didn't found";
}
echo $sMessage . "\r\n";
}
}