1 | <?php |
||
48 | class Application |
||
|
|||
49 | { |
||
50 | use Common\Helper; |
||
51 | use Common\Singleton; |
||
52 | |||
53 | /** |
||
54 | * @var string Environment name |
||
55 | */ |
||
56 | protected $environment = 'production'; |
||
57 | |||
58 | /** |
||
59 | * @var string Application path |
||
60 | */ |
||
61 | protected $path; |
||
62 | |||
63 | /** |
||
64 | * @var bool Debug application flag |
||
65 | */ |
||
66 | protected $debugFlag = false; |
||
67 | |||
68 | /** |
||
69 | * @var bool Layout usage flag |
||
70 | */ |
||
71 | protected $layoutFlag = true; |
||
72 | |||
73 | /** |
||
74 | * Get application environment |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | 585 | public function getEnvironment() : string |
|
79 | { |
||
80 | 585 | return $this->environment; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Get path to Application |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 585 | public function getPath() : string |
|
89 | { |
||
90 | 585 | if (!$this->path) { |
|
91 | 579 | if (defined('PATH_APPLICATION')) { |
|
92 | 579 | $this->path = PATH_APPLICATION; |
|
93 | } else { |
||
94 | $reflection = new \ReflectionClass($this); |
||
95 | // 3 level up |
||
96 | $this->path = dirname($reflection->getFileName(), 3); |
||
97 | } |
||
98 | } |
||
99 | 585 | return $this->path; |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * Return Debug flag |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | 2 | public function isDebug() : bool |
|
111 | |||
112 | /** |
||
113 | * Return/setup Layout Flag |
||
114 | * |
||
115 | * @param bool|null $flag |
||
116 | * |
||
117 | * @return bool |
||
118 | */ |
||
119 | 35 | public function useLayout($flag = null) : bool |
|
127 | |||
128 | /** |
||
129 | * Initialize system packages |
||
130 | * |
||
131 | * @param string $environment |
||
132 | * |
||
133 | * @throws ApplicationException |
||
134 | * @return void |
||
135 | */ |
||
136 | 580 | public function init($environment = 'production') : void |
|
137 | { |
||
138 | 580 | $this->environment = $environment; |
|
139 | |||
140 | try { |
||
141 | // initial default helper path |
||
142 | 580 | $this->addHelperPath(__DIR__ . '/Helper/'); |
|
143 | |||
144 | // init Config |
||
145 | 580 | $this->initConfig(); |
|
146 | |||
147 | // first log message |
||
148 | 580 | Logger::info('app:init'); |
|
149 | |||
150 | // init Session, start inside class (if needed) |
||
151 | 580 | Session::getInstance(); |
|
152 | |||
153 | // init Messages |
||
154 | 580 | Messages::getInstance(); |
|
155 | |||
156 | // init Translator |
||
157 | 580 | Translator::getInstance(); |
|
158 | |||
159 | // init Request |
||
160 | 580 | $this->initRequest(); |
|
161 | |||
162 | // init Response |
||
163 | 580 | $this->initResponse(); |
|
164 | |||
165 | // init Router |
||
166 | 580 | $this->initRouter(); |
|
167 | } catch (\Exception $e) { |
||
168 | throw new ApplicationException("Application can't be loaded: " . $e->getMessage()); |
||
169 | } |
||
170 | 580 | } |
|
171 | |||
172 | /** |
||
173 | * Initial Request instance |
||
174 | * |
||
175 | * @return void |
||
176 | */ |
||
177 | 580 | protected function initConfig() : void |
|
178 | { |
||
179 | 580 | Config::getInstance(); |
|
180 | |||
181 | // setup configuration for current environment |
||
182 | 580 | if ($debug = Config::getData('debug')) { |
|
183 | $this->debugFlag = (bool)$debug; |
||
184 | } |
||
185 | |||
186 | // initial php settings |
||
187 | 580 | if ($ini = Config::getData('php')) { |
|
188 | foreach ($ini as $key => $value) { |
||
189 | $result = ini_set($key, $value); |
||
190 | Logger::info('app:init:php:' . $key . ':' . ($result ?: '---')); |
||
191 | } |
||
192 | } |
||
193 | 580 | } |
|
194 | |||
195 | /** |
||
196 | * Initial Request instance |
||
197 | * |
||
198 | * @return void |
||
199 | * @throws \InvalidArgumentException |
||
200 | */ |
||
201 | 580 | protected function initRequest() : void |
|
202 | { |
||
203 | 580 | $request = RequestFactory::fromGlobals(); |
|
204 | |||
205 | 580 | Request::setInstance($request); |
|
206 | 580 | } |
|
207 | |||
208 | /** |
||
209 | * Initial Response instance |
||
210 | * |
||
211 | * @return void |
||
212 | */ |
||
213 | 580 | protected function initResponse() : void |
|
214 | { |
||
215 | 580 | $response = new ResponseInstance(); |
|
216 | |||
217 | 580 | Response::setInstance($response); |
|
218 | 580 | } |
|
219 | |||
220 | /** |
||
221 | * Initial Router instance |
||
222 | * |
||
223 | * @return void |
||
224 | */ |
||
225 | 580 | protected function initRouter() : void |
|
232 | |||
233 | /** |
||
234 | * Get Response instance |
||
235 | * |
||
236 | * @return \Bluz\Response\Response |
||
237 | */ |
||
238 | 3 | public function getResponse() : ResponseInstance |
|
242 | |||
243 | /** |
||
244 | * Get Request instance |
||
245 | * |
||
246 | * @return \Zend\Diactoros\ServerRequest |
||
247 | */ |
||
248 | 1 | public function getRequest() : ServerRequest |
|
252 | |||
253 | /** |
||
254 | * Run application |
||
255 | * |
||
256 | * @return void |
||
257 | * @throws ApplicationException |
||
258 | */ |
||
259 | 1 | public function run() : void |
|
260 | { |
||
261 | 1 | $this->process(); |
|
262 | 1 | $this->render(); |
|
263 | 1 | $this->end(); |
|
264 | 1 | } |
|
265 | |||
266 | /** |
||
267 | * Process application |
||
268 | * |
||
269 | * Note: |
||
270 | * - Why you don't use "X-" prefix for custom headers? |
||
271 | * - Because it deprecated ({@link http://tools.ietf.org/html/rfc6648}) |
||
272 | * |
||
273 | * @return void |
||
274 | * @throws ApplicationException |
||
275 | */ |
||
276 | 9 | public function process() : void |
|
282 | |||
283 | /** |
||
284 | * Extension point: pre process |
||
285 | * |
||
286 | * - Router processing |
||
287 | * - Analyse request headers |
||
288 | * |
||
289 | * @return void |
||
290 | * @throws ApplicationException |
||
291 | */ |
||
292 | 9 | protected function preProcess() : void |
|
307 | |||
308 | /** |
||
309 | * Do process |
||
310 | * |
||
311 | * - Dispatch controller |
||
312 | * - Exceptions handling |
||
313 | * - Setup layout |
||
314 | * - Setup response body |
||
315 | * |
||
316 | * @return void |
||
317 | */ |
||
318 | protected function doProcess() : void |
||
348 | |||
349 | /** |
||
350 | * Extension point: post process |
||
351 | * |
||
352 | * @return void |
||
353 | */ |
||
354 | 9 | protected function postProcess() : void |
|
358 | |||
359 | /** |
||
360 | * Dispatch controller with params |
||
361 | * |
||
362 | * Call dispatch from any \Bluz\Package |
||
363 | * Application::getInstance()->dispatch($module, $controller, array $params); |
||
364 | * |
||
365 | * @param string $module |
||
366 | * @param string $controller |
||
367 | * @param array $params |
||
368 | * |
||
369 | * @return Controller |
||
370 | * @throws ComponentException |
||
371 | * @throws CommonException |
||
372 | * @throws ControllerException |
||
373 | * @throws ForbiddenException |
||
374 | * @throws NotAcceptableException |
||
375 | * @throws NotAllowedException |
||
376 | */ |
||
377 | 41 | public function dispatch($module, $controller, array $params = []) : Controller |
|
392 | |||
393 | /** |
||
394 | * Extension point: pre dispatch |
||
395 | * |
||
396 | * @param Controller $controller |
||
397 | * |
||
398 | * @return void |
||
399 | * @throws ComponentException |
||
400 | * @throws ForbiddenException |
||
401 | * @throws NotAcceptableException |
||
402 | * @throws NotAllowedException |
||
403 | */ |
||
404 | 41 | protected function preDispatch($controller) : void |
|
415 | |||
416 | /** |
||
417 | * Do dispatch |
||
418 | * |
||
419 | * @param Controller $controller |
||
420 | * |
||
421 | * @return void |
||
422 | * @throws ComponentException |
||
423 | * @throws ControllerException |
||
424 | */ |
||
425 | 40 | protected function doDispatch($controller) : void |
|
430 | |||
431 | /** |
||
432 | * Extension point: post dispatch |
||
433 | * |
||
434 | * @param Controller $controller |
||
435 | * |
||
436 | * @return void |
||
437 | */ |
||
438 | 38 | protected function postDispatch($controller) : void |
|
442 | |||
443 | /** |
||
444 | * Render, is send Response |
||
445 | * |
||
446 | * @return void |
||
447 | */ |
||
448 | 1 | public function render() : void |
|
452 | |||
453 | /** |
||
454 | * Extension point: finally method |
||
455 | * |
||
456 | * @return void |
||
457 | */ |
||
458 | 1 | public function end() : void |
|
462 | } |
||
463 |