-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.php
390 lines (334 loc) · 9.72 KB
/
View.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* This file is part of Piko - Web micro framework
*
* @copyright 2019-2022 Sylvain PHILIP
* @license LGPL-3.0; see LICENSE.txt
* @link https://github.com/piko-framework/piko
*/
declare(strict_types=1);
namespace Piko;
use Piko\View\Event\AfterEndBodyEvent;
use Piko\View\Event\AfterHeadEvent;
use Piko\View\Event\AfterRenderEvent;
use Piko\View\Event\BeforeEndBodyEvent;
use Piko\View\Event\BeforeHeadEvent;
use Piko\View\Event\BeforeRenderEvent;
use Exception;
use RuntimeException;
/**
* Base application view.
*
* @method string getUrl(string $route, array<mixed> $params, boolean $absolute) Convenient method to convert
* a route to an url (see Router::getUrl()). This method is implemented as a behavior and can be overriden.
*
* @author Sylvain PHILIP <contact@sphilip.com>
*/
class View
{
use BehaviorTrait;
use EventHandlerTrait;
/**
* Head position.
*
* @var integer
*/
public const POS_HEAD = 1;
/**
* End of body position.
*
* @var integer
*/
public const POS_END = 2;
/**
* View filename extension
*
* @var string
*/
public $extension = 'php';
/**
* View parameters.
*
* @var array<mixed>
*/
public $params = [];
/**
* The page title
*
* @var string
*/
public $title = '';
/**
* The registered CSS code blocks.
*
* @var array<string>
* @see View::registerCss()
*/
public $css = [];
/**
* The registered CSS files.
*
* @var array<string>
* @see View::registerCssFile()
*/
public $cssFiles = [];
/**
* The registered JS code blocks
*
* @var array<string[]>
* @see View::registerJs()
*/
public $js = [];
/**
* The registered JS files.
*
* @var array<string[]>
* @see View::registerJsFile()
*/
public $jsFiles = [];
/**
* Directories where to find view files.
*
* @var array<string>
*/
public $paths = [];
/**
* Parts of the head.
*
* @var array<string>
*/
public $head = [];
/**
* Parts of the end of the body.
*
* @var array<string>
*/
public $endBody = [];
/**
* Theme map configuration.
*
* A key paired array where each key represents
* a path to override and the value, the mapped path. The value could be either a string
* or an array of path and in this case, it may be possibe to use child themes.
* Configuration example :
* ```
* ...
* 'view' => [
* 'class' => 'piko\View',
* 'themeMap' => [
* '@app/modules/site/views' => [
* '@app/themes/child-theme',
* '@app/themes/parent-theme',
* ],
* '@app/modules/admin/views' => '@app/themes/piko/admin',
* ],
* ],
* ```
*
* @var array<string, string|array<string>>
*/
public $themeMap = [];
/**
* The charset encoding used in the view.
*
* @var string
*/
public $charset = 'UTF-8';
/**
* @param array<string, mixed> $config
*/
public function __construct(array $config = [])
{
\Piko::configureObject($this, $config);
}
/**
* Assemble html in the head position.
*
* @return string The head html.
*/
protected function head(): string
{
if (!empty($this->cssFiles)) {
foreach ($this->cssFiles as $url) {
$this->head[] = '<link href="' . $url . '" rel="stylesheet">';
}
}
if (!empty($this->css)) {
$this->head[] = '<style>';
foreach ($this->css as $css) {
$this->head[] = $css;
}
$this->head[] = '</style>';
}
if (!empty($this->jsFiles[self::POS_HEAD])) {
foreach ($this->jsFiles[self::POS_HEAD] as $url) {
$this->head[] = '<script src="' . $url . '"></script>';
}
}
if (!empty($this->js[self::POS_HEAD])) {
$this->head[] = '<script>';
foreach ($this->js[self::POS_HEAD] as $js) {
$this->head[] = $js;
}
$this->head[] = '</script>';
}
$beforeEvent = $this->trigger(new BeforeHeadEvent());
assert($beforeEvent instanceof BeforeHeadEvent);
$afterEvent = $this->trigger(new AfterHeadEvent());
assert($afterEvent instanceof AfterHeadEvent);
return $beforeEvent->output . implode("\n", $this->head) . $afterEvent->output;
}
/**
* Assemble html in the end of the body position.
*
* @return string The end of the body html.
*/
protected function endBody(): string
{
if (!empty($this->jsFiles[self::POS_END])) {
foreach ($this->jsFiles[self::POS_END] as $url) {
$this->endBody[] = '<script src="' . $url . '"></script>';
}
}
if (!empty($this->js[self::POS_END])) {
$this->endBody[] = '<script>' . PHP_EOL;
foreach ($this->js[self::POS_END] as $js) {
$this->endBody[] = $js;
}
$this->endBody[] = '</script>' . PHP_EOL;
}
$beforeEvent = $this->trigger(new BeforeEndBodyEvent());
assert($beforeEvent instanceof BeforeEndBodyEvent);
$afterEvent = $this->trigger(new AfterEndBodyEvent());
assert($afterEvent instanceof AfterEndBodyEvent);
return $beforeEvent->output . implode("\n", $this->endBody) . $afterEvent->output;
}
/**
* Register a script url.
*
* @param string $url The script url.
* @param int $position The view position where to insert the script (default at the end of the body).
* @param string $key An optional identifier
* @return void
*/
public function registerJsFile(string $url, int $position = self::POS_END, string $key = null): void
{
$key = $key ?: md5($url);
$this->jsFiles[$position][$key] = $url;
}
/**
* Register a stylesheet url.
*
* @param string $url The stylesheet url.
* @return void
*/
public function registerCSSFile(string $url): void
{
$this->cssFiles[] = $url;
}
/**
* Register a script.
*
* @param string $js The script code.
* @param int $position The view position where to insert the script (default at the end of the body).
* @param string $key An optional identifier
* @return void
*/
public function registerJs(string $js, int $position = self::POS_END, string $key = null): void
{
$key = $key ?: md5($js);
$this->js[$position][$key] = $js;
}
/**
* Register css code.
*
* @param string $css The css code.
* @param string $key An optional identifier
* @return void
*/
public function registerCSS(string $css, string $key = null): void
{
$key = $key ?: md5($css);
$this->css[$key] = $css;
}
/**
* Escape HTML special characters.
*
* @param string $string Dirty html.
* @return string Clean html.
*/
public function escape(string $string): string
{
return htmlentities($string, ENT_COMPAT | ENT_HTML5, $this->charset);
}
/**
* Retrieve a view file.
*
* @param string $viewName The view name (without extension).
* @throws RuntimeException if view file not found.
* @return string The path of the view file.
*/
protected function findFile(string $viewName): string
{
foreach ($this->paths as $path) {
if (file_exists(\Piko::getAlias($path) . '/' . $viewName . '.' . $this->extension)) {
return \Piko::getAlias($path) . '/' . $viewName . '.' . $this->extension;
}
}
throw new RuntimeException("Cannot find the view file for the viewname: $viewName");
}
/**
* Try to find an override of the file in a theme.
*
* @param string $path The file path
* @return string The overriden or not file path
*/
protected function applyTheme(string $path): string
{
if (!empty($this->themeMap)) {
foreach ($this->themeMap as $from => $tos) {
$from = (string) \Piko::getAlias($from);
if (strpos($path, $from) === 0) {
$n = strlen($from);
foreach ((array) $tos as $to) {
$to = \Piko::getAlias($to);
$file = $to . substr($path, $n);
if (is_file($file)) {
return $file;
}
}
}
}
}
return $path;
}
/**
* Render the view.
*
* @param string $file The view file name.
* @param array<mixed> $model An array of data (name-value pairs) to transmit to the view file.
* @return string The view output.
*/
public function render(string $file, array $model = []): string
{
if (!file_exists($file)) {
$file = $this->findFile($file);
}
$file = $this->applyTheme($file);
$beforeRenderEvent = new BeforeRenderEvent($this, $file, $model);
$this->trigger($beforeRenderEvent);
extract($beforeRenderEvent->model, EXTR_OVERWRITE);
ob_start();
try {
require $beforeRenderEvent->file;
$output = (string) ob_get_contents();
} catch (Exception $e) {
throw $e;
} finally {
ob_end_clean();
}
$afterRenderEvent = new AfterRenderEvent($output);
$this->trigger($afterRenderEvent);
return (string) $afterRenderEvent->output;
}
}