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.
Passed
Push — master ( a1835d...10e84a )
by
unknown
02:48
created

Framework::__construct()   F

Complexity

Conditions 14
Paths 4608

Size

Total Lines 84
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 41
nc 4608
nop 0
dl 0
loc 84
rs 2.1
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System;
15
16
// ------------------------------------------------------------------------
17
18
/*
19
 * ---------------------------------------------------------------
20
 * ERROR REPORTING
21
 * ---------------------------------------------------------------
22
 *
23
 * Different environments will require different levels of error reporting.
24
 * By default development will show errors but testing and live will hide them.
25
 */
26
switch (strtoupper(ENVIRONMENT)) {
0 ignored issues
show
Bug introduced by
The constant O2System\ENVIRONMENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
27
    case 'DEVELOPMENT':
28
        error_reporting(-1);
29
        ini_set('display_errors', 1);
30
        break;
31
    case 'TESTING':
32
    case 'PRODUCTION':
33
        ini_set('display_errors', 0);
34
        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
35
        break;
36
    default:
37
        header('HTTP/1.1 503 Service Unavailable.', true, 503);
38
        echo 'The application environment is not set correctly.';
39
        exit(1); // EXIT_ERROR
40
}
41
42
/*
43
 *---------------------------------------------------------------
44
 * VENDOR PATH
45
 *---------------------------------------------------------------
46
 *
47
 * RealPath to vendor folder.
48
 *
49
 * WITH TRAILING SLASH!
50
 */
51
if ( ! defined('PATH_VENDOR')) {
52
    define('PATH_VENDOR', PATH_ROOT . 'vendor' . DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
The constant O2System\PATH_ROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
53
}
54
55
/*
56
 *---------------------------------------------------------------
57
 * FRAMEWORK PATH
58
 *---------------------------------------------------------------
59
 *
60
 * RealPath to framework folder.
61
 *
62
 * WITH TRAILING SLASH!
63
 */
64
if ( ! defined('PATH_FRAMEWORK')) {
65
    define('PATH_FRAMEWORK', __DIR__ . DIRECTORY_SEPARATOR);
66
}
67
68
/*
69
 *---------------------------------------------------------------
70
 * APP PATH
71
 *---------------------------------------------------------------
72
 *
73
 * RealPath to application folder.
74
 *
75
 * WITH TRAILING SLASH!
76
 */
77
if ( ! defined('PATH_APP')) {
78
    define('PATH_APP', PATH_ROOT . DIR_APP . DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
The constant O2System\DIR_APP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
79
}
80
81
/*
82
 *---------------------------------------------------------------
83
 * PUBLIC PATH
84
 *---------------------------------------------------------------
85
 *
86
 * RealPath to public folder.
87
 *
88
 * WITH TRAILING SLASH!
89
 */
90
if ( ! defined('PATH_PUBLIC')) {
91
    define('PATH_PUBLIC', PATH_ROOT . DIR_PUBLIC . DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
The constant O2System\DIR_PUBLIC was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
92
}
93
94
/*
95
 *---------------------------------------------------------------
96
 * CACHE PATH
97
 *---------------------------------------------------------------
98
 *
99
 * RealPath to writable caching folder.
100
 *
101
 * WITH TRAILING SLASH!
102
 */
103
if ( ! defined('PATH_CACHE')) {
104
    define('PATH_CACHE', PATH_ROOT . DIR_CACHE . DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
The constant O2System\DIR_CACHE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
105
}
106
107
/*
108
 *---------------------------------------------------------------
109
 * STORAGE PATH
110
 *---------------------------------------------------------------
111
 *
112
 * RealPath to writable storage folder.
113
 *
114
 * WITH TRAILING SLASH!
115
 */
116
if ( ! defined('PATH_STORAGE')) {
117
    define('PATH_STORAGE', PATH_ROOT . DIR_STORAGE . DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
The constant O2System\DIR_STORAGE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
118
}
119
120
/*
121
 *---------------------------------------------------------------
122
 * RESOURCES PATH
123
 *---------------------------------------------------------------
124
 *
125
 * RealPath to writable resources folder.
126
 *
127
 * WITH TRAILING SLASH!
128
 */
129
if ( ! defined('PATH_RESOURCES')) {
130
    define('PATH_RESOURCES', PATH_ROOT . DIR_RESOURCES . DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
The constant O2System\DIR_RESOURCES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
131
}
132
133
134
/*
135
 *---------------------------------------------------------------
136
 * FRAMEWORK CONSTANTS
137
 *---------------------------------------------------------------
138
 */
139
require __DIR__ . '/Config/Constants.php';
140
141
/*
142
 *---------------------------------------------------------------
143
 * FRAMEWORK HELPERS
144
 *---------------------------------------------------------------
145
 */
146
require __DIR__ . '/Helpers/Framework.php';
147
148
/**
149
 * Class Framework
150
 *
151
 * @package O2System
152
 */
153
class Framework extends Kernel
154
{
155
    /**
156
     * Framework::$config
157
     *
158
     * Framework Container Config
159
     *
160
     * @var Framework\Containers\Config
161
     */
162
    public $config;
163
164
    /**
165
     * Framework::$globals
166
     *
167
     * Framework Container Globals
168
     *
169
     * @var Framework\Containers\Globals
170
     */
171
    public $globals;
172
173
    /**
174
     * Framework::$environment
175
     *
176
     * Framework Container Environment
177
     *
178
     * @var Framework\Containers\Environment
179
     */
180
    public $environment;
181
182
    /**
183
     * Framework::$models
184
     *
185
     * Framework Container Models
186
     *
187
     * @var Framework\Containers\Models
188
     */
189
    public $models;
190
191
    /**
192
     * Framework::$modules
193
     *
194
     * Framework Container Modules
195
     *
196
     * @var Framework\Containers\Modules
197
     */
198
    public $modules;
199
200
    // ------------------------------------------------------------------------
201
202
    /**
203
     * Framework::__construct
204
     */
205
    protected function __construct()
206
    {
207
        parent::__construct();
208
209
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
210
            profiler()->watch('Starting O2System Framework');
211
        }
212
213
        // Add App Views Folder
214
        output()->addFilePath(PATH_APP);
215
216
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
217
            profiler()->watch('Starting Framework Services');
218
        }
219
220
        $services = [
221
            'Services\Hooks'    => 'hooks',
222
            'Services\Shutdown' => 'shutdown',
223
            'Services\Logger'   => 'logger',
224
            'Services\Loader'   => 'loader',
225
        ];
226
227
        foreach ($services as $className => $classOffset) {
228
            $this->services->load($className, $classOffset);
229
        }
230
231
        // Instantiate Config Container
232
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
233
            profiler()->watch('Starting Config Container');
234
        }
235
236
        $this->config = new Framework\Containers\Config();
237
238
        // Instantiate Globals Container
239
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
240
            profiler()->watch('Starting Globals Container');
241
        }
242
        $this->globals = new Framework\Containers\Globals();
243
244
        // Instantiate Environment Container
245
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
246
            profiler()->watch('Starting Environment Container');
247
        }
248
        $this->environment = new Framework\Containers\Environment();
249
250
        // Instantiate Models Container
251
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
252
            profiler()->watch('Starting Models Container');
253
        }
254
255
        $this->models = new Framework\Containers\Models();
256
257
        // Instantiate Modules Container
258
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
259
            profiler()->watch('Starting Modules Container');
260
        }
261
        $this->modules = new Framework\Containers\Modules();
262
263
        if (config()->loadFile('cache') === true) {
0 ignored issues
show
Bug introduced by
The method loadFile() does not exist on O2System\Kernel\Datastructures\Config. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

263
        if (config()->/** @scrutinizer ignore-call */ loadFile('cache') === true) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
264
            // Instantiate Cache Service
265
            if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
266
                profiler()->watch('Starting Cache Service');
267
            }
268
269
            $this->services->add(new Framework\Services\Cache(config('cache', true)), 'cache');
0 ignored issues
show
Bug introduced by
It seems like config('cache', true) can also be of type O2System\Framework\Containers\Config; however, parameter $config of O2System\Framework\Services\Cache::__construct() does only seem to accept O2System\Cache\Datastructures\Config, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

269
            $this->services->add(new Framework\Services\Cache(/** @scrutinizer ignore-type */ config('cache', true)), 'cache');
Loading history...
270
271
            // Language Service Load Registry
272
            if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
273
                profiler()->watch('Loading Language Registry');
274
            }
275
276
            language()->loadRegistry();
0 ignored issues
show
introduced by
The method loadRegistry() does not exist on O2System\Kernel\Services\Language. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

276
            language()->/** @scrutinizer ignore-call */ loadRegistry();
Loading history...
277
278
            // Modules Service Load Registry
279
            if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
280
                profiler()->watch('Loading Modules Registry');
281
            }
282
            $this->modules->loadRegistry();
283
        }
284
285
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
286
            profiler()->watch('Starting O2System Framework Hooks Pre System');
287
        }
288
        hooks()->callEvent(Framework\Services\Hooks::PRE_SYSTEM);
289
    }
290
291
    // ------------------------------------------------------------------------
292
293
    /**
294
     * Framework::__reconstruct
295
     */
296
    protected function __reconstruct()
297
    {
298
        // Modules default app
299
        if (null !== ($defaultApp = config('app'))) {
0 ignored issues
show
introduced by
The condition null !== $defaultApp = config('app') is always true.
Loading history...
300
            if (false !== ($defaultModule = modules()->getApp($defaultApp))) {
0 ignored issues
show
Bug introduced by
The method getApp() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

300
            if (false !== ($defaultModule = modules()->/** @scrutinizer ignore-call */ getApp($defaultApp))) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like $defaultApp can also be of type O2System\Framework\Containers\Config; however, parameter $segment of O2System\Framework\Containers\Modules::getApp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

300
            if (false !== ($defaultModule = modules()->getApp(/** @scrutinizer ignore-type */ $defaultApp))) {
Loading history...
301
                // Register Domain App Module Namespace
302
                loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath());
303
304
                // Push Domain App Module
305
                modules()->push($defaultModule);
0 ignored issues
show
Bug introduced by
The method push() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

305
                modules()->/** @scrutinizer ignore-call */ push($defaultModule);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
306
            } elseif (false !== ($defaultModule = modules()->getModule($defaultApp))) {
0 ignored issues
show
Bug introduced by
The method getModule() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

306
            } elseif (false !== ($defaultModule = modules()->/** @scrutinizer ignore-call */ getModule($defaultApp))) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like $defaultApp can also be of type O2System\Framework\Containers\Config; however, parameter $segments of O2System\Framework\Containers\Modules::getModule() does only seem to accept array|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

306
            } elseif (false !== ($defaultModule = modules()->getModule(/** @scrutinizer ignore-type */ $defaultApp))) {
Loading history...
307
                // Register Path Module Namespace
308
                loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath());
309
310
                // Push Path Module
311
                modules()->push($defaultModule);
312
            }
313
        }
314
315
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
316
            profiler()->watch('Calling Hooks Service: Post System');
317
        }
318
        hooks()->callEvent(Framework\Services\Hooks::POST_SYSTEM);
319
320
        if (is_cli()) {
321
            $this->cliHandler();
322
        } else {
323
            $this->httpHandler();
324
        }
325
    }
326
327
    // ------------------------------------------------------------------------
328
329
    /**
330
     * Framework::cliHandler
331
     *
332
     * @return void
333
     * @throws \ReflectionException
334
     */
335
    private function cliHandler()
336
    {
337
        // Instantiate CLI Router Service
338
        $this->services->load(Kernel\Cli\Router::class);
339
340
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
341
            profiler()->watch('Parse Router Request');
342
        }
343
        router()->parseRequest();
344
345
        if ($commander = router()->getCommander()) {
0 ignored issues
show
Bug introduced by
The method getCommander() does not exist on O2System\Framework\Http\Router. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

345
        if ($commander = router()->/** @scrutinizer ignore-call */ getCommander()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
346
            if ($commander instanceof Kernel\Cli\Router\DataStructures\Commander) {
0 ignored issues
show
introduced by
$commander is always a sub-type of O2System\Kernel\Cli\Rout...ataStructures\Commander.
Loading history...
347
                // Autoload Language
348
                language()->loadFile($commander->getParameter());
349
                language()->loadFile($commander->getRequestMethod());
350
                language()->loadFile($commander->getParameter() . '/' . $commander->getRequestMethod());
351
352
                // Autoload Model
353
                foreach ($this->modules as $module) {
354
                    if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) {
355
                        continue;
356
                    }
357
                    $module->loadModel();
358
                }
359
360
                // Autoload Model
361
                $modelClassName = str_replace('Commanders', 'Models', $commander->getName());
362
363
                if (class_exists($modelClassName)) {
364
                    $this->models->load($modelClassName, 'commander');
365
                }
366
367
                // Initialize Controller
368
                if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
369
                    profiler()->watch('Calling Hooks Service: Pre Commander');
370
                }
371
                hooks()->callEvent(Framework\Services\Hooks::PRE_COMMANDER);
372
373
                if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
374
                    profiler()->watch('Instantiating Requested Commander: ' . $commander->getClass());
375
                }
376
                $requestCommander = $commander->getInstance();
377
378
                if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
379
                    profiler()->watch('Calling Hooks Service: Post Commander');
380
                }
381
                hooks()->callEvent(Framework\Services\Hooks::POST_COMMANDER);
382
383
                if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
384
                    profiler()->watch('Execute Requested Commander: ' . $commander->getClass());
385
                }
386
                $requestCommander->execute();
387
388
                exit(EXIT_SUCCESS);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
389
            }
390
        }
391
    }
392
393
    // ------------------------------------------------------------------------
394
395
    /**
396
     * Framework::httpHandler
397
     *
398
     * @return void
399
     * @throws \ReflectionException
400
     */
401
    private function httpHandler()
402
    {
403
        if (config()->loadFile('session') === true) {
404
405
            // Instantiate Session Service
406
            $session = new Session(config('session', true));
0 ignored issues
show
Bug introduced by
It seems like config('session', true) can also be of type O2System\Framework\Containers\Config; however, parameter $config of O2System\Session::__construct() does only seem to accept O2System\Kernel\Datastructures\Config, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

406
            $session = new Session(/** @scrutinizer ignore-type */ config('session', true));
Loading history...
407
            $session->setLogger($this->services->get('logger'));
408
409
            if ( ! $session->isStarted()) {
410
                $session->start();
411
            }
412
413
            $this->services->add($session, 'session');
414
415
            if ($session->has('language') and $this->services->has('language')) {
416
                language()->setDefault($session->get('language'));
417
            } else {
418
                $session->set('language', language()->getDefault());
419
            }
420
421
            if (config('security')->protection[ 'csrf' ] === true) {
0 ignored issues
show
Bug Best Practice introduced by
The property protection does not exist on O2System\Framework\Containers\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
422
                $csrfProtection = new Security\Protections\Csrf();
423
                $this->services->add($csrfProtection, 'csrfProtection');
424
            }
425
426
            if (config('security')->protection[ 'xss' ] === true) {
427
                $xssProtection = new Security\Protections\Xss();
428
                $this->services->add($xssProtection, 'xssProtection');
429
            }
430
        }
431
432
        if (config()->loadFile('view') === true) {
433
            // Instantiate Http UserAgent Service
434
            $this->services->load(Framework\Http\UserAgent::class);
435
436
            // Instantiate Http View Service
437
            $this->services->load(Framework\Http\Parser::class);
438
439
            // Instantiate Http View Service
440
            $this->services->load(Framework\Http\View::class);
441
442
            // Instantiate Http Presenter Service
443
            $this->services->load(Framework\Http\Presenter::class);
444
445
            // Initialize Http Presenter Service
446
            presenter()->initialize();
447
        }
448
449
        // Instantiate Http Middleware Service
450
        $this->services->load(Framework\Http\Middleware::class);
451
452
        // Instantiate Http Router Service
453
        $this->services->load(Framework\Http\Router::class);
454
455
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
456
            profiler()->watch('Parse Router Request');
457
        }
458
        router()->parseRequest();
459
460
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
461
            profiler()->watch('Running Middleware Service: Pre Controller');
462
        }
463
        middleware()->run();
464
465
        if ($this->services->has('controller')) {
466
            $controller = $this->services->get('controller');
467
468
            $controllerParameter = dash($controller->getParameter());
469
            $controllerRequestMethod = dash($controller->getRequestMethod());
470
471
            // Autoload Language
472
            if ($this->services->has('language')) {
473
                language()->loadFile($controller->getParameter());
474
                language()->loadFile($controller->getRequestMethod());
475
                language()->loadFile($controller->getParameter() . '/' . $controller->getRequestMethod());
476
            }
477
478
            // Autoload Model
479
            foreach ($this->modules as $module) {
480
                if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) {
481
                    continue;
482
                }
483
                $module->loadModel();
484
            }
485
486
            // Autoload Model
487
            $modelClassName = str_replace(['Controllers', 'Presenters'], 'Models', $controller->getName());
488
489
            if (class_exists($modelClassName)) {
490
                $this->models->load($modelClassName, 'controller');
491
            }
492
493
            // Autoload Assets
494
            if ($this->services->has('view')) {
495
                $controllerAssets = [];
496
                $controllerAssetsDirs = [];
497
498
                // Autoload Assets
499
                foreach ($this->modules as $module) {
500
                    if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) {
501
                        continue;
502
                    }
503
504
                    $controllerAssets[] = $module->getParameter();
505
                    $controllerAssetsDirs[] = $module->getParameter();
506
                }
507
508
                $controllerAssets = array_reverse($controllerAssets);
509
                $controllerAssetsDirs = array_reverse($controllerAssetsDirs);
510
511
                $controllerAssets[] = $controllerParameter;
512
                $controllerAssetsDirs[] = $controllerParameter;
513
514
                $controllerAssets[] = $controllerRequestMethod;
515
516
                foreach ($controllerAssetsDirs as $controllerAssetsDir) {
517
                    $controllerAssets[] = $controllerAssetsDir . '/' . $controllerParameter;
518
                    $controllerAssets[] = $controllerAssetsDir . '/' . $controllerRequestMethod;
519
                    $controllerAssets[] = $controllerAssetsDir . '/' . $controllerParameter . '/' . $controllerRequestMethod;
520
                }
521
522
                // Autoload Presenter
523
                $presenterClassName = str_replace('Controllers', 'Presenters', $controller->getName());
524
525
                if (class_exists($presenterClassName)) {
526
                    $presenterClassObject = new $presenterClassName();
527
                    if ($presenterClassObject instanceof Framework\Http\Presenter) {
528
                        $this->services->add($presenterClassObject, 'presenter');
529
                    }
530
                }
531
532
                // Autoload Assets
533
                presenter()->assets->loadCss($controllerAssets);
534
                presenter()->assets->loadJs($controllerAssets);
535
            }
536
537
            // Initialize Controller
538
            if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
539
                profiler()->watch('Calling Hooks Service: Pre Controller');
540
            }
541
542
            hooks()->callEvent(Framework\Services\Hooks::PRE_CONTROLLER);
543
544
            if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
545
                profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass());
546
            }
547
            $requestController = $controller->getInstance();
548
549
            if (method_exists($requestController, '__reconstruct')) {
550
                $requestController->__reconstruct();
551
            }
552
553
            $this->services->add($requestController, 'controller');
554
555
            if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
556
                profiler()->watch('Calling Hooks Service: Post Controller');
557
            }
558
            hooks()->callEvent(Framework\Services\Hooks::POST_CONTROLLER);
559
560
            if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
561
                profiler()->watch('Calling Middleware Service: Post Controller');
562
            }
563
            middleware()->run();
564
565
            $requestMethod = $controller->getRequestMethod();
566
            $requestMethodArgs = $controller->getRequestMethodArgs();
567
568
            // Call the requested controller method
569
            if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
570
                profiler()->watch('Execute Requested Controller Method');
571
            }
572
573
            ob_start();
574
            $requestControllerOutput = $requestController->__call($requestMethod, $requestMethodArgs);
575
576
            if (empty($requestControllerOutput)) {
577
                $requestControllerOutput = ob_get_contents();
578
                ob_end_clean();
579
            }
580
581
            if (is_numeric($requestControllerOutput)) {
582
                output()->sendError($requestControllerOutput);
583
            } elseif (is_bool($requestControllerOutput)) {
584
                if ($requestControllerOutput === true) {
585
                    output()->sendError(200);
586
                } elseif ($requestControllerOutput === false) {
0 ignored issues
show
introduced by
The condition $requestControllerOutput === false is always true.
Loading history...
587
                    output()->sendError(204);
588
                }
589
            } elseif (is_array($requestControllerOutput) or is_object($requestControllerOutput)) {
590
                output()->sendPayload($requestControllerOutput);
0 ignored issues
show
Bug introduced by
It seems like $requestControllerOutput can also be of type object; however, parameter $data of O2System\Kernel\Http\Output::sendPayload() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

590
                output()->sendPayload(/** @scrutinizer ignore-type */ $requestControllerOutput);
Loading history...
Bug introduced by
The method sendPayload() does not exist on O2System\Kernel\Cli\Output. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

590
                output()->/** @scrutinizer ignore-call */ sendPayload($requestControllerOutput);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
591
            } elseif ($requestController instanceof Framework\Http\Controllers\Restful) {
592
                if (empty($requestControllerOutput) or $requestControllerOutput === '') {
593
                    $requestController->sendError(204);
594
                } elseif (is_string($requestControllerOutput)) {
595
                    if (is_json($requestControllerOutput)) {
596
                        output()->setContentType('application/json');
0 ignored issues
show
Bug introduced by
The method setContentType() does not exist on O2System\Kernel\Cli\Output. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

596
                        output()->/** @scrutinizer ignore-call */ setContentType('application/json');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
597
                    } else {
598
                        output()->setContentType('text/plain');
599
                    }
600
601
                    echo $requestControllerOutput;
602
                }
603
            } elseif (is_string($requestControllerOutput)) {
604
                if (is_json($requestControllerOutput)) {
605
                    output()->setContentType('application/json');
606
                } elseif ($this->services->has('view')) {
607
                    if (empty($requestControllerOutput) or $requestControllerOutput === '') {
608
                        $filenames = [
609
                            $controllerRequestMethod,
610
                            $controllerParameter . DIRECTORY_SEPARATOR . $controllerRequestMethod,
611
                        ];
612
613
                        if ($controllerRequestMethod === 'index') {
614
                            array_unshift($filenames, $controllerParameter);
615
                        }
616
617
                        foreach ($filenames as $filename) {
618
                            if (false !== ($filePath = view()->getFilePath($filename))) {
619
                                view()->load($filePath);
620
                                break;
621
                            }
622
                        }
623
                    } else {
624
                        presenter()->partials->offsetSet('content', $requestControllerOutput);
625
                    }
626
627
                    if (presenter()->partials->offsetExists('content')) {
628
                        $htmlOutput = view()->render();
629
630
                        if (empty($htmlOutput)) {
631
                            output()->sendError(204);
632
                        } else {
633
                            output()->setContentType('text/html');
634
                            output()->send($htmlOutput);
0 ignored issues
show
Bug introduced by
The method send() does not exist on O2System\Kernel\Cli\Output. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

634
                            output()->/** @scrutinizer ignore-call */ send($htmlOutput);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
635
                        }
636
                    } else {
637
                        output()->sendError(204);
638
                    }
639
                } elseif (empty($requestControllerOutput) or $requestControllerOutput === '') {
640
                    output()->sendError(204);
641
                } else {
642
                    output()->setContentType('text/plain');
643
                    output()->send($requestControllerOutput);
644
                }
645
            }
646
        } else {
647
            // Show Error (404) Page Not Found
648
            output()->sendError(404);
649
        }
650
    }
651
}
652