Passed
Pull Request — master (#322)
by Arman
03:30
created

WebAppTrait::loadModules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 10
c 1
b 0
f 0
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.8
13
 */
14
15
namespace Quantum\App\Traits;
16
17
use Quantum\App\Exceptions\StopExecutionException;
18
use Quantum\Libraries\ResourceCache\ViewCache;
19
use Quantum\Module\Exceptions\ModuleException;
20
use Quantum\Config\Exceptions\ConfigException;
21
use Quantum\Router\Exceptions\RouteException;
22
use Quantum\Http\Exceptions\HttpException;
23
use Quantum\App\Exceptions\BaseException;
24
use Quantum\Di\Exceptions\DiException;
25
use Quantum\Module\ModuleLoader;
26
use DebugBar\DebugBarException;
27
use Quantum\Environment\Server;
28
use Quantum\Debugger\Debugger;
29
use Quantum\Http\Response;
30
use Quantum\Router\Router;
31
use Quantum\Http\Request;
32
use Quantum\Loader\Setup;
33
use ReflectionException;
34
use Quantum\Di\Di;
35
36
/**
37
 * Trait WebAppTrait
38
 * @package Quantum\App
39
 */
40
trait WebAppTrait
41
{
42
43
    /**
44
     * @param Request $request
45
     * @param Response $response
46
     * @throws BaseException
47
     * @throws DiException
48
     * @throws ReflectionException
49
     * @throws HttpException
50
     */
51
    private function initializeRequestResponse(Request $request, Response $response)
52
    {
53
        $request->init(Server::getInstance());
0 ignored issues
show
Bug introduced by
The method init() does not exist on Quantum\Http\Request. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

53
        $request->/** @scrutinizer ignore-call */ 
54
                  init(Server::getInstance());
Loading history...
54
        $response->init();
55
    }
56
57
    /**
58
     * @throws DebugBarException
59
     */
60
    private function initializeDebugger()
61
    {
62
        $debugger = Debugger::getInstance();
63
        $debugger->initStore();
64
    }
65
66
    /**
67
     * Load modules
68
     * @throws ModuleException
69
     * @throws RouteException
70
     */
71
    private function loadModules()
72
    {
73
        $moduleLoader = ModuleLoader::getInstance();
74
75
        $modulesDependencies = $moduleLoader->loadModulesDependencies();
76
        Di::registerDependencies($modulesDependencies);
77
78
        $modulesRoutes = $moduleLoader->loadModulesRoutes();
79
        Router::setRoutes($modulesRoutes);
80
    }
81
82
    /**
83
     * @return ViewCache
84
     * @throws ConfigException
85
     * @throws DiException
86
     * @throws ReflectionException
87
     */
88
    private function setupViewCache(): ViewCache
89
    {
90
        $viewCache = ViewCache::getInstance();
91
92
        if ($viewCache->isEnabled()) {
93
            $viewCache->setup();
94
        }
95
96
        return $viewCache;
97
    }
98
99
    /**
100
     * @param $request
101
     * @throws BaseException
102
     * @throws ConfigException
103
     * @throws DebugBarException
104
     * @throws DiException
105
     * @throws ReflectionException
106
     * @throws RouteException
107
     * @throws StopExecutionException
108
     */
109
    private function initializeRouter($request)
110
    {
111
        $router = new Router($request);
112
        $router->findRoute();
113
    }
114
115
    /**
116
     * @param Response $response
117
     * @throws ConfigException
118
     * @throws DiException
119
     * @throws ReflectionException
120
     */
121
    private function handleCors(Response $response)
122
    {
123
        if (!config()->has('cors')) {
124
            config()->import(new Setup('config', 'cors'));
125
        }
126
127
        foreach (config()->get('cors') as $key => $value) {
128
            $response->setHeader($key, $value);
129
        }
130
    }
131
}