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 | */ |
||
237 | 604 | protected function initRouter(): void |
|
238 | { |
||
239 | 604 | $router = new \Bluz\Router\Router(); |
|
240 | 604 | $router->setOptions(Config::get('router')); |
|
241 | |||
242 | 604 | Router::setInstance($router); |
|
243 | 604 | } |
|
244 | |||
245 | /** |
||
246 | * Get Response instance |
||
247 | * |
||
248 | * @return \Bluz\Response\Response |
||
249 | */ |
||
250 | 3 | public function getResponse(): ResponseInstance |
|
254 | |||
255 | /** |
||
256 | * Get Request instance |
||
257 | * |
||
258 | * @return \Zend\Diactoros\ServerRequest |
||
259 | */ |
||
260 | 1 | public function getRequest(): ServerRequest |
|
264 | |||
265 | /** |
||
266 | * Run application |
||
267 | * |
||
268 | * @return void |
||
269 | * @throws ApplicationException |
||
270 | */ |
||
271 | 1 | public function run(): void |
|
277 | |||
278 | /** |
||
279 | * Process application |
||
280 | * |
||
281 | * Note: |
||
282 | * - Why you don't use "X-" prefix for custom headers? |
||
283 | * - Because it deprecated ({@link http://tools.ietf.org/html/rfc6648}) |
||
284 | * |
||
285 | * @return void |
||
286 | */ |
||
287 | 9 | public function process(): void |
|
293 | |||
294 | /** |
||
295 | * Extension point: pre process |
||
296 | * |
||
297 | * - Router processing |
||
298 | * - Analyse request headers |
||
299 | * |
||
300 | * @return void |
||
301 | */ |
||
302 | 9 | protected function preProcess(): void |
|
317 | |||
318 | /** |
||
319 | * Do process |
||
320 | * |
||
321 | * - Dispatch controller |
||
322 | * - Exceptions handling |
||
323 | * - Setup layout |
||
324 | * - Setup response body |
||
325 | * |
||
326 | * @return void |
||
327 | */ |
||
328 | protected function doProcess(): void |
||
358 | |||
359 | /** |
||
360 | * Extension point: post process |
||
361 | * |
||
362 | * @return void |
||
363 | */ |
||
364 | 9 | protected function postProcess(): void |
|
368 | |||
369 | /** |
||
370 | * Dispatch controller with params |
||
371 | * |
||
372 | * Call dispatch from any \Bluz\Package |
||
373 | * Application::getInstance()->dispatch($module, $controller, array $params); |
||
374 | * |
||
375 | * @param string $module |
||
376 | * @param string $controller |
||
377 | * @param array $params |
||
378 | * |
||
379 | * @return Controller |
||
380 | * @throws ComponentException |
||
381 | * @throws CommonException |
||
382 | * @throws ControllerException |
||
383 | * @throws ForbiddenException |
||
384 | * @throws NotAcceptableException |
||
385 | * @throws NotAllowedException |
||
386 | */ |
||
387 | 41 | public function dispatch($module, $controller, array $params = []): Controller |
|
402 | |||
403 | /** |
||
404 | * Extension point: pre dispatch |
||
405 | * |
||
406 | * @param Controller $controller |
||
407 | * |
||
408 | * @return void |
||
409 | */ |
||
410 | 41 | protected function preDispatch($controller): void |
|
421 | |||
422 | /** |
||
423 | * Do dispatch |
||
424 | * |
||
425 | * @param Controller $controller |
||
426 | * |
||
427 | * @return void |
||
428 | * @throws ComponentException |
||
429 | * @throws ControllerException |
||
430 | */ |
||
431 | 40 | protected function doDispatch($controller): void |
|
436 | |||
437 | /** |
||
438 | * Extension point: post dispatch |
||
439 | * |
||
440 | * @param Controller $controller |
||
441 | * |
||
442 | * @return void |
||
443 | */ |
||
444 | 38 | protected function postDispatch($controller): void |
|
448 | |||
449 | /** |
||
450 | * Render, is send Response |
||
451 | * |
||
452 | * @return void |
||
453 | */ |
||
454 | 1 | public function render(): void |
|
458 | |||
459 | /** |
||
460 | * Extension point: finally method |
||
461 | * |
||
462 | * @return void |
||
463 | */ |
||
464 | 1 | public function end(): void |
|
468 | } |
||
469 |