Passed
Pull Request — master (#108)
by Arman
03:08
created

Bootstrap   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 24
c 5
b 0
f 0
dl 0
loc 71
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handleCors() 0 8 3
A run() 0 32 4
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.8.0
13
 */
14
15
namespace Quantum;
16
17
use Quantum\Exceptions\StopExecutionException;
18
use Quantum\Router\ModuleLoader;
19
use Quantum\Libraries\Lang\Lang;
20
use Quantum\Environment\Server;
21
use Quantum\Debugger\Debugger;
22
use Quantum\Hooks\HookManager;
23
use Quantum\Mvc\MvcManager;
24
use Quantum\Router\Router;
25
use Quantum\Http\Response;
26
use Quantum\Http\Request;
27
use Quantum\Loader\Setup;
28
use Psr\Log\LogLevel;
29
use Quantum\Di\Di;
30
31
/**
32
 * Class Bootstrap
33
 * @package Quantum
34
 */
35
class Bootstrap
36
{
37
38
    /**
39
     * Boots the app
40
     * @throws Exceptions\ModuleLoaderException
41
     * @throws Exceptions\ControllerException
42
     * @throws Exceptions\MiddlewareException
43
     * @throws Exceptions\DatabaseException
44
     * @throws Exceptions\SessionException
45
     * @throws Exceptions\ConfigException
46
     * @throws Exceptions\RouteException
47
     * @throws Exceptions\CsrfException
48
     * @throws Exceptions\LangException
49
     * @throws Exceptions\ViewException
50
     * @throws Exceptions\DiException
51
     * @throws \Twig\Error\RuntimeError
52
     * @throws \Twig\Error\LoaderError
53
     * @throws \Twig\Error\SyntaxError
54
     * @throws \ReflectionException
55
     */
56
    public static function run()
57
    {
58
        try {
59
            $request = Di::get(Request::class);
60
            $response = Di::get(Response::class);
61
62
            $request->init(new Server);
63
            $response->init();
64
65
            if ($request->getMethod() == 'OPTIONS') {
66
                stop();
67
            }
68
69
            Debugger::initStore();
70
71
            ModuleLoader::loadModulesRoutes();
72
73
            $router = new Router($request, $response);
74
            $router->findRoute();
75
76
            if (config()->get('multilang')) {
77
                Lang::getInstance((int)config()->get(Lang::LANG_SEGMENT))->load();
78
            }
79
80
            Debugger::addToStore(Debugger::HOOKS, LogLevel::INFO, HookManager::getRegistered());
81
82
            MvcManager::handle($request, $response);
83
84
            stop();
85
        } catch (StopExecutionException $e) {
86
            self::handleCors($response);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $response does not seem to be defined for all execution paths leading up to this point.
Loading history...
87
            $response->send();
88
        }
89
    }
90
91
    /**
92
     * Handles CORS
93
     * @param Response $response
94
     * @throws Exceptions\ConfigException
95
     * @throws Exceptions\DiException
96
     * @throws \ReflectionException
97
     */
98
    private static function handleCors(Response $response)
99
    {
100
        if (!config()->has('cors')) {
101
            config()->import(new Setup('config', 'cors'));
102
        }
103
104
        foreach (config()->get('cors') as $key => $value) {
105
            $response->setHeader($key, $value);
106
        }
107
    }
108
109
}
110