Issues (169)

src/Bootstrap.php (1 issue)

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.9.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 ReflectionException;
29
use Psr\Log\LogLevel;
30
use Quantum\Di\Di;
31
32
/**
33
 * Class Bootstrap
34
 * @package Quantum
35
 */
36
class Bootstrap
37
{
38
39
    /**
40
     * Boots the app
41
     * @throws Exceptions\ModuleLoaderException
42
     * @throws Exceptions\ControllerException
43
     * @throws Exceptions\MiddlewareException
44
     * @throws Exceptions\ConfigException
45
     * @throws Exceptions\RouteException
46
     * @throws Exceptions\ViewException
47
     * @throws ReflectionException
48
     * @throws \Twig\Error\LoaderError
49
     * @throws \Twig\Error\RuntimeError
50
     * @throws \Twig\Error\SyntaxError
51
     */
52
    public static function run()
53
    {
54
        try {
55
            $request = Di::get(Request::class);
56
            $response = Di::get(Response::class);
57
58
            $request->init(Server::getInstance());
59
            $response->init();
60
61
            if ($request->getMethod() == 'OPTIONS') {
62
                stop();
63
            }
64
65
            Debugger::initStore();
66
67
            ModuleLoader::loadModulesRoutes();
68
69
            $router = new Router($request, $response);
70
            $router->findRoute();
71
72
            if (config()->get('multilang')) {
73
                Lang::getInstance((int)config()->get(Lang::LANG_SEGMENT))->load();
74
            }
75
76
            Debugger::addToStore(Debugger::HOOKS, LogLevel::INFO, HookManager::getRegistered());
77
78
            MvcManager::handle($request, $response);
79
80
            stop();
81
        } catch (StopExecutionException $e) {
82
            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...
83
            $response->send();
84
        }
85
    }
86
87
    /**
88
     * Handles CORS
89
     * @param Response $response
90
     * @throws Exceptions\ConfigException
91
     * @throws Exceptions\DiException
92
     * @throws ReflectionException
93
     */
94
    private static function handleCors(Response $response)
95
    {
96
        if (!config()->has('cors')) {
97
            config()->import(new Setup('config', 'cors'));
98
        }
99
100
        foreach (config()->get('cors') as $key => $value) {
101
            $response->setHeader($key, $value);
102
        }
103
    }
104
105
}
106