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 ( 8e5a12...84c455 )
by Vermeulen
03:45
created

Application::getModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace BFW;
4
5
use \Exception;
6
use \BFW\Helpers\Constants;
7
8
/**
9
 * Application class
10
 * Manage all BFW application
11
 * Load and init components, modules, ...
12
 */
13
class Application extends Subjects
14
{
15
    /**
16
     * @var \BFW\Application|null $instance Application instance (Singleton)
17
     */
18
    protected static $instance = null;
19
20
    /**
21
     * @var string $rootDir Path to application project directory
22
     */
23
    protected $rootDir = '';
24
25
    /**
26
     * @var \BFW\Config $config Config loaded for BFW application
27
     */
28
    protected $config;
29
30
    /**
31
     * @var \BFW\Core\Options $options Option passed to this instance
32
     */
33
    protected $options;
34
35
    /**
36
     * @var \Composer\Autoload\ClassLoader $composerLoader loader used by
37
     *  composer.
38
     */
39
    protected $composerLoader;
40
41
    /**
42
     *
43
     * @var array[] $runPhases All steps to init Application
44
     */
45
    protected $runPhases = [];
46
47
    /**
48
     * @var Object $memcached The class used to connect to memcache(d) server.
49
     * The class used should be declared on config
50
     */
51
    protected $memcached;
52
53
    /**
54
     * @var \BFW\Request $request Informations about the http request
55
     */
56
    protected $request;
57
58
    /**
59
     * @var \BFW\Modules $modules System who manage all modules
60
     */
61
    protected $modules;
62
    
63
    /**
64
     * @var \BFW\Core\Errors $errors System who manage personnel errors page
65
     */
66
    protected $errors;
67
68
    /**
69
     * Constructor
70
     * Initialize all components
71
     * 
72
     * protected for Singleton pattern
73
     * 
74
     * @param array $options Options passed to application
75
     */
76
    protected function __construct($options)
77
    {
78
        //Start the output buffering
79
        ob_start();
80
81
        $this->initOptions($options);
82
        $this->initConstants();
83
        $this->initComposerLoader();
84
        $this->initConfig();
85
        $this->initRequest();
86
        $this->initSession();
87
        $this->initErrors();
88
        $this->initModules();
89
90
        $this->declareRunPhases();
91
92
        //Defaut http header. Define here add possiblity to override him
93
        header('Content-Type: text/html; charset=utf-8');
94
    }
95
96
    /**
97
     * get the Application instance (Singleton pattern)
98
     * 
99
     * @param array $options Options passed to application
100
     * 
101
     * @return \BFW\Application The current instance of this class
102
     */
103
    public static function getInstance($options = [])
104
    {
105
        if (self::$instance === null) {
106
            $calledClass = get_called_class(); //Autorize extends this class
107
            
108
            self::$instance = new $calledClass($options);
109
        }
110
111
        return self::$instance;
112
    }
113
114
    /**
115
     * Like getInstance. This is to have a keyword easier for users who want
116
     * initialize the application
117
     * 
118
     * @param array $options Options passed to application
119
     * 
120
     * @return \BFW\Application The current instance of this class
121
     */
122
    public static function init($options = [])
123
    {
124
        return self::getInstance($options);
125
    }
126
127
    /**
128
     * Getter to access to composerLoader attribut
129
     * 
130
     * @return \Composer\Autoload\ClassLoader The composer class loader
131
     */
132
    public function getComposerLoader()
133
    {
134
        return $this->composerLoader;
135
    }
136
137
    /**
138
     * Getter to access to a BFW config value
139
     * 
140
     * @param string $configKey The key in the config file
141
     * 
142
     * @return mixed
143
     */
144
    public function getConfig($configKey)
145
    {
146
        return $this->config->getConfig($configKey);
147
    }
148
    
149
    /**
150
     * Getter to access to a module
151
     * 
152
     * @param string $moduleName The module name to access
153
     * 
154
     * @return \BFW\Module
155
     */
156
    public function getModule($moduleName)
157
    {
158
        return $this->modules->getModule($moduleName);
159
    }
160
161
    /**
162
     * Getter to access to a BFW option value
163
     * 
164
     * @param string $optionKey The key for the option
165
     * 
166
     * @return mixed
167
     */
168
    public function getOption($optionKey)
169
    {
170
        return $this->options->getOption($optionKey);
171
    }
172
    
173
    /**
174
     * Getter to access to Request instance
175
     * 
176
     * @return \BFW\Request
177
     */
178
    public function getRequest()
179
    {
180
        return $this->request;
181
    }
182
183
    /**
184
     * Initialize attribute options with the class \BFW\Core\Options
185
     * 
186
     * @param array $options The option passed when initialize this class
187
     */
188
    protected function initOptions($options)
189
    {
190
        $defaultOptions = [
191
            'rootDir'    => null,
192
            'vendorDir'  => null,
193
            'runSession' => true
194
        ];
195
196
        $this->options = new \BFW\Core\Options($defaultOptions, $options);
197
    }
198
199
    /**
200
     * Initialize all constants used by framework
201
     * Use helper Constants::create to allow override of constants
202
     * 
203
     * @return void
204
     */
205
    protected function initConstants()
206
    {
207
        Constants::create('ROOT_DIR', $this->options->getOption('rootDir'));
208
209
        Constants::create('APP_DIR', ROOT_DIR.'app/');
210
        Constants::create('SRC_DIR', ROOT_DIR.'src/');
211
        Constants::create('WEB_DIR', ROOT_DIR.'web/');
212
213
        Constants::create('CONFIG_DIR', APP_DIR.'config/');
214
        Constants::create('MODULES_DIR', APP_DIR.'modules/');
215
216
        Constants::create('CLI_DIR', SRC_DIR.'cli/');
217
        Constants::create('CTRL_DIR', SRC_DIR.'controllers/');
218
        Constants::create('MODELES_DIR', SRC_DIR.'modeles/');
219
        Constants::create('VIEW_DIR', SRC_DIR.'view/');
220
    }
221
222
    /**
223
     * Initialize composer loader
224
     * Get composerLoader instance
225
     * Call addComposerNamespaces method to add Application namespace
226
     * 
227
     * @return void
228
     */
229
    protected function initComposerLoader()
230
    {
231
        $this->composerLoader = require($this->options->getOption('vendorDir').'autoload.php');
232
        $this->addComposerNamespaces();
233
    }
234
235
    /**
236
     * Initialize attribute config with \BFW\Config instance
237
     * The config class will search all file in "bfw" directory and load files
238
     * 
239
     * @return void
240
     */
241
    protected function initConfig()
242
    {
243
        $this->config = new \BFW\Config('bfw');
244
        $this->config->loadFiles();
245
    }
246
247
    /**
248
     * Initialize request attribute with \BFW\Request class
249
     * 
250
     * @return void
251
     */
252
    protected function initRequest()
253
    {
254
        $this->request = \BFW\Request::getInstance();
255
    }
256
257
    /**
258
     * Initiliaze php session if option "runSession" is not (bool) false
259
     * 
260
     * @return void
261
     */
262
    protected function initSession()
263
    {
264
        if ($this->options->getOption('runSession') === false) {
265
            return;
266
        }
267
268
        //Destroy session cookie if browser quit
269
        session_set_cookie_params(0);
270
271
        //Run session
272
        session_start();
273
    }
274
275
    /**
276
     * Initialize errors attribute with \BFW\Core\Errors class
277
     * 
278
     * @return void
279
     */
280
    protected function initErrors()
281
    {
282
        $this->errors = new \BFW\Core\Errors($this);
283
    }
284
285
    /**
286
     * Initialize modules attribut with \BFW\Modules class
287
     * 
288
     * @return void
289
     */
290
    protected function initModules()
291
    {
292
        $this->modules = new \BFW\Modules;
293
    }
294
295
    /**
296
     * Add namespace used by a BFW Application to composer
297
     * 
298
     * @return void
299
     */
300
    protected function addComposerNamespaces()
301
    {
302
        $this->composerLoader->addPsr4('Controller\\', CTRL_DIR);
303
        $this->composerLoader->addPsr4('Modules\\', MODULES_DIR);
304
        $this->composerLoader->addPsr4('Modeles\\', MODELES_DIR);
305
    }
306
307
    /**
308
     * Declare all step to run application
309
     * 
310
     * @return void
311
     */
312
    protected function declareRunPhases()
313
    {
314
        $this->runPhases = [
315
            [$this, 'loadMemcached'],
316
            [$this, 'readAllModules'],
317
            [$this, 'loadAllCoreModules'],
318
            [$this, 'loadAllAppModules'],
319
            [$this, 'runCliFile']
320
        ];
321
    }
322
323
    /**
324
     * Run the application
325
     * 
326
     * @return void
327
     */
328
    public function run()
329
    {
330
        foreach ($this->runPhases as $action) {
331
            $action();
332
333
            $notifyAction = $action;
334
            if (is_array($action)) {
335
                $notifyAction = $action[1];
336
            }
337
338
            $this->notifyAction('apprun_'.$notifyAction);
339
        }
340
341
        $this->notifyAction('bfw_run_finish');
342
    }
343
344
    /**
345
     * Connect to memcache(d) server with the class declared in config file
346
     * 
347
     * @return Object
348
     * 
349
     * @throws Exception If memcached is enabled but no class is define. Or if
350
     *  The class declared in config is not found.
351
     */
352
    protected function loadMemcached()
353
    {
354
        $memcacheConfig = $this->getConfig('memcached');
355
356
        if ($memcacheConfig['enabled'] === false) {
357
            return;
358
        }
359
360
        $class = $memcacheConfig['class'];
361
        if (empty($class)) {
362
            throw new Exception('Memcached is active but no class is define');
363
        }
364
365
        if (class_exists($class) === false) {
366
            throw new Exception('Memcache class '.$class.' not found.');
367
        }
368
369
        $this->memcached = new $class($this);
370
    }
371
372
    /**
373
     * Read all directory in modules directory and add the module to Modules
374
     * class.
375
     * Generate the load tree.
376
     * Not initialize modules !
377
     * 
378
     * @return void
379
     */
380
    protected function readAllModules()
381
    {
382
        $listModules = array_diff(scandir(MODULES_DIR), ['.', '..']);
383
384
        foreach ($listModules as $moduleName) {
385
            $modulePath = realpath(MODULES_DIR.$moduleName); //Symlink
386
387
            if (!is_dir($modulePath)) {
388
                continue;
389
            }
390
391
            $this->modules->addModule($moduleName);
392
        }
393
394
        $this->modules->generateTree();
395
    }
396
397
    /**
398
     * Load modules define in config bfw file.
399
     * It's module for controller, router, database and template only.
400
     * 
401
     * @return void
402
     */
403
    protected function loadAllCoreModules()
404
    {
405
        foreach ($this->getConfig('modules') as $moduleInfos) {
406
            $moduleName    = $moduleInfos['name'];
407
            $moduleEnabled = $moduleInfos['enabled'];
408
409
            if (empty($moduleName) || $moduleEnabled === false) {
410
                continue;
411
            }
412
413
            $this->loadModule($moduleName);
414
        }
415
    }
416
417
    /**
418
     * Load all modules (except core).
419
     * Get the load tree, read this and load modules in order declared in tree.
420
     * 
421
     * @return void
422
     */
423
    protected function loadAllAppModules()
424
    {
425
        $tree = $this->modules->getLoadTree();
426
427
        foreach ($tree as $firstLine) {
428
            foreach ($firstLine as $secondLine) {
429
                foreach ($secondLine as $moduleName) {
430
                    $this->loadModule($moduleName);
431
                }
432
            }
433
        }
434
    }
435
436
    /**
437
     * Load a module
438
     * 
439
     * @param string $moduleName The module's name to load
440
     * 
441
     * @return void
442
     */
443
    protected function loadModule($moduleName)
444
    {
445
        $this->notifyAction('load_module_'.$moduleName);
446
        $this->modules->getModule($moduleName)->runModule();
447
    }
448
449
    /**
450
     * Run the cli file if we're in cli mode
451
     * 
452
     * @return void
453
     * 
454
     * @throws Exception If no file is specified or if the file not exist.
455
     */
456
    protected function runCliFile()
457
    {
458
        if (PHP_SAPI !== 'cli') {
459
            return;
460
        }
461
462
        $opt = getopt('f:');
463
        if (!isset($opt['f'])) {
464
            throw new Exception('Error: No file specified.');
465
        }
466
467
        $file = $opt['f'];
468
        if (!file_exists(CLI_DIR.$file.'.php')) {
469
            throw new Exception('File to execute not found.');
470
        }
471
472
        $fctExecuteFile = function() use ($file) {
473
            require_once(CLI_DIR.$file.'.php');
474
        };
475
476
        $this->notifyAction('run_cli_file');
477
        $fctExecuteFile();
478
    }
479
}
480