|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mezon\Application; |
|
3
|
|
|
|
|
4
|
|
|
use Mezon\HtmlTemplate\HtmlTemplate; |
|
5
|
|
|
use Mezon\Rest; |
|
6
|
|
|
use Mezon\Router\Utils; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class CommonApplication |
|
10
|
|
|
* |
|
11
|
|
|
* @package Mezon |
|
12
|
|
|
* @subpackage CommonApplication |
|
13
|
|
|
* @author Dodonov A.A. |
|
14
|
|
|
* @version v.1.0 (2019/08/07) |
|
15
|
|
|
* @copyright Copyright (c) 2019, aeon.org |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Common application with any available template |
|
20
|
|
|
* |
|
21
|
|
|
* To load routes from the config call $this->load_routes_from_config('./conf/routes.json'); |
|
22
|
|
|
* |
|
23
|
|
|
* The format of the *.json config must be like this: |
|
24
|
|
|
* |
|
25
|
|
|
* [ |
|
26
|
|
|
* { |
|
27
|
|
|
* "route" : "/route1" , |
|
28
|
|
|
* "callback" : "callback1" , |
|
29
|
|
|
* "method" : "POST" |
|
30
|
|
|
* } , |
|
31
|
|
|
* { |
|
32
|
|
|
* "route" : "/route2" , |
|
33
|
|
|
* "callback" : "callback2" , |
|
34
|
|
|
* "method" : ["GET" , "POST"] |
|
35
|
|
|
* } |
|
36
|
|
|
* ] |
|
37
|
|
|
*/ |
|
38
|
|
|
class CommonApplication extends Application |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Application's template |
|
43
|
|
|
* |
|
44
|
|
|
* @var HtmlTemplate |
|
45
|
|
|
*/ |
|
46
|
|
|
private $template = false; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Constructor |
|
50
|
|
|
* |
|
51
|
|
|
* @param HtmlTemplate $template |
|
52
|
|
|
* Template |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(HtmlTemplate $template) |
|
55
|
|
|
{ |
|
56
|
|
|
parent::__construct(); |
|
57
|
|
|
|
|
58
|
|
|
$this->template = $template; |
|
59
|
|
|
|
|
60
|
|
|
$this->getRouter()->setNoProcessorFoundErrorHandler([ |
|
61
|
|
|
$this, |
|
62
|
|
|
'noRouteFoundErrorHandler' |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
$this->loadActoinsFromConfig(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Method handles 404 errors |
|
70
|
|
|
* |
|
71
|
|
|
* @codeCoverageIgnore |
|
72
|
|
|
*/ |
|
73
|
|
|
public function noRouteFoundErrorHandler(): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->redirectTo('/404'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Method renders common parts of all pages. |
|
80
|
|
|
* |
|
81
|
|
|
* @return array List of common parts. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function crossRender(): array |
|
84
|
|
|
{ |
|
85
|
|
|
return []; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Method formats exception object |
|
90
|
|
|
* |
|
91
|
|
|
* @param \Exception $e |
|
92
|
|
|
* Exception |
|
93
|
|
|
* @return object Formatted exception object |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function baseFormatter(\Exception $e): object |
|
96
|
|
|
{ |
|
97
|
|
|
$error = new \stdClass(); |
|
98
|
|
|
$error->message = $e->getMessage(); |
|
99
|
|
|
$error->code = $e->getCode(); |
|
100
|
|
|
$error->call_stack = $this->formatCallStack($e); |
|
101
|
|
|
if (isset($_SERVER['HTTP_HOST']) && $_SERVER['REQUEST_URI']) { |
|
102
|
|
|
$error->host = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
|
103
|
|
|
} else { |
|
104
|
|
|
$error->host = 'undefined'; |
|
105
|
|
|
} |
|
106
|
|
|
return $error; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Method processes exception. |
|
111
|
|
|
* |
|
112
|
|
|
* @param Rest\Exception $e |
|
113
|
|
|
* RestException object. |
|
114
|
|
|
*/ |
|
115
|
|
|
public function handleRestException(Rest\Exception $e): void |
|
116
|
|
|
{ |
|
117
|
|
|
$error = $this->baseFormatter($e); |
|
118
|
|
|
|
|
119
|
|
|
$error->httpBody = $e->getHttpBody(); |
|
120
|
|
|
|
|
121
|
|
|
print('<pre>' . json_encode($error, JSON_PRETTY_PRINT)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Method processes exception. |
|
126
|
|
|
* |
|
127
|
|
|
* @param \Exception $e |
|
128
|
|
|
* Exception object. |
|
129
|
|
|
*/ |
|
130
|
|
|
public function handleException(\Exception $e): void |
|
131
|
|
|
{ |
|
132
|
|
|
$error = $this->baseFormatter($e); |
|
133
|
|
|
|
|
134
|
|
|
print('<pre>' . json_encode($error, JSON_PRETTY_PRINT)); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Method sets GET parameter as template var |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $fieldName |
|
141
|
|
|
* name of the GET parameter |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function setGetVar(string $fieldName): void |
|
144
|
|
|
{ |
|
145
|
|
|
if (isset($_GET[$fieldName])) { |
|
146
|
|
|
$this->template->setPageVar($fieldName, $_GET[$fieldName]); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Running application. |
|
152
|
|
|
*/ |
|
153
|
|
|
public function run(): void |
|
154
|
|
|
{ |
|
155
|
|
|
try { |
|
156
|
|
|
$callRouteResult = $this->callRoute(); |
|
157
|
|
|
|
|
158
|
|
|
if (is_array($callRouteResult) === false) { |
|
159
|
|
|
throw (new \Exception('Route was not called properly')); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$result = array_merge($callRouteResult, $this->crossRender()); |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
if (is_array($result)) { |
|
|
|
|
|
|
165
|
|
|
foreach ($result as $key => $value) { |
|
166
|
|
|
$content = $value instanceof ViewInterface ? $value->render() : $value; |
|
167
|
|
|
|
|
168
|
|
|
$this->template->setPageVar($key, $content); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$this->setGetVar('redirect-to'); |
|
173
|
|
|
|
|
174
|
|
|
print($this->template->compile()); |
|
175
|
|
|
} catch (Rest\Exception $e) { |
|
176
|
|
|
$this->handleRestException($e); |
|
177
|
|
|
} catch (\Exception $e) { |
|
178
|
|
|
$this->handleException($e); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Getting template |
|
184
|
|
|
* |
|
185
|
|
|
* @return HtmlTemplate Application's template |
|
186
|
|
|
* @codeCoverageIgnore |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getTemplate(): HtmlTemplate |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->template; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Setting template |
|
195
|
|
|
* |
|
196
|
|
|
* @param HtmlTemplate $template |
|
197
|
|
|
* Template |
|
198
|
|
|
* @codeCoverageIgnore |
|
199
|
|
|
*/ |
|
200
|
|
|
public function setTemplate(HtmlTemplate $template): void |
|
201
|
|
|
{ |
|
202
|
|
|
$this->template = $template; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Method returns localized error message by it's key |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $actionMessageCode |
|
209
|
|
|
* key of the message |
|
210
|
|
|
* @return string localized error message by it's key |
|
211
|
|
|
*/ |
|
212
|
|
|
protected function getActionMessage(string $actionMessageCode): string |
|
213
|
|
|
{ |
|
214
|
|
|
$classPath = $this->getClassPath(); |
|
215
|
|
|
|
|
216
|
|
|
if (file_exists($classPath . '/res/action-messages.json')) { |
|
217
|
|
|
$messages = json_decode(file_get_contents($classPath . '/res/action-messages.json'), true); |
|
218
|
|
|
|
|
219
|
|
|
if (isset($messages[$actionMessageCode])) { |
|
220
|
|
|
return $messages[$actionMessageCode]; |
|
221
|
|
|
} else { |
|
222
|
|
|
throw (new \Exception('The message with locator "' . $actionMessageCode . '" was not found', - 1)); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return ''; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Method returns action-message or '' if not set |
|
231
|
|
|
* |
|
232
|
|
|
* @return string |
|
233
|
|
|
*/ |
|
234
|
|
|
protected function getActionMessageCode(): string |
|
235
|
|
|
{ |
|
236
|
|
|
return $_GET['action-message'] ?? ''; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Method sets message variable |
|
241
|
|
|
* |
|
242
|
|
|
* @param string $actionMessageCode |
|
243
|
|
|
* message code |
|
244
|
|
|
*/ |
|
245
|
|
|
protected function setSuccessMessage(string $actionMessageCode): void |
|
246
|
|
|
{ |
|
247
|
|
|
$this->getTemplate()->setPageVar('action-message', $this->getActionMessage($actionMessageCode)); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Method compiles result record |
|
252
|
|
|
* |
|
253
|
|
|
* @param mixed $presenter |
|
254
|
|
|
* main area presenter |
|
255
|
|
|
* @return array result record |
|
256
|
|
|
*/ |
|
257
|
|
|
public function result($presenter = null): void |
|
258
|
|
|
{ |
|
259
|
|
|
if ($presenter !== null) { |
|
260
|
|
|
$presenter->run(); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
if ($actionMessage = $this->getActionMessageCode()) { |
|
264
|
|
|
$this->setSuccessMessage($actionMessage); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Method creates action from JSON config |
|
270
|
|
|
* |
|
271
|
|
|
* @param string $path |
|
272
|
|
|
* path to JSON config |
|
273
|
|
|
*/ |
|
274
|
|
|
private function createActionFromJsonConfig(string $path): void |
|
275
|
|
|
{ |
|
276
|
|
|
$method = 'action' . basename($path, '.json'); |
|
277
|
|
|
|
|
278
|
|
|
$this->$method = function () use ($path): array { |
|
279
|
|
|
$result = []; |
|
280
|
|
|
$views = []; |
|
281
|
|
|
$presenter = null; |
|
282
|
|
|
$config = json_decode(file_get_contents($path), true); |
|
283
|
|
|
|
|
284
|
|
|
foreach ($config as $key => $value) { |
|
285
|
|
|
if (is_string($value)) { |
|
286
|
|
|
$result[$key] = $value; |
|
287
|
|
|
} elseif ($key === 'presenter') { |
|
288
|
|
|
$presenter = new $value['class']( |
|
289
|
|
|
isset($value['view']) && isset($views[$value['view']]) ? $views[$value['view']] : null, |
|
290
|
|
|
$value['name'], |
|
291
|
|
|
$this->getRequestParamsFetcher()); |
|
292
|
|
|
} else { |
|
293
|
|
|
$views[$key] = new $value['class']($this->getTemplate(), $value['name']); |
|
294
|
|
|
$result[$value['placeholder']] = $views[$key]; |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
$this->result($presenter); |
|
299
|
|
|
|
|
300
|
|
|
return $result; |
|
301
|
|
|
}; |
|
302
|
|
|
|
|
303
|
|
|
$this->loadRoute( |
|
304
|
|
|
[ |
|
305
|
|
|
'route' => Utils::convertMethodNameToRoute($method), |
|
306
|
|
|
'callback' => [ |
|
307
|
|
|
$this, |
|
308
|
|
|
$method |
|
309
|
|
|
], |
|
310
|
|
|
'method' => [ |
|
311
|
|
|
'GET', |
|
312
|
|
|
'POST' |
|
313
|
|
|
] |
|
314
|
|
|
]); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Method loads all actions from ./actions directory |
|
319
|
|
|
*/ |
|
320
|
|
|
private function loadActoinsFromConfig(): void |
|
321
|
|
|
{ |
|
322
|
|
|
$classPath = $this->getClassPath(); |
|
323
|
|
|
|
|
324
|
|
|
if (file_exists($classPath . '/actions')) { |
|
325
|
|
|
$files = scandir($classPath . '/actions'); |
|
326
|
|
|
|
|
327
|
|
|
foreach ($files as $file) { |
|
328
|
|
|
if (is_file($classPath . '/actions/' . $file) && strpos($file, '.json') !== false) { |
|
329
|
|
|
$this->createActionFromJsonConfig($classPath . '/actions/' . $file); |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
|