GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 3.0 ( afe539...dd669d )
by Vermeulen
02:14
created

ModuleInstall::createSymbolicLink()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 34
rs 8.439
cc 6
eloc 18
nc 7
nop 0
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 symlink] ';
299
            $alreadyExist = false;
300
            
301
            if (!rmdir($this->targetConfigPath)) {
302
                echo "\033[1;31m"
303
                    .'Remove module config directory fail.'
304
                    ."\033[0m\n";
305
                
306
                throw new Exception(
307
                    'Reinstall fail. Remove module config directory error.'
308
                );
309
            }
310
        }
311
        
312
        //If the directory already exist, nothing to do
313
        if ($alreadyExist) {
314
            echo "\033[1;33mAlready exist.\033[0m\n";
315
            return true;
316
        }
317
        
318
        //Create the directory
319
        if (mkdir($this->targetConfigPath, 0755)) {
320
            echo "\033[1;32mCreated. \033[0m\n";
321
            return true;
322
        }
323
324
        //If error during the directory creation
325
        trigger_error('Module '.$this->name.' Error to create config directory', E_USER_WARNING);
326
        echo "\033[1;31mFail. \033[0m\n";
327
        
328
        return false;
329
    }
330
    
331
    /**
332
     * Copy a config file into config directory for this module
333
     * 
334
     * @param string $configFileName : The config filename
335
     * 
336
     * @return void
337
     * 
338
     * @throws Exception If copy fail or if source file not exist
339
     */
340
    protected function copyConfigFile($configFileName)
341
    {
342
        echo ' >> Copy '.$configFileName.' ... ';
343
344
        //Define paths to the config file
345
        $sourceFile = realpath($this->sourceConfigPath.'/'.$configFileName);
346
        $targetFile = realpath($this->targetConfigPath).'/'.$configFileName;
347
348
        //Check if config file already exist
349
        if (file_exists($targetFile)) {
350
            echo "\033[1;33mAlready exist.\033[0m\n";
351
            return;
352
        }
353
354
        //If source file not exist
355
        if (!file_exists($sourceFile)) {
356
            echo "\033[1;31mConfig file not exist in module source.\033[0m\n";
357
            throw new Exception('Source file not exist');
358
        }
359
360
        //Alors on copie le fichier vers le dossier /configs/[monModule]/
361
        if (!copy($sourceFile, $targetFile)) {
362
            echo "\033[1;31mCopy fail.\033[0m\n";
363
            throw new Exception('Copy fail');
364
        }
365
366
        echo "\033[1;32mDone\033[0m\n";
367
    }
368
369
    /**
370
     * Run specific module install script if declared
371
     * 
372
     * @return void
373
     */
374
    protected function runInstallScript()
375
    {
376
        echo ' > Run install specific script : ';
377
378
        //If no script to complete the install
379
        if (
380
            $this->sourceInstallScript === ''
381
            || $this->sourceInstallScript === false
382
        ) {
383
            echo "\033[1;33m"
384
                .'No specific script declared. Pass'
385
                ."\033[0m\n";
386
            
387
            return;
388
        }
389
390
        //If the module ask a install script but not declare the name
391
        //Use the default name
392
        if ($this->sourceInstallScript === true) {
393
            $this->sourceInstallScript = 'runInstallModule.php';
394
        }
395
396
        //Include the file for complete the install
397
        require_once($this->sourcePath.'/'.$this->sourceInstallScript);
398
    }
399
}
400