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 ( 1aaa8a...5b7375 )
by Vermeulen
02:33
created

ModuleInstall::getInfosFromModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 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 $bfwPath : Path to root bfw project
19
     */
20
    protected $bfwPath = '';
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 $pathToModule : Path to module which be installed
44
     */
45
    protected $pathToModule = '';
46
47
    /**
48
     * @var string $srcPath : Path to module source files
49
     */
50
    protected $srcPath = '';
51
52
    /**
53
     * @var string $configPath : Path to module config files
54
     */
55
    protected $configPath = '';
56
57
    /**
58
     * @var array $configFiles : List of config file
59
     *                              to copy on the config directory
60
     */
61
    protected $configFiles = [];
62
63
    /**
64
     * @var string|bool $installScript : Script to run for a specific install
65
     *                                  of the module
66
     */
67
    protected $installScript = '';
68
69
    /**
70
     * Constructor
71
     * 
72
     * @param string $bfwPath Path to root bfw project
73
     * @param string $pathToModule Path to module which be installed
74
     */
75
    public function __construct($bfwPath, $pathToModule)
76
    {
77
        $this->bfwPath      = $bfwPath;
78
        $this->pathToModule = $pathToModule;
79
80
        $this->bfwConfigPath = $this->bfwPath.'/app/config/';
81
        $this->bfwModulePath = $this->bfwPath.'/app/modules/';
82
    }
83
84
    /**
85
     * Get accessor to module name
86
     * 
87
     * @return string : Module name
88
     */
89
    public function getName()
90
    {
91
        return $this->name;
92
    }
93
94
    /**
95
     * Load module informations from files
96
     * Define module name
97
     * 
98
     * @return void
99
     */
100
    public function loadInfos()
101
    {
102
        //Get the module name
103
        $pathExplode = explode('/', $this->pathToModule);
104
        $this->name  = $pathExplode[(count($pathExplode) - 1)];
105
106
        //Define path
107
        $this->bfwConfigPath .= $this->name;
108
        $this->bfwModulePath .= $this->name;
109
110
        $infos = $this->getInfosFromModule();
111
        
112
        //check if srcPath is define
113
        if (!property_exists($infos, 'srcPath')) {
114
            throw new Exception(
115
                'srcPath must be present in install json file for module '
116
                .$this->name
117
            );
118
        }
119
120
        //Defines default paths
121
        $this->srcPath    = $infos->srcPath;
122
        $this->configPath = $infos->srcPath;
123
124
        //Defines properties
125
        
126
        if (property_exists($infos, 'configFiles')) {
127
            $this->configFiles = (array) $infos->configFiles;
128
        }
129
130
        if (property_exists($infos, 'configPath')) {
131
            $this->configPath = $infos->configPath;
132
        }
133
134
        if (property_exists($infos, 'installScript')) {
135
            $this->installScript = $infos->installScript;
136
        }
137
138
        //Update srcPath to get the complete path
139
        $this->srcPath = realpath($this->pathToModule.'/'.$this->srcPath);
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->pathToModule);
151
    }
152
153
    /**
154
     * Run module installation
155
     * 
156
     * @param boolean $reinstall : If we force reinstall module
157
     * 
158
     * @return void
159
     */
160
    public function install($reinstall)
161
    {
162
        $this->forceReinstall = $reinstall;
163
        
164
        echo $this->name." : Run install.\n";
165
        
166
        try {
167
            $this->createSymbolicLink();
168
            $this->copyConfigFiles();
169
            $this->runInstallScript();
170
        } catch (Exception $e) {
171
            trigger_error('Module '.$this->name.' install error : '.$e->getMessage(), E_USER_WARNING);
172
        }
173
    }
174
    
175
    /**
176
     * Create symlink in bfw project module directory
177
     * 
178
     * @return void
179
     * 
180
     * @throws Exception : If remove symlink fail for reinstall option
181
     */
182
    protected function createSymbolicLink()
183
    {
184
        echo ' > Create symbolic link ... '."\n";
185
186
        $targetPath     = $this->bfwModulePath;
187
        $alreadyCreated = file_exists($targetPath);
188
189
        //If symlink already exist and it's a reinstall mode
190
        if ($alreadyCreated && $this->forceReinstall === true) {
191
            echo '[Force Reinstall: Remove symlink] ';
192
            $alreadyCreated = false;
193
194
            //Error with remove symlink
195
            if (!unlink($targetPath)) {
196
                echo "\033[1;31m Symbolic link remove fail.\033[0m\n";
197
                throw new Exception('Reinstall fail. Symlink remove error');
198
            }
199
        }
200
201
        //If module already exist in "modules" directory
202
        if ($alreadyCreated) {
203
            echo "\033[1;33m"
204
                .' Not created. Module already exist in \'modules\' directory.'
205
                ."\033[0m\n";
206
            return;
207
        }
208
209
        //If symbolic link create fail.
210
        if (!symlink($this->srcPath, $targetPath)) {
211
            echo "\033[1;31m Symbolic link creation fail.\033[0m\n";
212
            return;
213
        }
214
215
        echo "\033[1;32m Done\033[0m\n";
216
    }
217
218
    /**
219
     * Create a directory in bfw project config directory for this module and
220
     * copy all config files declared in this directory
221
     * 
222
     * @return void
223
     */
224
    protected function copyConfigFiles()
225
    {
226
        echo ' > Copy config files : '."\n";
227
228
        //No file to copy
229
        if ($this->configFiles === []) {
230
            echo ' >> '
231
                ."\033[1;33m"
232
                .'No config file declared. Pass'
233
                ."\033[0m\n";
234
            
235
            return;
236
        }
237
238
        //Create the module directory in config directory.
239
        $configDirStatus = $this->createConfigDirectory();
240
        if ($configDirStatus === false) {
241
            return;
242
        }
243
244
        //Copy each config file declared
245
        foreach ($this->configFiles as $configFile) {
246
            try {
247
                $this->copyConfigFile($configFile);
248
            } catch (Exception $e) {
249
                trigger_error(
250
                    'Module '.$this->name.' Config file '.$configFile
251
                    .' copy error: '.$e->getMessage(),
252
                    E_USER_WARNING
253
                );
254
            }
255
        }
256
    }
257
258
    /**
259
     * Create a directory in bfw project config directory for this module
260
     * 
261
     * @return boolean : If directory exist.
262
     * 
263
     * @throws Exception : If remove directory fail for reinstall option
264
     */
265
    protected function createConfigDirectory()
266
    {
267
        echo ' >> Create config directory for this module ... ';
268
        
269
        $targetDirectory = $this->bfwConfigPath;
270
        $alreadyExist    = file_exists($targetDirectory);
271
        
272
        //If the directory already exist and if it's a reinstall
273
        if ($alreadyExist && $this->forceReinstall === true) {
274
            echo '[Force Reinstall: Remove symlink] ';
275
            $alreadyExist = false;
276
            
277
            if (!rmdir($targetDirectory)) {
278
                echo "\033[1;31m "
279
                    .'Remove module config directory fail.'
280
                    ."\033[0m\n";
281
                
282
                throw new Exception(
283
                    'Reinstall fail. Remove module config directory error.'
284
                );
285
            }
286
        }
287
        
288
        //If the directory already exist, nothing to do
289
        if ($alreadyExist) {
290
            echo "\033[1;33m Already exist.\033[0m\n";
291
            return true;
292
        }
293
        
294
        //Create the directory
295
        if (mkdir($targetDirectory, 0755)) {
296
            echo "\033[1;32m Created. \033[0m\n";
297
            return true;
298
        }
299
300
        //If error during the directory creation
301
        trigger_error('Module '.$this->name.' Error to create config directory', E_USER_WARNING);
302
        echo "\033[1;31m Fail. \033[0m\n";
303
        
304
        return false;
305
    }
306
    
307
    /**
308
     * Copy a config file into config directory for this module
309
     * 
310
     * @param string $configFileName : The config filename
311
     * 
312
     * @return void
313
     * 
314
     * @throws Exception If copy fail or if source file not exist
315
     */
316
    protected function copyConfigFile($configFileName)
317
    {
318
        echo ' >> Copy '.$configFileName.' ... ';
319
320
        //Define paths to the config file
321
        $sourceFile = realpath($this->configPath.'/'.$configFileName);
322
        $targetFile = realpath($this->bfwConfigPath.'/'.$configFileName);
323
324
        //Check if config file already exist
325
        if (file_exists($targetFile)) {
326
            echo "\033[1;33m Existe déjà.\033[0m\n";
327
            return;
328
        }
329
330
        //If source file not exist
331
        if (!file_exists($sourceFile)) {
332
            echo "\033[1;31m Config file not exist in module source.\033[0m\n";
333
            throw new Exception('Source file not exist');
334
        }
335
336
        //Alors on copie le fichier vers le dossier /configs/[monModule]/
337
        if (!copy($sourceFile, $targetFile)) {
338
            echo "\033[1;31m Copy fail.\033[0m\n";
339
            throw new Exception('Copy fail');
340
        }
341
342
        echo "\033[1;32m Done\033[0m\n";
343
    }
344
345
    /**
346
     * Run specific module install script if declared
347
     * 
348
     * @return void
349
     */
350
    protected function runInstallScript()
351
    {
352
        echo ' >> Run install specific script : ';
353
354
        //If no script to complete the install
355
        if ($this->installScript === '' || $this->installScript === false) {
356
            echo ' >> '
357
                ."\033[1;33m"
358
                .'No specific script declared. Pass'
359
                ."\033[0m\n";
360
            
361
            return;
362
        }
363
364
        //If the module ask a install script but not declare the name
365
        //Use the default name
366
        if ($this->installScript === true) {
367
            $this->installScript = 'runInstallModule.php';
368
        }
369
370
        //Include the file for complete the install
371
        require_once($this->pathToModule.'/'.$this->installScript);
372
    }
373
}
374