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
|
|
|
* Find the module name and declare path to target directories install. |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
protected function findModuleName() |
114
|
|
|
{ |
115
|
|
|
$pathExplode = explode('/', $this->sourcePath); |
116
|
|
|
$this->name = $pathExplode[(count($pathExplode) - 1)]; |
117
|
|
|
|
118
|
|
|
$this->targetSrcPath = $this->bfwModulePath.$this->name; |
119
|
|
|
$this->targetConfigPath = $this->bfwConfigPath.$this->name; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get infos for this module from BFW Module class |
124
|
|
|
* It's a separate method for easy override. |
125
|
|
|
* |
126
|
|
|
* @return \stdClass |
127
|
|
|
*/ |
128
|
|
|
protected function getInfosFromModule() |
129
|
|
|
{ |
130
|
|
|
return \BFW\Module::installInfos($this->sourcePath); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Load module informations from files |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
public function loadInfos() |
139
|
|
|
{ |
140
|
|
|
$this->findModuleName(); |
141
|
|
|
|
142
|
|
|
$infos = $this->getInfosFromModule(); |
143
|
|
|
|
144
|
|
|
//check if srcPath is define |
145
|
|
|
if (!property_exists($infos, 'srcPath')) { |
146
|
|
|
throw new Exception( |
147
|
|
|
'srcPath must be present in install json file for module ' |
148
|
|
|
.$this->name |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
//Defines default paths |
153
|
|
|
$this->sourceSrcPath = $infos->srcPath; |
154
|
|
|
$this->sourceConfigPath = $infos->srcPath; |
155
|
|
|
|
156
|
|
|
//Defines properties |
157
|
|
|
|
158
|
|
|
if (property_exists($infos, 'configFiles')) { |
159
|
|
|
$this->configFilesList = (array) $infos->configFiles; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (property_exists($infos, 'configPath')) { |
163
|
|
|
$this->sourceConfigPath = $infos->configPath; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (property_exists($infos, 'installScript')) { |
167
|
|
|
$this->sourceInstallScript = $infos->installScript; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$this->sourceSrcPath = realpath( |
171
|
|
|
$this->sourcePath.'/'.$this->sourceSrcPath |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
$this->sourceConfigPath = realpath( |
175
|
|
|
$this->sourcePath.'/'.$this->sourceConfigPath |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Run module installation |
181
|
|
|
* |
182
|
|
|
* @param boolean $reinstall : If we force reinstall module |
183
|
|
|
* |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
|
|
public function install($reinstall) |
187
|
|
|
{ |
188
|
|
|
$this->forceReinstall = $reinstall; |
189
|
|
|
|
190
|
|
|
echo $this->name." : Run install.\n"; |
191
|
|
|
|
192
|
|
|
try { |
193
|
|
|
$this->createSymbolicLink(); |
194
|
|
|
$this->copyConfigFiles(); |
195
|
|
|
$this->runInstallScript(); |
196
|
|
|
} catch (Exception $e) { |
197
|
|
|
trigger_error('Module '.$this->name.' install error : '.$e->getMessage(), E_USER_WARNING); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Create symlink in bfw project module directory |
203
|
|
|
* |
204
|
|
|
* @return void |
205
|
|
|
* |
206
|
|
|
* @throws Exception : If remove symlink fail for reinstall option |
207
|
|
|
*/ |
208
|
|
|
protected function createSymbolicLink() |
209
|
|
|
{ |
210
|
|
|
echo ' > Create symbolic link ... '; |
211
|
|
|
|
212
|
|
|
$alreadyCreated = file_exists($this->targetSrcPath); |
213
|
|
|
|
214
|
|
|
//If symlink already exist and it's a reinstall mode |
215
|
|
|
if ($alreadyCreated && $this->forceReinstall === true) { |
216
|
|
|
echo '[Force Reinstall: Remove symlink] '; |
217
|
|
|
$alreadyCreated = false; |
218
|
|
|
|
219
|
|
|
//Error with remove symlink |
220
|
|
|
if (!unlink($this->targetSrcPath)) { |
221
|
|
|
echo "\033[1;31mSymbolic link remove fail.\033[0m\n"; |
222
|
|
|
throw new Exception('Reinstall fail. Symlink remove error'); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
//If module already exist in "modules" directory |
227
|
|
|
if ($alreadyCreated) { |
228
|
|
|
echo "\033[1;33m" |
229
|
|
|
.'Not created. Module already exist in \'modules\' directory.' |
230
|
|
|
."\033[0m\n"; |
231
|
|
|
return; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
//If symbolic link create fail. |
235
|
|
|
if (!symlink($this->sourceSrcPath, $this->targetSrcPath)) { |
236
|
|
|
echo "\033[1;31mSymbolic link creation fail.\033[0m\n"; |
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
echo "\033[1;32mDone\033[0m\n"; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Create a directory in bfw project config directory for this module and |
245
|
|
|
* copy all config files declared in this directory |
246
|
|
|
* |
247
|
|
|
* @return void |
248
|
|
|
*/ |
249
|
|
|
protected function copyConfigFiles() |
250
|
|
|
{ |
251
|
|
|
echo ' > Copy config files : '."\n"; |
252
|
|
|
|
253
|
|
|
//No file to copy |
254
|
|
|
if ($this->configFilesList === []) { |
255
|
|
|
echo ' >> ' |
256
|
|
|
."\033[1;33m" |
257
|
|
|
.'No config file declared. Pass' |
258
|
|
|
."\033[0m\n"; |
259
|
|
|
|
260
|
|
|
return; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
//Create the module directory in config directory. |
264
|
|
|
$configDirStatus = $this->createConfigDirectory(); |
265
|
|
|
if ($configDirStatus === false) { |
266
|
|
|
return; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
//Copy each config file declared |
270
|
|
|
foreach ($this->configFilesList as $configFile) { |
271
|
|
|
try { |
272
|
|
|
$this->copyConfigFile($configFile); |
273
|
|
|
} catch (Exception $e) { |
274
|
|
|
trigger_error( |
275
|
|
|
'Module '.$this->name.' Config file '.$configFile |
276
|
|
|
.' copy error: '.$e->getMessage(), |
277
|
|
|
E_USER_WARNING |
278
|
|
|
); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Create a directory in bfw project config directory for this module |
285
|
|
|
* |
286
|
|
|
* @return boolean : If directory exist. |
287
|
|
|
* |
288
|
|
|
* @throws Exception : If remove directory fail for reinstall option |
289
|
|
|
*/ |
290
|
|
|
protected function createConfigDirectory() |
291
|
|
|
{ |
292
|
|
|
echo ' >> Create config directory for this module ... '; |
293
|
|
|
|
294
|
|
|
$alreadyExist = file_exists($this->targetConfigPath); |
295
|
|
|
|
296
|
|
|
//If the directory already exist and if it's a reinstall |
297
|
|
|
if ($alreadyExist && $this->forceReinstall === true) { |
298
|
|
|
echo '[Force Reinstall: Remove directory] '; |
299
|
|
|
|
300
|
|
|
$calledClass = get_called_class(); //Autorize extends this class |
301
|
|
|
$alreadyExist = false; |
302
|
|
|
|
303
|
|
|
if (!$calledClass::removeDirectory($this->targetConfigPath)) { |
304
|
|
|
echo "\033[1;31m" |
305
|
|
|
.'Remove module config directory fail.' |
306
|
|
|
."\033[0m\n"; |
307
|
|
|
|
308
|
|
|
throw new Exception( |
309
|
|
|
'Reinstall fail. Remove module config directory error.' |
310
|
|
|
); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
//If the directory already exist, nothing to do |
315
|
|
|
if ($alreadyExist) { |
316
|
|
|
echo "\033[1;33mAlready exist.\033[0m\n"; |
317
|
|
|
return true; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
//Create the directory |
321
|
|
|
if (mkdir($this->targetConfigPath, 0755)) { |
322
|
|
|
echo "\033[1;32mCreated.\033[0m\n"; |
323
|
|
|
return true; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
//If error during the directory creation |
327
|
|
|
trigger_error('Module '.$this->name.' Error to create config directory', E_USER_WARNING); |
328
|
|
|
echo "\033[1;31mFail. \033[0m\n"; |
329
|
|
|
|
330
|
|
|
return false; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Supprime les dossiers récursivement |
335
|
|
|
* |
336
|
|
|
* @param string $dirPath Le chemin vers le dossier |
337
|
|
|
* |
338
|
|
|
* @return boolean |
339
|
|
|
*/ |
340
|
|
|
protected static function removeDirectory($dirPath) |
341
|
|
|
{ |
342
|
|
|
$calledClass = get_called_class(); //Autorize extends this class |
343
|
|
|
|
344
|
|
|
$dir = opendir($dirPath); |
345
|
|
|
if ($dir === false) { |
346
|
|
|
return false; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
while (($file = readdir($dir)) !== false) { |
350
|
|
|
if ($file === '.' || $file === '..') { |
351
|
|
|
continue; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$removeStatus = true; |
355
|
|
|
$filePath = $dirPath.'/'.$file; |
356
|
|
|
|
357
|
|
|
if (is_dir($filePath)) { |
358
|
|
|
$removeStatus = $calledClass::removeDirectory($filePath); |
359
|
|
|
} elseif (is_file($filePath) || is_link($filePath)) { |
360
|
|
|
$removeStatus = unlink($filePath); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
if ($removeStatus === false) { |
364
|
|
|
return false; |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
closedir($dir); |
369
|
|
|
return rmdir($dirPath); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Copy a config file into config directory for this module |
374
|
|
|
* |
375
|
|
|
* @param string $configFileName : The config filename |
376
|
|
|
* |
377
|
|
|
* @return void |
378
|
|
|
* |
379
|
|
|
* @throws Exception If copy fail or if source file not exist |
380
|
|
|
*/ |
381
|
|
|
protected function copyConfigFile($configFileName) |
382
|
|
|
{ |
383
|
|
|
echo ' >> Copy '.$configFileName.' ... '; |
384
|
|
|
|
385
|
|
|
//Define paths to the config file |
386
|
|
|
$sourceFile = realpath($this->sourceConfigPath.'/'.$configFileName); |
387
|
|
|
$targetFile = realpath($this->targetConfigPath).'/'.$configFileName; |
388
|
|
|
|
389
|
|
|
//Check if config file already exist |
390
|
|
|
if (file_exists($targetFile)) { |
391
|
|
|
echo "\033[1;33mAlready exist.\033[0m\n"; |
392
|
|
|
return; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
//If source file not exist |
396
|
|
|
if (!file_exists($sourceFile)) { |
397
|
|
|
echo "\033[1;31mConfig file not exist in module source.\033[0m\n"; |
398
|
|
|
throw new Exception('Source file not exist'); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
//Alors on copie le fichier vers le dossier /configs/[monModule]/ |
402
|
|
|
if (!copy($sourceFile, $targetFile)) { |
403
|
|
|
echo "\033[1;31mCopy fail.\033[0m\n"; |
404
|
|
|
throw new Exception('Copy fail'); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
echo "\033[1;32mDone\033[0m\n"; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Run specific module install script if declared |
412
|
|
|
* |
413
|
|
|
* @return void |
414
|
|
|
*/ |
415
|
|
|
protected function runInstallScript() |
416
|
|
|
{ |
417
|
|
|
echo ' > Run install specific script :'."\n"; |
418
|
|
|
|
419
|
|
|
//If no script to complete the install |
420
|
|
|
if ( |
421
|
|
|
$this->sourceInstallScript === '' |
422
|
|
|
|| $this->sourceInstallScript === false |
423
|
|
|
) { |
424
|
|
|
echo " >> \033[1;33m" |
425
|
|
|
.'No specific script declared. Pass' |
426
|
|
|
."\033[0m\n"; |
427
|
|
|
|
428
|
|
|
return; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
//If the module ask a install script but not declare the name |
432
|
|
|
//Use the default name |
433
|
|
|
if ($this->sourceInstallScript === true) { |
434
|
|
|
$this->sourceInstallScript = 'runInstallModule.php'; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
//Include the file for complete the install |
438
|
|
|
require_once($this->sourcePath.'/'.$this->sourceInstallScript); |
439
|
|
|
echo "\n"; |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
|