|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BFW\Install; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class to get module install informations and run install of the module |
|
9
|
|
|
*/ |
|
10
|
|
|
class ModuleInstall |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @const INSTALL_SCRIPT_VERSION : Script's version |
|
14
|
|
|
*/ |
|
15
|
|
|
const INSTALL_SCRIPT_VERSION = '3.0.0'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string $projectPath : Path to root bfw project |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $projectPath = ''; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string $bfwConfigPath : Path to bfw config directory |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $bfwConfigPath = ''; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string $bfwModulePath : Path to bfw modules directory |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $bfwModulePath = ''; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var boolean $forceReinstall : Force complete reinstall of module |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $forceReinstall = false; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string $name : Module name |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $name = ''; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string $sourcePath : Path to module which be installed |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $sourcePath = ''; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string $sourceSrcPath : Path to directory contains file to install |
|
49
|
|
|
* in project module directory |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $sourceSrcPath = ''; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string $sourceConfigPath : Path to directory contains config file |
|
55
|
|
|
* to install in projet config directory |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $sourceConfigPath = ''; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var array $configFiles : List of config file |
|
61
|
|
|
* to copy on the config directory |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $configFilesList = []; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var string|bool $sourceInstallScript : Script to run for a specific |
|
67
|
|
|
* install of the module |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $sourceInstallScript = ''; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var string $targetSrcPath : Path to directory where module will be |
|
73
|
|
|
* installed |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $targetSrcPath = ''; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var string $targetConfigPath : Path to directory where config files |
|
79
|
|
|
* will be installed |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $targetConfigPath = ''; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Constructor |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $projectPath Path to root bfw project |
|
87
|
|
|
* @param string $modulePath Path to module which be installed |
|
88
|
|
|
*/ |
|
89
|
|
|
public function __construct($projectPath, $modulePath) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->projectPath = $projectPath; |
|
92
|
|
|
$this->sourcePath = $modulePath; |
|
93
|
|
|
|
|
94
|
|
|
$this->bfwConfigPath = $this->projectPath.'/app/config/'; |
|
95
|
|
|
$this->bfwModulePath = $this->projectPath.'/app/modules/'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get accessor to module name |
|
100
|
|
|
* |
|
101
|
|
|
* @return string : Module name |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getName() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->name; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get accessor to source module path |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getSourcePath() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->sourcePath; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get accessor to the install script file or list |
|
120
|
|
|
* |
|
121
|
|
|
* @return string|array |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getSourceInstallScript() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->sourceInstallScript; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Find the module name and declare path to target directories install. |
|
130
|
|
|
* |
|
131
|
|
|
* @return void |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function findModuleName() |
|
134
|
|
|
{ |
|
135
|
|
|
$pathExplode = explode('/', $this->sourcePath); |
|
136
|
|
|
$this->name = $pathExplode[(count($pathExplode) - 1)]; |
|
137
|
|
|
|
|
138
|
|
|
$this->targetSrcPath = $this->bfwModulePath.$this->name; |
|
139
|
|
|
$this->targetConfigPath = $this->bfwConfigPath.$this->name; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get infos for this module from BFW Module class |
|
144
|
|
|
* It's a separate method for easy override. |
|
145
|
|
|
* |
|
146
|
|
|
* @return \stdClass |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function getInfosFromModule() |
|
149
|
|
|
{ |
|
150
|
|
|
return \BFW\Module::installInfos($this->sourcePath); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Load module informations from files |
|
155
|
|
|
* |
|
156
|
|
|
* @return void |
|
157
|
|
|
*/ |
|
158
|
|
|
public function loadInfos() |
|
159
|
|
|
{ |
|
160
|
|
|
$this->findModuleName(); |
|
161
|
|
|
|
|
162
|
|
|
$infos = $this->getInfosFromModule(); |
|
163
|
|
|
|
|
164
|
|
|
//check if srcPath is define |
|
165
|
|
|
if (!property_exists($infos, 'srcPath')) { |
|
166
|
|
|
throw new Exception( |
|
167
|
|
|
'srcPath must be present in install json file for module ' |
|
168
|
|
|
.$this->name |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
//Defines default paths |
|
173
|
|
|
$this->sourceSrcPath = $infos->srcPath; |
|
174
|
|
|
$this->sourceConfigPath = $infos->srcPath; |
|
175
|
|
|
|
|
176
|
|
|
//Defines properties |
|
177
|
|
|
|
|
178
|
|
|
if (property_exists($infos, 'configFiles')) { |
|
179
|
|
|
$this->configFilesList = (array) $infos->configFiles; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if (property_exists($infos, 'configPath')) { |
|
183
|
|
|
$this->sourceConfigPath = $infos->configPath; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
if (property_exists($infos, 'installScript')) { |
|
187
|
|
|
$this->sourceInstallScript = $infos->installScript; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$this->sourceSrcPath = realpath( |
|
191
|
|
|
$this->sourcePath.'/'.$this->sourceSrcPath |
|
192
|
|
|
); |
|
193
|
|
|
|
|
194
|
|
|
$this->sourceConfigPath = realpath( |
|
195
|
|
|
$this->sourcePath.'/'.$this->sourceConfigPath |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Run module installation |
|
201
|
|
|
* |
|
202
|
|
|
* @param boolean $reinstall : If we force reinstall module |
|
203
|
|
|
* |
|
204
|
|
|
* @return void |
|
205
|
|
|
*/ |
|
206
|
|
|
public function install($reinstall) |
|
207
|
|
|
{ |
|
208
|
|
|
$this->forceReinstall = $reinstall; |
|
209
|
|
|
|
|
210
|
|
|
echo $this->name." : Run install.\n"; |
|
211
|
|
|
|
|
212
|
|
|
try { |
|
213
|
|
|
$this->createSymbolicLink(); |
|
214
|
|
|
$this->copyConfigFiles(); |
|
215
|
|
|
$this->checkInstallScript(); |
|
216
|
|
|
} catch (Exception $e) { |
|
217
|
|
|
trigger_error('Module '.$this->name.' install error : '.$e->getMessage(), E_USER_WARNING); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Create symlink in bfw project module directory |
|
223
|
|
|
* |
|
224
|
|
|
* @return void |
|
225
|
|
|
* |
|
226
|
|
|
* @throws Exception : If remove symlink fail for reinstall option |
|
227
|
|
|
*/ |
|
228
|
|
|
protected function createSymbolicLink() |
|
229
|
|
|
{ |
|
230
|
|
|
echo ' > Create symbolic link ... '; |
|
231
|
|
|
|
|
232
|
|
|
$alreadyCreated = file_exists($this->targetSrcPath); |
|
233
|
|
|
|
|
234
|
|
|
//If symlink already exist and it's a reinstall mode |
|
235
|
|
|
if ($alreadyCreated && $this->forceReinstall === true) { |
|
236
|
|
|
echo '[Force Reinstall: Remove symlink] '; |
|
237
|
|
|
$alreadyCreated = false; |
|
238
|
|
|
|
|
239
|
|
|
//Error with remove symlink |
|
240
|
|
|
if (!unlink($this->targetSrcPath)) { |
|
241
|
|
|
echo "\033[1;31mSymbolic link remove fail.\033[0m\n"; |
|
242
|
|
|
throw new Exception('Reinstall fail. Symlink remove error'); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
//If module already exist in "modules" directory |
|
247
|
|
|
if ($alreadyCreated) { |
|
248
|
|
|
echo "\033[1;33m" |
|
249
|
|
|
.'Not created. Module already exist in \'modules\' directory.' |
|
250
|
|
|
."\033[0m\n"; |
|
251
|
|
|
return; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
//If symbolic link create fail. |
|
255
|
|
|
if (!symlink($this->sourceSrcPath, $this->targetSrcPath)) { |
|
256
|
|
|
echo "\033[1;31mSymbolic link creation fail.\033[0m\n"; |
|
257
|
|
|
return; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
echo "\033[1;32mDone\033[0m\n"; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Create a directory in bfw project config directory for this module and |
|
265
|
|
|
* copy all config files declared in this directory |
|
266
|
|
|
* |
|
267
|
|
|
* @return void |
|
268
|
|
|
*/ |
|
269
|
|
|
protected function copyConfigFiles() |
|
270
|
|
|
{ |
|
271
|
|
|
echo ' > Copy config files : '."\n"; |
|
272
|
|
|
|
|
273
|
|
|
//No file to copy |
|
274
|
|
|
if ($this->configFilesList === []) { |
|
275
|
|
|
echo ' >> ' |
|
276
|
|
|
."\033[1;33m" |
|
277
|
|
|
.'No config file declared. Pass' |
|
278
|
|
|
."\033[0m\n"; |
|
279
|
|
|
|
|
280
|
|
|
return; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
//Create the module directory in config directory. |
|
284
|
|
|
$configDirStatus = $this->createConfigDirectory(); |
|
285
|
|
|
if ($configDirStatus === false) { |
|
286
|
|
|
return; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
//Copy each config file declared |
|
290
|
|
|
foreach ($this->configFilesList as $configFile) { |
|
291
|
|
|
try { |
|
292
|
|
|
$this->copyConfigFile($configFile); |
|
293
|
|
|
} catch (Exception $e) { |
|
294
|
|
|
trigger_error( |
|
295
|
|
|
'Module '.$this->name.' Config file '.$configFile |
|
296
|
|
|
.' copy error: '.$e->getMessage(), |
|
297
|
|
|
E_USER_WARNING |
|
298
|
|
|
); |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Create a directory in bfw project config directory for this module |
|
305
|
|
|
* |
|
306
|
|
|
* @return boolean : If directory exist. |
|
307
|
|
|
* |
|
308
|
|
|
* @throws Exception : If remove directory fail for reinstall option |
|
309
|
|
|
*/ |
|
310
|
|
|
protected function createConfigDirectory() |
|
311
|
|
|
{ |
|
312
|
|
|
echo ' >> Create config directory for this module ... '; |
|
313
|
|
|
|
|
314
|
|
|
$alreadyExist = file_exists($this->targetConfigPath); |
|
315
|
|
|
|
|
316
|
|
|
//If the directory already exist and if it's a reinstall |
|
317
|
|
|
if ($alreadyExist && $this->forceReinstall === true) { |
|
318
|
|
|
echo '[Force Reinstall: Remove directory] '; |
|
319
|
|
|
|
|
320
|
|
|
$calledClass = get_called_class(); //Autorize extends this class |
|
321
|
|
|
$alreadyExist = false; |
|
322
|
|
|
|
|
323
|
|
|
if (!$calledClass::removeDirectory($this->targetConfigPath)) { |
|
324
|
|
|
echo "\033[1;31m" |
|
325
|
|
|
.'Remove module config directory fail.' |
|
326
|
|
|
."\033[0m\n"; |
|
327
|
|
|
|
|
328
|
|
|
throw new Exception( |
|
329
|
|
|
'Reinstall fail. Remove module config directory error.' |
|
330
|
|
|
); |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
//If the directory already exist, nothing to do |
|
335
|
|
|
if ($alreadyExist) { |
|
336
|
|
|
echo "\033[1;33mAlready exist.\033[0m\n"; |
|
337
|
|
|
return true; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
//Create the directory |
|
341
|
|
|
if (mkdir($this->targetConfigPath, 0755)) { |
|
342
|
|
|
echo "\033[1;32mCreated.\033[0m\n"; |
|
343
|
|
|
return true; |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
//If error during the directory creation |
|
347
|
|
|
trigger_error('Module '.$this->name.' Error to create config directory', E_USER_WARNING); |
|
348
|
|
|
echo "\033[1;31mFail. \033[0m\n"; |
|
349
|
|
|
|
|
350
|
|
|
return false; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Supprime les dossiers récursivement |
|
355
|
|
|
* |
|
356
|
|
|
* @param string $dirPath Le chemin vers le dossier |
|
357
|
|
|
* |
|
358
|
|
|
* @return boolean |
|
359
|
|
|
*/ |
|
360
|
|
|
protected static function removeDirectory($dirPath) |
|
361
|
|
|
{ |
|
362
|
|
|
$calledClass = get_called_class(); //Autorize extends this class |
|
363
|
|
|
|
|
364
|
|
|
$dir = opendir($dirPath); |
|
365
|
|
|
if ($dir === false) { |
|
366
|
|
|
return false; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
while (($file = readdir($dir)) !== false) { |
|
370
|
|
|
if ($file === '.' || $file === '..') { |
|
371
|
|
|
continue; |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
$removeStatus = true; |
|
375
|
|
|
$filePath = $dirPath.'/'.$file; |
|
376
|
|
|
|
|
377
|
|
|
if (is_dir($filePath)) { |
|
378
|
|
|
$removeStatus = $calledClass::removeDirectory($filePath); |
|
379
|
|
|
} elseif (is_file($filePath) || is_link($filePath)) { |
|
380
|
|
|
$removeStatus = unlink($filePath); |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
if ($removeStatus === false) { |
|
384
|
|
|
return false; |
|
385
|
|
|
} |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
closedir($dir); |
|
389
|
|
|
return rmdir($dirPath); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* Copy a config file into config directory for this module |
|
394
|
|
|
* |
|
395
|
|
|
* @param string $configFileName : The config filename |
|
396
|
|
|
* |
|
397
|
|
|
* @return void |
|
398
|
|
|
* |
|
399
|
|
|
* @throws Exception If copy fail or if source file not exist |
|
400
|
|
|
*/ |
|
401
|
|
|
protected function copyConfigFile($configFileName) |
|
402
|
|
|
{ |
|
403
|
|
|
echo ' >> Copy '.$configFileName.' ... '; |
|
404
|
|
|
|
|
405
|
|
|
//Define paths to the config file |
|
406
|
|
|
$sourceFile = realpath($this->sourceConfigPath.'/'.$configFileName); |
|
407
|
|
|
$targetFile = realpath($this->targetConfigPath).'/'.$configFileName; |
|
408
|
|
|
|
|
409
|
|
|
//Check if config file already exist |
|
410
|
|
|
if (file_exists($targetFile)) { |
|
411
|
|
|
echo "\033[1;33mAlready exist.\033[0m\n"; |
|
412
|
|
|
return; |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
//If source file not exist |
|
416
|
|
|
if (!file_exists($sourceFile)) { |
|
417
|
|
|
echo "\033[1;31mConfig file not exist in module source.\033[0m\n"; |
|
418
|
|
|
throw new Exception('Source file not exist'); |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
//Alors on copie le fichier vers le dossier /configs/[monModule]/ |
|
422
|
|
|
if (!copy($sourceFile, $targetFile)) { |
|
423
|
|
|
echo "\033[1;31mCopy fail.\033[0m\n"; |
|
424
|
|
|
throw new Exception('Copy fail'); |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
echo "\033[1;32mDone\033[0m\n"; |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* Run specific module install script if declared |
|
432
|
|
|
* |
|
433
|
|
|
* @return void |
|
434
|
|
|
*/ |
|
435
|
|
|
protected function checkInstallScript() |
|
436
|
|
|
{ |
|
437
|
|
|
echo ' > Check install specific script :'."\n"; |
|
438
|
|
|
|
|
439
|
|
|
//If no script to complete the install |
|
440
|
|
|
if ( |
|
441
|
|
|
$this->sourceInstallScript === '' |
|
442
|
|
|
|| $this->sourceInstallScript === false |
|
443
|
|
|
) { |
|
444
|
|
|
echo " >> \033[1;33m" |
|
445
|
|
|
.'No specific script declared. Pass' |
|
446
|
|
|
."\033[0m\n"; |
|
447
|
|
|
|
|
448
|
|
|
return; |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
//If the module ask a install script but not declare the name |
|
452
|
|
|
//Use the default name |
|
453
|
|
|
if ($this->sourceInstallScript === true) { |
|
454
|
|
|
$this->sourceInstallScript = 'runInstallModule.php'; |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
echo " >> \033[1;33m" |
|
458
|
|
|
.'Scripts find. Add to list to execute.' |
|
459
|
|
|
."\033[0m\n"; |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
public function runInstall($scriptName) { |
|
463
|
|
|
echo " >> \033[1;33m".'Execute script '.$scriptName."\033[0m\n"; |
|
464
|
|
|
|
|
465
|
|
|
require_once($this->sourcePath.'/'.$this->sourceInstallScript); |
|
466
|
|
|
echo "\n"; |
|
467
|
|
|
} |
|
468
|
|
|
} |
|
469
|
|
|
|