1 | <?php |
||
49 | class Application |
||
|
|||
50 | { |
||
51 | use Common\Helper; |
||
52 | use Common\Singleton; |
||
53 | |||
54 | /** |
||
55 | * @var string Environment name |
||
56 | */ |
||
57 | protected $environment = 'production'; |
||
58 | |||
59 | /** |
||
60 | * @var string Application path |
||
61 | */ |
||
62 | protected $path; |
||
63 | |||
64 | /** |
||
65 | * @var bool Debug application flag |
||
66 | */ |
||
67 | protected $debugFlag = false; |
||
68 | |||
69 | /** |
||
70 | * @var bool Layout usage flag |
||
71 | */ |
||
72 | protected $layoutFlag = true; |
||
73 | |||
74 | /** |
||
75 | * Get application environment |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 604 | public function getEnvironment() : string |
|
83 | |||
84 | /** |
||
85 | * Get path to Application |
||
86 | * |
||
87 | * @return string |
||
88 | * @throws \ReflectionException |
||
89 | */ |
||
90 | 604 | public function getPath() : string |
|
103 | |||
104 | /** |
||
105 | * Return Debug flag |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | 2 | public function isDebug() : bool |
|
113 | |||
114 | /** |
||
115 | * Return/setup Layout Flag |
||
116 | * |
||
117 | * @param bool|null $flag |
||
118 | * |
||
119 | * @return bool |
||
120 | */ |
||
121 | 35 | public function useLayout($flag = null) : bool |
|
129 | |||
130 | /** |
||
131 | * Initialize system packages |
||
132 | * |
||
133 | * @param string $environment |
||
134 | * |
||
135 | * @throws ApplicationException |
||
136 | * @return void |
||
137 | */ |
||
138 | 604 | public function init($environment = 'production') : void |
|
173 | |||
174 | /** |
||
175 | * Initial Request instance |
||
176 | * |
||
177 | * @return void |
||
178 | * @throws \Bluz\Config\ConfigException |
||
179 | * @throws \ReflectionException |
||
180 | */ |
||
181 | 604 | protected function initConfig() : void |
|
206 | |||
207 | /** |
||
208 | * Initial Request instance |
||
209 | * |
||
210 | * @return void |
||
211 | * @throws \InvalidArgumentException |
||
212 | */ |
||
213 | 604 | protected function initRequest() : void |
|
219 | |||
220 | /** |
||
221 | * Initial Response instance |
||
222 | * |
||
223 | * @return void |
||
224 | */ |
||
225 | 604 | protected function initResponse() : void |
|
231 | |||
232 | /** |
||
233 | * Initial Router instance |
||
234 | * |
||
235 | * @return void |
||
236 | * @throws \Bluz\Config\ConfigException |
||
237 | */ |
||
238 | 604 | protected function initRouter() : void |
|
245 | |||
246 | /** |
||
247 | * Get Response instance |
||
248 | * |
||
249 | * @return \Bluz\Response\Response |
||
250 | */ |
||
251 | 3 | public function getResponse() : ResponseInstance |
|
255 | |||
256 | /** |
||
257 | * Get Request instance |
||
258 | * |
||
259 | * @return \Zend\Diactoros\ServerRequest |
||
260 | */ |
||
261 | 1 | public function getRequest() : ServerRequest |
|
265 | |||
266 | /** |
||
267 | * Run application |
||
268 | * |
||
269 | * @return void |
||
270 | * @throws ApplicationException |
||
271 | */ |
||
272 | 1 | public function run() : void |
|
278 | |||
279 | /** |
||
280 | * Process application |
||
281 | * |
||
282 | * Note: |
||
283 | * - Why you don't use "X-" prefix for custom headers? |
||
284 | * - Because it deprecated ({@link http://tools.ietf.org/html/rfc6648}) |
||
285 | * |
||
286 | * @return void |
||
287 | * @throws ApplicationException |
||
288 | */ |
||
289 | 9 | public function process() : void |
|
295 | |||
296 | /** |
||
297 | * Extension point: pre process |
||
298 | * |
||
299 | * - Router processing |
||
300 | * - Analyse request headers |
||
301 | * |
||
302 | * @return void |
||
303 | */ |
||
304 | 9 | protected function preProcess() : void |
|
319 | |||
320 | /** |
||
321 | * Do process |
||
322 | * |
||
323 | * - Dispatch controller |
||
324 | * - Exceptions handling |
||
325 | * - Setup layout |
||
326 | * - Setup response body |
||
327 | * |
||
328 | * @return void |
||
329 | */ |
||
330 | protected function doProcess() : void |
||
360 | |||
361 | /** |
||
362 | * Extension point: post process |
||
363 | * |
||
364 | * @return void |
||
365 | */ |
||
366 | 9 | protected function postProcess() : void |
|
370 | |||
371 | /** |
||
372 | * Dispatch controller with params |
||
373 | * |
||
374 | * Call dispatch from any \Bluz\Package |
||
375 | * Application::getInstance()->dispatch($module, $controller, array $params); |
||
376 | * |
||
377 | * @param string $module |
||
378 | * @param string $controller |
||
379 | * @param array $params |
||
380 | * |
||
381 | * @return Controller |
||
382 | * @throws ComponentException |
||
383 | * @throws CommonException |
||
384 | * @throws ControllerException |
||
385 | * @throws ForbiddenException |
||
386 | * @throws NotAcceptableException |
||
387 | * @throws NotAllowedException |
||
388 | */ |
||
389 | 41 | public function dispatch($module, $controller, array $params = []) : Controller |
|
404 | |||
405 | /** |
||
406 | * Extension point: pre dispatch |
||
407 | * |
||
408 | * @param Controller $controller |
||
409 | * |
||
410 | * @return void |
||
411 | * @throws ComponentException |
||
412 | * @throws ForbiddenException |
||
413 | * @throws NotAcceptableException |
||
414 | * @throws NotAllowedException |
||
415 | */ |
||
416 | 41 | protected function preDispatch($controller) : void |
|
427 | |||
428 | /** |
||
429 | * Do dispatch |
||
430 | * |
||
431 | * @param Controller $controller |
||
432 | * |
||
433 | * @return void |
||
434 | * @throws ComponentException |
||
435 | * @throws ControllerException |
||
436 | */ |
||
437 | 40 | protected function doDispatch($controller) : void |
|
442 | |||
443 | /** |
||
444 | * Extension point: post dispatch |
||
445 | * |
||
446 | * @param Controller $controller |
||
447 | * |
||
448 | * @return void |
||
449 | */ |
||
450 | 38 | protected function postDispatch($controller) : void |
|
454 | |||
455 | /** |
||
456 | * Render, is send Response |
||
457 | * |
||
458 | * @return void |
||
459 | */ |
||
460 | 1 | public function render() : void |
|
464 | |||
465 | /** |
||
466 | * Extension point: finally method |
||
467 | * |
||
468 | * @return void |
||
469 | */ |
||
470 | 1 | public function end() : void |
|
474 | } |
||
475 |