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 ( 7c2f4b...3ca115 )
by Vermeulen
02:17
created

Application::getMemcached()   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 0
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
     * Init output buffering
71
     * Declare run steps
72
     * Set UTF-8 header
73
     * 
74
     * protected for Singleton pattern
75
     */
76
    protected function __construct()
77
    {
78
        //Start the output buffering
79
        ob_start();
80
81
        $this->declareRunPhases();
82
83
        //Defaut http header. Define here add possiblity to override him
84
        header('Content-Type: text/html; charset=utf-8');
85
    }
86
87
    /**
88
     * get the Application instance (Singleton pattern)
89
     * 
90
     * @param array $options Options passed to application
91
     * 
92
     * @return \BFW\Application The current instance of this class
93
     */
94
    public static function getInstance($options = [])
95
    {
96
        if (self::$instance === null) {
97
            $calledClass = get_called_class(); //Autorize extends this class
98
            self::$instance = new $calledClass;
99
            self::$instance->initSystem($options);
100
        }
101
102
        return self::$instance;
103
    }
104
105
    /**
106
     * Like getInstance. This is to have a keyword easier for users who want
107
     * initialize the application
108
     * 
109
     * @param array $options Options passed to application
110
     * 
111
     * @return \BFW\Application The current instance of this class
112
     */
113
    public static function init($options = [])
114
    {
115
        $calledClass = get_called_class(); //Autorize extends this class
116
        return $calledClass::getInstance($options);
117
    }
118
119
    /**
120
     * Getter to access to composerLoader attribut
121
     * 
122
     * @return \Composer\Autoload\ClassLoader The composer class loader
123
     */
124
    public function getComposerLoader()
125
    {
126
        return $this->composerLoader;
127
    }
128
129
    /**
130
     * Getter to access to a BFW config value
131
     * 
132
     * @param string $configKey The key in the config file
133
     * 
134
     * @return mixed
135
     */
136
    public function getConfig($configKey)
137
    {
138
        return $this->config->getConfig($configKey);
139
    }
140
    
141
    /**
142
     * Getter to access to memcache instance
143
     * 
144
     * @return Object|null
145
     */
146
    public function getMemcached()
147
    {
148
        return $this->memcached;
149
    }
150
    
151
    /**
152
     * Getter to access to a module
153
     * 
154
     * @param string $moduleName The module name to access
155
     * 
156
     * @return \BFW\Module
157
     */
158
    public function getModule($moduleName)
159
    {
160
        return $this->modules->getModule($moduleName);
161
    }
162
163
    /**
164
     * Getter to access to a BFW option value
165
     * 
166
     * @param string $optionKey The key for the option
167
     * 
168
     * @return mixed
169
     */
170
    public function getOption($optionKey)
171
    {
172
        return $this->options->getOption($optionKey);
173
    }
174
    
175
    /**
176
     * Getter to access to Request instance
177
     * 
178
     * @return \BFW\Request
179
     */
180
    public function getRequest()
181
    {
182
        return $this->request;
183
    }
184
    
185
    /**
186
     * Initialize all components
187
     * 
188
     * @param array $options Options passed to application
189
     * 
190
     * @return void
191
     */
192
    protected function initSystem($options)
193
    {
194
        $this->initOptions($options);
195
        $this->initConstants();
196
        $this->initComposerLoader();
197
        $this->initConfig();
198
        $this->initRequest();
199
        $this->initSession();
200
        $this->initErrors();
201
        $this->initModules();
202
    }
203
204
    /**
205
     * Initialize attribute options with the class \BFW\Core\Options
206
     * 
207
     * @param array $options The option passed when initialize this class
208
     */
209
    protected function initOptions($options)
210
    {
211
        $defaultOptions = [
212
            'rootDir'    => null,
213
            'vendorDir'  => null,
214
            'runSession' => true
215
        ];
216
217
        $this->options = new \BFW\Core\Options($defaultOptions, $options);
218
    }
219
220
    /**
221
     * Initialize all constants used by framework
222
     * Use helper Constants::create to allow override of constants
223
     * 
224
     * @return void
225
     */
226
    protected function initConstants()
227
    {
228
        Constants::create('ROOT_DIR', $this->options->getOption('rootDir'));
229
230
        Constants::create('APP_DIR', ROOT_DIR.'app/');
231
        Constants::create('SRC_DIR', ROOT_DIR.'src/');
232
        Constants::create('WEB_DIR', ROOT_DIR.'web/');
233
234
        Constants::create('CONFIG_DIR', APP_DIR.'config/');
235
        Constants::create('MODULES_DIR', APP_DIR.'modules/');
236
237
        Constants::create('CLI_DIR', SRC_DIR.'cli/');
238
        Constants::create('CTRL_DIR', SRC_DIR.'controllers/');
239
        Constants::create('MODELES_DIR', SRC_DIR.'modeles/');
240
        Constants::create('VIEW_DIR', SRC_DIR.'view/');
241
    }
242
243
    /**
244
     * Initialize composer loader
245
     * Get composerLoader instance
246
     * Call addComposerNamespaces method to add Application namespace
247
     * 
248
     * @return void
249
     */
250
    protected function initComposerLoader()
251
    {
252
        $this->composerLoader = require($this->options->getOption('vendorDir').'autoload.php');
253
        $this->addComposerNamespaces();
254
    }
255
256
    /**
257
     * Initialize attribute config with \BFW\Config instance
258
     * The config class will search all file in "bfw" directory and load files
259
     * 
260
     * @return void
261
     */
262
    protected function initConfig()
263
    {
264
        $this->config = new \BFW\Config('bfw');
265
        $this->config->loadFiles();
266
    }
267
268
    /**
269
     * Initialize request attribute with \BFW\Request class
270
     * 
271
     * @return void
272
     */
273
    protected function initRequest()
274
    {
275
        $this->request = \BFW\Request::getInstance();
276
    }
277
278
    /**
279
     * Initiliaze php session if option "runSession" is not (bool) false
280
     * 
281
     * @return void
282
     */
283
    protected function initSession()
284
    {
285
        if ($this->options->getOption('runSession') === false) {
286
            return;
287
        }
288
289
        //Destroy session cookie if browser quit
290
        session_set_cookie_params(0);
291
292
        //Run session
293
        session_start();
294
    }
295
296
    /**
297
     * Initialize errors attribute with \BFW\Core\Errors class
298
     * 
299
     * @return void
300
     */
301
    protected function initErrors()
302
    {
303
        $this->errors = new \BFW\Core\Errors();
304
    }
305
306
    /**
307
     * Initialize modules attribut with \BFW\Modules class
308
     * 
309
     * @return void
310
     */
311
    protected function initModules()
312
    {
313
        $this->modules = new \BFW\Modules;
314
    }
315
316
    /**
317
     * Add namespace used by a BFW Application to composer
318
     * 
319
     * @return void
320
     */
321
    protected function addComposerNamespaces()
322
    {
323
        $this->composerLoader->addPsr4('Controller\\', CTRL_DIR);
324
        $this->composerLoader->addPsr4('Modules\\', MODULES_DIR);
325
        $this->composerLoader->addPsr4('Modeles\\', MODELES_DIR);
326
    }
327
328
    /**
329
     * Declare all step to run application
330
     * 
331
     * @return void
332
     */
333
    protected function declareRunPhases()
334
    {
335
        $this->runPhases = [
336
            [$this, 'loadMemcached'],
337
            [$this, 'readAllModules'],
338
            [$this, 'loadAllCoreModules'],
339
            [$this, 'loadAllAppModules'],
340
            [$this, 'runCliFile']
341
        ];
342
    }
343
344
    /**
345
     * Run the application
346
     * 
347
     * @return void
348
     */
349 View Code Duplication
    public function run()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
350
    {
351
        foreach ($this->runPhases as $action) {
352
            $action();
353
354
            $notifyAction = $action;
355
            if (is_array($action)) {
356
                $notifyAction = $action[1];
357
            }
358
359
            $this->notifyAction('apprun_'.$notifyAction);
360
        }
361
362
        $this->notifyAction('bfw_run_finish');
363
    }
364
365
    /**
366
     * Connect to memcache(d) server with the class declared in config file
367
     * 
368
     * @return Object
369
     * 
370
     * @throws Exception If memcached is enabled but no class is define. Or if
371
     *  The class declared in config is not found.
372
     */
373
    protected function loadMemcached()
374
    {
375
        $memcacheConfig = $this->getConfig('memcached');
376
377
        if ($memcacheConfig['enabled'] === false) {
378
            return;
379
        }
380
381
        $class = $memcacheConfig['class'];
382
        if (empty($class)) {
383
            throw new Exception('Memcached is active but no class is define');
384
        }
385
386
        if (class_exists($class) === false) {
387
            throw new Exception('Memcache class '.$class.' not found.');
388
        }
389
390
        $this->memcached = new $class;
391
    }
392
393
    /**
394
     * Read all directory in modules directory and add the module to Modules
395
     * class.
396
     * Generate the load tree.
397
     * Not initialize modules !
398
     * 
399
     * @return void
400
     */
401
    protected function readAllModules()
402
    {
403
        $listModules = array_diff(scandir(MODULES_DIR), ['.', '..']);
404
405
        foreach ($listModules as $moduleName) {
406
            $modulePath = realpath(MODULES_DIR.$moduleName); //Symlink
407
408
            if (!is_dir($modulePath)) {
409
                continue;
410
            }
411
412
            $this->modules->addModule($moduleName);
413
        }
414
415
        $this->modules->readNeedMeDependencies();
416
        $this->modules->generateTree();
417
    }
418
419
    /**
420
     * Load modules define in config bfw file.
421
     * It's module for controller, router, database and template only.
422
     * 
423
     * @return void
424
     */
425
    protected function loadAllCoreModules()
426
    {
427
        foreach ($this->getConfig('modules') as $moduleInfos) {
428
            $moduleName    = $moduleInfos['name'];
429
            $moduleEnabled = $moduleInfos['enabled'];
430
431
            if (empty($moduleName) || $moduleEnabled === false) {
432
                continue;
433
            }
434
435
            $this->loadModule($moduleName);
436
        }
437
    }
438
439
    /**
440
     * Load all modules (except core).
441
     * Get the load tree, read this and load modules in order declared in tree.
442
     * 
443
     * @return void
444
     */
445
    protected function loadAllAppModules()
446
    {
447
        $tree = $this->modules->getLoadTree();
448
449
        foreach ($tree as $firstLine) {
450
            foreach ($firstLine as $secondLine) {
451
                foreach ($secondLine as $moduleName) {
452
                    $this->loadModule($moduleName);
453
                }
454
            }
455
        }
456
    }
457
458
    /**
459
     * Load a module
460
     * 
461
     * @param string $moduleName The module's name to load
462
     * 
463
     * @return void
464
     */
465
    protected function loadModule($moduleName)
466
    {
467
        $this->notifyAction('load_module_'.$moduleName);
468
        $this->modules->getModule($moduleName)->runModule();
469
    }
470
471
    /**
472
     * Run the cli file if we're in cli mode
473
     * 
474
     * @return void
475
     * 
476
     * @throws Exception If no file is specified or if the file not exist.
477
     */
478
    protected function runCliFile()
479
    {
480
        if (PHP_SAPI !== 'cli') {
481
            return;
482
        }
483
484
        $opt = getopt('f:');
485
        if (!isset($opt['f'])) {
486
            throw new Exception('Error: No file specified.');
487
        }
488
489
        $file = $opt['f'];
490
        if (!file_exists(CLI_DIR.$file.'.php')) {
491
            throw new Exception('File to execute not found.');
492
        }
493
494
        $fctExecuteFile = function() use ($file) {
495
            require_once(CLI_DIR.$file.'.php');
496
        };
497
498
        $this->notifyAction('run_cli_file');
499
        $fctExecuteFile();
500
    }
501
}
502