1 | <?php |
||
2 | namespace Mezon\Application; |
||
3 | |||
4 | use Mezon\HtmlTemplate\HtmlTemplate; |
||
5 | use Mezon\Rest; |
||
6 | |||
7 | /** |
||
8 | * Class CommonApplication |
||
9 | * |
||
10 | * @package Mezon |
||
11 | * @subpackage CommonApplication |
||
12 | * @author Dodonov A.A. |
||
13 | * @version v.1.0 (2019/08/07) |
||
14 | * @copyright Copyright (c) 2019, aeon.org |
||
15 | */ |
||
16 | |||
17 | /** |
||
18 | * Common application with any available template |
||
19 | * |
||
20 | * To load routes from the config call $this->load_routes_from_config('./conf/routes.json'); |
||
21 | * |
||
22 | * The format of the *.json config must be like this: |
||
23 | * |
||
24 | * [ |
||
25 | * { |
||
26 | * "route" : "/route1" , |
||
27 | * "callback" : "callback1" , |
||
28 | * "method" : "POST" |
||
29 | * } , |
||
30 | * { |
||
31 | * "route" : "/route2" , |
||
32 | * "callback" : "callback2" , |
||
33 | * "method" : ["GET" , "POST"] |
||
34 | * } |
||
35 | * ] |
||
36 | */ |
||
37 | class CommonApplication extends Application |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * Application's template |
||
42 | * |
||
43 | * @var HtmlTemplate |
||
44 | */ |
||
45 | private $template = false; |
||
46 | |||
47 | /** |
||
48 | * Constructor |
||
49 | * |
||
50 | * @param HtmlTemplate $template |
||
51 | * Template |
||
52 | */ |
||
53 | public function __construct(HtmlTemplate $template) |
||
54 | { |
||
55 | parent::__construct(); |
||
56 | |||
57 | $this->template = $template; |
||
58 | |||
59 | $this->getRouter()->setNoProcessorFoundErrorHandler([ |
||
60 | $this, |
||
61 | 'noRouteFoundErrorHandler' |
||
62 | ]); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Method handles 404 errors |
||
67 | * |
||
68 | * @codeCoverageIgnore |
||
69 | */ |
||
70 | public function noRouteFoundErrorHandler(): void |
||
71 | { |
||
72 | $this->redirectTo('/404'); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Method renders common parts of all pages. |
||
77 | * |
||
78 | * @return array List of common parts. |
||
79 | */ |
||
80 | public function crossRender(): array |
||
81 | { |
||
82 | return []; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Method formats exception object |
||
87 | * |
||
88 | * @param \Exception $e |
||
89 | * Exception |
||
90 | * @return object Formatted exception object |
||
91 | */ |
||
92 | protected function baseFormatter(\Exception $e): object |
||
93 | { |
||
94 | $error = new \stdClass(); |
||
95 | $error->message = $e->getMessage(); |
||
96 | $error->code = $e->getCode(); |
||
97 | $error->call_stack = $this->formatCallStack($e); |
||
98 | if (isset($_SERVER['HTTP_HOST']) && $_SERVER['REQUEST_URI']) { |
||
99 | $error->host = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
100 | } else { |
||
101 | $error->host = 'undefined'; |
||
102 | } |
||
103 | return $error; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Method processes exception. |
||
108 | * |
||
109 | * @param Rest\Exception $e |
||
110 | * RestException object. |
||
111 | */ |
||
112 | public function handleRestException(Rest\Exception $e): void |
||
113 | { |
||
114 | $error = $this->baseFormatter($e); |
||
115 | |||
116 | $error->httpBody = $e->getHttpBody(); |
||
117 | |||
118 | print('<pre>' . json_encode($error, JSON_PRETTY_PRINT)); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Method processes exception. |
||
123 | * |
||
124 | * @param \Exception $e |
||
125 | * Exception object. |
||
126 | */ |
||
127 | public function handleException(\Exception $e): void |
||
128 | { |
||
129 | $error = $this->baseFormatter($e); |
||
130 | |||
131 | print('<pre>' . json_encode($error, JSON_PRETTY_PRINT)); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Running application. |
||
136 | */ |
||
137 | public function run(): void |
||
138 | { |
||
139 | try { |
||
140 | $callRouteResult = $this->callRoute(); |
||
141 | if (is_array($callRouteResult) === false) { |
||
142 | throw (new \Exception('Route was not called properly')); |
||
143 | } |
||
144 | |||
145 | $result = array_merge($callRouteResult, $this->crossRender()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
146 | |||
147 | if (is_array($result)) { |
||
0 ignored issues
–
show
|
|||
148 | foreach ($result as $key => $value) { |
||
149 | $content = $value instanceof ViewInterface ? $value->render() : $value; |
||
150 | |||
151 | $this->template->setPageVar($key, $content); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | print($this->template->compile()); |
||
156 | } catch (Rest\Exception $e) { |
||
157 | $this->handleRestException($e); |
||
158 | } catch (\Exception $e) { |
||
159 | $this->handleException($e); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Getting template |
||
165 | * |
||
166 | * @return HtmlTemplate Application's template |
||
167 | * @codeCoverageIgnore |
||
168 | */ |
||
169 | public function getTemplate(): HtmlTemplate |
||
170 | { |
||
171 | return $this->template; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Setting template |
||
176 | * |
||
177 | * @param HtmlTemplate $template |
||
178 | * Template |
||
179 | * @codeCoverageIgnore |
||
180 | */ |
||
181 | public function setTemplate(HtmlTemplate $template): void |
||
182 | { |
||
183 | $this->template = $template; |
||
184 | } |
||
185 | } |
||
186 |