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 ( 572920...bfe862 )
by Vermeulen
02:24
created

Application::declareRunSteps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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