1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\installer; |
3
|
|
|
|
4
|
|
|
use Composer\IO\IOInterface; |
5
|
|
|
use Composer\IO\NullIO; |
6
|
|
|
use keeko\core\model\Application; |
7
|
|
|
use keeko\core\model\ApplicationQuery; |
8
|
|
|
use keeko\core\model\ApplicationUri; |
9
|
|
|
use keeko\core\model\CountryQuery; |
10
|
|
|
use keeko\core\model\Group; |
11
|
|
|
use keeko\core\model\LanguageQuery; |
12
|
|
|
use keeko\core\model\Localization; |
13
|
|
|
use keeko\core\model\LocalizationQuery; |
14
|
|
|
use keeko\core\model\Map\UserTableMap; |
15
|
|
|
use keeko\core\model\Preference; |
16
|
|
|
use keeko\core\model\User; |
17
|
|
|
use keeko\core\model\UserQuery; |
18
|
|
|
use keeko\core\package\AbstractApplication; |
19
|
|
|
use keeko\core\package\ModuleManager; |
20
|
|
|
use keeko\core\package\PackageManager; |
21
|
|
|
use keeko\core\preferences\SystemPreferences; |
22
|
|
|
use keeko\core\service\ServiceContainer; |
23
|
|
|
use Propel\Runtime\Propel; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
|
26
|
|
|
class InstallerApplication extends AbstractApplication { |
27
|
|
|
|
28
|
|
|
const DEFAULT_LOCALE = 'en-GB'; |
29
|
|
|
|
30
|
|
|
/** @var IOInterface */ |
31
|
|
|
private $io; |
32
|
|
|
|
33
|
|
|
/** @var PackageManager */ |
34
|
|
|
private $packageManager; |
35
|
|
|
|
36
|
|
|
/** @var AppInstaller */ |
37
|
|
|
private $appInstaller; |
38
|
|
|
|
39
|
|
|
/** @var ModuleInstaller */ |
40
|
|
|
private $moduleInstaller; |
41
|
|
|
|
42
|
|
|
/** @var ModuleManager */ |
43
|
|
|
private $moduleManager; |
44
|
|
|
|
45
|
|
|
public function __construct(Application $model, ServiceContainer $service, IOInterface $io = null) { |
46
|
|
|
parent::__construct($model, $service); |
47
|
|
|
$this->io = $io ?: new NullIO(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function initialize() { |
51
|
|
|
$this->packageManager = $this->service->getPackageManager(); |
|
|
|
|
52
|
|
|
$this->appInstaller = new AppInstaller($this->service); |
|
|
|
|
53
|
|
|
$this->moduleInstaller = new ModuleInstaller($this->service); |
|
|
|
|
54
|
|
|
$this->moduleManager = $this->service->getModuleManager(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function install($rootUrl, $locale = self::DEFAULT_LOCALE) { |
58
|
|
|
if (!KEEKO_DATABASE_LOADED) { |
59
|
|
|
throw new \Exception('Cannot install keeko - no database defined'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->io->write('Install Log:'); |
63
|
|
|
|
64
|
|
|
$this->installDatabase(); |
65
|
|
|
$this->initialize(); |
66
|
|
|
$this->installGroupsAndUsers(); |
67
|
|
|
$this->installKeeko($rootUrl, $locale); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Runs the main setup routine |
72
|
|
|
*/ |
73
|
|
|
public function run(Request $request) { |
74
|
|
|
$uri = $request->getUri(); |
|
|
|
|
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Writes the database config |
80
|
|
|
* |
81
|
|
|
* @param string $host |
82
|
|
|
* @param string $database |
83
|
|
|
* @param string $user |
84
|
|
|
* @param string $password |
85
|
|
|
*/ |
86
|
|
|
public function writeConfig($host, $database, $user, $password) { |
|
|
|
|
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* |
92
|
|
|
* @param string $locale |
93
|
|
|
* @return Localization |
94
|
|
|
*/ |
95
|
|
|
private function getLocalization($locale) { |
96
|
|
|
list($langCode, $countryCode) = sscanf($locale, '%2s-%s'); |
97
|
|
|
|
98
|
|
|
$lang = LanguageQuery::create()->findOneByAlpha2($langCode); |
|
|
|
|
99
|
|
|
$country = CountryQuery::create()->findOneByAlpha2($countryCode); |
100
|
|
|
|
101
|
|
|
$local = LocalizationQuery::create() |
102
|
|
|
->filterByLanguage($lang) |
103
|
|
|
->filterByCountry($country) |
104
|
|
|
->findOne(); |
105
|
|
|
|
106
|
|
|
// if no locale found -> create one |
107
|
|
|
if ($local === null) { |
108
|
|
|
$local = new Localization(); |
109
|
|
|
$local->setLanguage($lang); // de: 1546 |
110
|
|
|
$local->setCountry($country); // ger: 276 |
111
|
|
|
$local->setIsDefault(true); |
112
|
|
|
$local->save(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $local; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function installGroupsAndUsers() { |
119
|
|
|
$guestGroup = new Group(); |
120
|
|
|
$guestGroup->setName('Guest'); |
121
|
|
|
$guestGroup->setIsGuest(true); |
122
|
|
|
$guestGroup->save(); |
123
|
|
|
|
124
|
|
|
$userGroup = new Group(); |
125
|
|
|
$userGroup->setName('Users'); |
126
|
|
|
$userGroup->setIsDefault(true); |
127
|
|
|
$userGroup->save(); |
128
|
|
|
|
129
|
|
|
$adminGroup = new Group(); |
130
|
|
|
$adminGroup->setName('Administrators'); |
131
|
|
|
$adminGroup->save(); |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
$con = Propel::getConnection(); |
|
|
|
|
135
|
|
|
$adapter = Propel::getAdapter(); |
136
|
|
|
|
137
|
|
|
// guest |
138
|
|
|
$guest = new User(); |
139
|
|
|
$guest->setDisplayName('Guest'); |
140
|
|
|
$guest->save(); |
141
|
|
|
|
142
|
|
|
$stmt = $con->prepare(sprintf('UPDATE %s SET id = -1 WHERE ID = 1', $adapter->quoteIdentifierTable(UserTableMap::TABLE_NAME))); |
143
|
|
|
$stmt->execute(); |
144
|
|
|
|
145
|
|
|
// root |
146
|
|
|
$root = new User(); |
147
|
|
|
$root->setDisplayName('root'); |
148
|
|
|
$root->setLoginName('root'); |
149
|
|
|
$root->setPassword(password_hash('root', PASSWORD_BCRYPT)); |
150
|
|
|
$root->save(); |
151
|
|
|
|
152
|
|
|
$stmt = $con->prepare(sprintf('UPDATE %s SET id = 0 WHERE ID = 2', $adapter->quoteIdentifierTable(UserTableMap::TABLE_NAME))); |
153
|
|
|
$stmt->execute(); |
154
|
|
|
|
155
|
|
|
$root = UserQuery::create()->findOneById(0); |
156
|
|
|
$root->addGroup($userGroup); |
157
|
|
|
$root->addGroup($adminGroup); |
158
|
|
|
$root->save(); |
159
|
|
|
|
160
|
|
|
// @TODO: Cross-SQL-Server routine wanted!! |
161
|
|
|
$stmt = $con->prepare(sprintf('ALTER TABLE %s AUTO_INCREMENT = 1', $adapter->quoteIdentifierTable(UserTableMap::TABLE_NAME))); |
162
|
|
|
$stmt->execute(); |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function installKeeko($rootUrl, $locale = self::DEFAULT_LOCALE) { |
167
|
|
|
// 1) apps |
168
|
|
|
|
169
|
|
|
// api |
170
|
|
|
$apiUrl = $rootUrl . '/api/'; |
171
|
|
|
$this->installApp('keeko/api-app'); |
172
|
|
|
$this->setupApp('keeko/api-app', $apiUrl, $locale); |
173
|
|
|
|
174
|
|
|
// developer |
175
|
|
|
$this->installApp('keeko/developer-app'); |
176
|
|
|
$this->setupApp('keeko/developer-app', $rootUrl . '/developer/', $locale); |
177
|
|
|
|
178
|
|
|
// website |
|
|
|
|
179
|
|
|
// $websiteApp = $this->installApp('keeko/website-app'); |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
// 2) preferences |
183
|
|
|
$core = $this->service->getPackageManager()->getComposerPackage('keeko/core'); |
184
|
|
|
|
185
|
|
|
$pref = new Preference(); |
186
|
|
|
$pref->setKey(SystemPreferences::VERSION); |
187
|
|
|
$pref->setValue($core->getPrettyVersion()); |
188
|
|
|
$pref->save(); |
189
|
|
|
|
190
|
|
|
$pref = new Preference(); |
191
|
|
|
$pref->setKey(SystemPreferences::PLATTFORM_NAME); |
192
|
|
|
$pref->setValue('Keeko'); |
193
|
|
|
$pref->save(); |
194
|
|
|
|
195
|
|
|
$pref = new Preference(); |
196
|
|
|
$pref->setKey(SystemPreferences::API_URL); |
197
|
|
|
$pref->setValue($apiUrl); |
198
|
|
|
$pref->save(); |
199
|
|
|
|
200
|
|
|
// 3) modules |
201
|
|
|
$this->installModule('keeko/user'); |
202
|
|
|
$this->activateModule('keeko/user'); |
203
|
|
|
|
204
|
|
|
$this->installModule('keeko/group'); |
205
|
|
|
$this->activateModule('keeko/group'); |
206
|
|
|
|
207
|
|
|
$this->installModule('keeko/auth'); |
208
|
|
|
$this->activateModule('keeko/auth'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function installApp($packageName) { |
|
|
|
|
212
|
|
|
return $this->appInstaller->install($this->io, $packageName); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function setupApp($packageName, $uri, $locale = self::DEFAULT_LOCALE) { |
216
|
|
|
$this->io->write(sprintf('[Keeko] Setup App %s at %s', $packageName, $uri)); |
217
|
|
|
$app = ApplicationQuery::create()->findOneByName($packageName); |
218
|
|
|
|
219
|
|
|
if ($app === null) { |
220
|
|
|
throw new \Exception(sprintf('Application (%s) not found', $packageName)); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$comps = parse_url($uri); |
224
|
|
|
|
225
|
|
|
$uri = new ApplicationUri(); |
226
|
|
|
$uri->setApplication($app); |
227
|
|
|
$uri->setLocalization($this->getLocalization($locale)); |
228
|
|
|
$uri->setHttphost($comps['host']); |
229
|
|
|
$uri->setBasepath($comps['path']); |
230
|
|
|
$uri->setSecure($comps['scheme'] == 'https'); |
231
|
|
|
$uri->save(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function installModule($packageName) { |
235
|
|
|
$this->moduleInstaller->install($this->io, $packageName); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function activateModule($packageName) { |
239
|
|
|
$this->moduleInstaller->activate($this->io, $packageName); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
private function installDatabase() { |
243
|
|
|
$files = [ |
244
|
|
|
'sql/keeko.sql', |
245
|
|
|
'data/static-data.sql' |
246
|
|
|
]; |
247
|
|
|
$con = Propel::getConnection(); |
|
|
|
|
248
|
|
|
|
249
|
|
|
foreach ($files as $file) { |
250
|
|
|
$path = KEEKO_PATH . '/packages/keeko/core/database/' . $file; |
251
|
|
|
|
252
|
|
|
if (file_exists($path)) { |
253
|
|
|
$sql = file_get_contents($path); |
254
|
|
|
|
255
|
|
|
try { |
256
|
|
|
$stmt = $con->prepare($sql); |
257
|
|
|
$stmt->execute(); |
258
|
|
|
} catch (\Exception $e) { |
259
|
|
|
echo $e->getMessage(); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.