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.7 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\App\Traits; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Config\Exceptions\ConfigException; |
18
|
|
|
use Quantum\Renderer\Exceptions\RendererException; |
19
|
|
|
use Quantum\Module\Exceptions\ModuleException; |
20
|
|
|
use Quantum\Libraries\ResourceCache\ViewCache; |
21
|
|
|
use Quantum\Exceptions\StopExecutionException; |
22
|
|
|
use Quantum\Router\Exceptions\RouteException; |
23
|
|
|
use Quantum\Di\Exceptions\DiException; |
24
|
|
|
use Quantum\Exceptions\BaseException; |
25
|
|
|
use Quantum\Module\ModuleLoader; |
26
|
|
|
use DebugBar\DebugBarException; |
27
|
|
|
use Quantum\Environment\Server; |
28
|
|
|
use Quantum\Debugger\Debugger; |
29
|
|
|
use Quantum\Router\Router; |
30
|
|
|
use Quantum\Http\Response; |
31
|
|
|
use Quantum\Loader\Setup; |
32
|
|
|
use ReflectionException; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Trait WebAppTrait |
36
|
|
|
* @package Quantum\App |
37
|
|
|
*/ |
38
|
|
|
trait WebAppTrait |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $request |
43
|
|
|
* @param $response |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
private function initializeRequestResponse($request, $response) |
47
|
|
|
{ |
48
|
|
|
$request->init(Server::getInstance()); |
49
|
|
|
$response->init(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return void |
54
|
|
|
* @throws DebugBarException |
55
|
|
|
*/ |
56
|
|
|
private function initializeDebugger() |
57
|
|
|
{ |
58
|
|
|
$debugger = Debugger::getInstance(); |
59
|
|
|
$debugger->initStore(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return void |
64
|
|
|
* @throws ModuleException |
65
|
|
|
* @throws RouteException |
66
|
|
|
*/ |
67
|
|
|
private function loadModulesRoutes() |
68
|
|
|
{ |
69
|
|
|
$modulesRoutes = ModuleLoader::getInstance()->loadModulesRoutes(); |
70
|
|
|
Router::setRoutes($modulesRoutes); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return ViewCache |
75
|
|
|
* @throws ConfigException |
76
|
|
|
* @throws DiException |
77
|
|
|
* @throws ReflectionException |
78
|
|
|
*/ |
79
|
|
|
private function setupViewCache(): ViewCache |
80
|
|
|
{ |
81
|
|
|
$viewCache = ViewCache::getInstance(); |
82
|
|
|
|
83
|
|
|
if ($viewCache->isEnabled()) { |
84
|
|
|
$viewCache->setup(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $viewCache; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $request |
92
|
|
|
* @return void |
93
|
|
|
* @throws ConfigException |
94
|
|
|
* @throws DiException |
95
|
|
|
* @throws ReflectionException |
96
|
|
|
* @throws RouteException |
97
|
|
|
* @throws StopExecutionException |
98
|
|
|
* @throws BaseException |
99
|
|
|
* @throws RendererException |
100
|
|
|
*/ |
101
|
|
|
private function initializeRouter($request) |
102
|
|
|
{ |
103
|
|
|
$router = new Router($request); |
104
|
|
|
$router->findRoute(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Response $response |
109
|
|
|
* @return void |
110
|
|
|
* @throws ConfigException |
111
|
|
|
* @throws DiException |
112
|
|
|
* @throws ReflectionException |
113
|
|
|
*/ |
114
|
|
|
private function handleCors(Response $response) |
115
|
|
|
{ |
116
|
|
|
if (!config()->has('cors')) { |
117
|
|
|
config()->import(new Setup('config', 'cors')); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
foreach (config()->get('cors') as $key => $value) { |
121
|
|
|
$response->setHeader($key, $value); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |