1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Blitz PHP framework. |
5
|
|
|
* |
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BlitzPHP\View\Adapters; |
13
|
|
|
|
14
|
|
|
use BlitzPHP\Debug\Toolbar\Collectors\ViewsCollector; |
15
|
|
|
use BlitzPHP\Exceptions\ViewException; |
16
|
|
|
use BlitzPHP\Utilities\Helpers; |
17
|
|
|
use RuntimeException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class View |
21
|
|
|
*/ |
22
|
|
|
class NativeAdapter extends AbstractAdapter |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
*/ |
27
|
|
|
protected string $ext = 'php'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Fusionner les données enregistrées et les données utilisateur |
31
|
|
|
*/ |
32
|
|
|
protected $tempData; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Indique si les données doivent être enregistrées entre les rendus. |
36
|
|
|
* |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
protected $saveData; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Nombre de vues chargées |
43
|
|
|
*/ |
44
|
|
|
protected int $viewsCount = 0; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Contient les sections et leurs données. |
48
|
|
|
*/ |
49
|
|
|
protected array $sections = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Le nom de la section actuelle en cours de rendu, le cas échéant. |
53
|
|
|
* |
54
|
|
|
* @var string[] |
55
|
|
|
*/ |
56
|
|
|
protected array $sectionStack = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Contient les css charges a partie de la section actuelle en cours de rendu |
60
|
|
|
*/ |
61
|
|
|
protected array $_styles = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Contient les librairies css charges a partie de la section actuelle en cours de rendu |
65
|
|
|
*/ |
66
|
|
|
protected array $_lib_styles = []; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Contient les scripts js charges a partie de la section actuelle en cours de rendu |
70
|
|
|
*/ |
71
|
|
|
protected array $_scripts = []; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Contient les scripts js des librairies charges a partie de la section actuelle en cours de rendu |
75
|
|
|
*/ |
76
|
|
|
protected array $_lib_scripts = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function __construct(protected array $config, $viewPathLocator = null, protected bool $debug = BLITZ_DEBUG) |
82
|
|
|
{ |
83
|
28 |
|
parent::__construct($config, $viewPathLocator, $debug); |
84
|
|
|
|
85
|
28 |
|
$this->saveData = (bool) ($config['save_data'] ?? true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
|
|
public function render(string $view, ?array $options = null, ?bool $saveData = null): string |
92
|
|
|
{ |
93
|
16 |
|
$this->renderVars['start'] = microtime(true); |
94
|
16 |
|
$this->renderVars['view'] = $view; |
95
|
16 |
|
$this->renderVars['options'] = $options ?? []; |
96
|
|
|
|
97
|
|
|
// Stocke les résultats ici donc même si |
98
|
|
|
// plusieurs vues sont appelées dans une vue, ce ne sera pas le cas |
99
|
|
|
// nettoyez-le sauf si nous le voulons. |
100
|
16 |
|
$saveData ??= $this->saveData; |
101
|
|
|
|
102
|
|
|
// A-t-il été mis en cache ? |
103
|
|
|
if (isset($this->renderVars['options']['cache'])) { |
104
|
4 |
|
$cacheName = $this->renderVars['options']['cache_name'] ?? str_replace('.' . $this->ext, '', $this->renderVars['view']); |
105
|
4 |
|
$cacheName = str_replace(['\\', '/'], '', $cacheName); |
106
|
|
|
|
107
|
4 |
|
$this->renderVars['cacheName'] = $cacheName; |
108
|
|
|
|
109
|
|
|
if ($output = cache($this->renderVars['cacheName'])) { |
110
|
4 |
|
$this->logPerformance($this->renderVars['start'], microtime(true), $this->renderVars['view']); |
|
|
|
|
111
|
|
|
|
112
|
4 |
|
return $output; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
16 |
|
$this->renderVars['file'] = $this->getRenderedFile($this->renderVars['options'], $this->renderVars['view'], $this->ext); |
117
|
|
|
|
118
|
|
|
// Rendre nos données de vue disponibles pour la vue. |
119
|
16 |
|
$this->prepareTemplateData($saveData); |
120
|
|
|
|
121
|
|
|
// Enregistrer les variables actuelles |
122
|
16 |
|
$renderVars = $this->renderVars; |
123
|
|
|
|
124
|
|
|
$output = (function (): string { |
125
|
16 |
|
extract($this->tempData); |
126
|
16 |
|
ob_start(); |
127
|
16 |
|
include $this->renderVars['file']; |
128
|
|
|
|
129
|
16 |
|
return ob_get_clean() ?: ''; |
130
|
16 |
|
})(); |
131
|
|
|
|
132
|
|
|
// Lors de l'utilisation de mises en page, les données ont déjà été stockées |
133
|
|
|
// dans $this->sections, et aucune autre sortie valide |
134
|
|
|
// est autorisé dans $output donc nous allons l'écraser. |
135
|
|
|
if ($this->layout !== null && $this->sectionStack === []) { |
136
|
6 |
|
$layoutView = $this->layout; |
137
|
6 |
|
$this->layout = null; |
138
|
|
|
// Enregistrer les variables actuelles |
139
|
6 |
|
$renderVars = $this->renderVars; |
140
|
6 |
|
$output = $this->render($layoutView, array_merge($options ?? [], ['viewPath' => LAYOUT_PATH]), $saveData); |
141
|
|
|
} |
142
|
|
|
|
143
|
16 |
|
$this->logPerformance($this->renderVars['start'], microtime(true), $this->renderVars['view']); |
144
|
|
|
|
145
|
|
|
if (($this->debug && (! isset($options['debug']) || $options['debug'] === true))) { |
146
|
|
|
if (in_array(ViewsCollector::class, config('toolbar.collectors'), true)) { |
|
|
|
|
147
|
|
|
// Nettoyer nos noms de chemins pour les rendre un peu plus propres |
148
|
|
|
$this->renderVars['file'] = clean_path($this->renderVars['file']); |
149
|
|
|
$this->renderVars['file'] = ++$this->viewsCount . ' ' . $this->renderVars['file']; |
150
|
|
|
|
151
|
|
|
$output = '<!-- DEBUG-VIEW START ' . $this->renderVars['file'] . ' -->' . PHP_EOL |
152
|
|
|
. $output . PHP_EOL |
153
|
|
|
. '<!-- DEBUG-VIEW ENDED ' . $this->renderVars['file'] . ' -->' . PHP_EOL; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
16 |
|
$output = $this->decorate($output); |
158
|
|
|
|
159
|
|
|
// Faut-il mettre en cache ? |
160
|
|
|
if (isset($this->renderVars['options']['cache'])) { |
161
|
4 |
|
cache()->write($this->renderVars['cacheName'], $output, (int) $this->renderVars['options']['cache']); |
162
|
|
|
} |
163
|
|
|
|
164
|
16 |
|
$this->tempData = null; |
165
|
|
|
|
166
|
|
|
// Récupère les variables actuelles |
167
|
16 |
|
$this->renderVars = $renderVars; |
168
|
|
|
|
169
|
16 |
|
return $output; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritDoc} |
174
|
|
|
*/ |
175
|
|
|
public function renderString(string $view, ?array $options = null, ?bool $saveData = null): string |
176
|
|
|
{ |
177
|
6 |
|
$start = microtime(true); |
178
|
6 |
|
$saveData ??= $this->saveData; |
179
|
6 |
|
$this->prepareTemplateData($saveData); |
180
|
|
|
|
181
|
|
|
$output = (function (string $view): string { |
182
|
6 |
|
extract($this->tempData); |
183
|
6 |
|
ob_start(); |
184
|
6 |
|
eval('?>' . $view); |
|
|
|
|
185
|
|
|
|
186
|
6 |
|
return ob_get_clean() ?: ''; |
187
|
6 |
|
})($view); |
188
|
|
|
|
189
|
6 |
|
$this->logPerformance($start, microtime(true), $this->excerpt($view)); |
|
|
|
|
190
|
6 |
|
$this->tempData = null; |
191
|
|
|
|
192
|
6 |
|
return $output; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Extraire le premier bit d'une longue chaîne et ajouter des points de suspension |
197
|
|
|
*/ |
198
|
|
|
public function excerpt(string $string, int $length = 20): string |
199
|
|
|
{ |
200
|
8 |
|
return (strlen($string) > $length) ? substr($string, 0, $length - 3) . '...' : $string; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritDoc} |
205
|
|
|
*/ |
206
|
|
|
public function setData(array $data = [], ?string $context = null): self |
207
|
|
|
{ |
208
|
|
|
if ($context) { |
209
|
2 |
|
$data = esc($data, $context); |
210
|
|
|
} |
211
|
|
|
|
212
|
2 |
|
$this->tempData = $data; |
213
|
|
|
|
214
|
2 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritDoc} |
219
|
|
|
*/ |
220
|
|
|
public function addData(array $data = [], ?string $context = null): self |
221
|
|
|
{ |
222
|
|
|
if ($context) { |
223
|
8 |
|
$data = esc($data, $context); |
224
|
|
|
} |
225
|
|
|
|
226
|
8 |
|
$this->tempData ??= $this->data; |
227
|
8 |
|
$this->tempData = array_merge($this->tempData, $data); |
228
|
|
|
|
229
|
8 |
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritDoc} |
234
|
|
|
*/ |
235
|
|
|
public function setVar(string $name, $value = null, ?string $context = null): self |
236
|
|
|
{ |
237
|
|
|
if ($context) { |
238
|
2 |
|
$value = esc($value, $context); |
239
|
|
|
} |
240
|
|
|
|
241
|
22 |
|
$this->tempData ??= $this->data; |
242
|
22 |
|
$this->tempData[$name] = $value; |
243
|
|
|
|
244
|
22 |
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* {@inheritDoc} |
249
|
|
|
*/ |
250
|
|
|
public function getData(): array |
251
|
|
|
{ |
252
|
8 |
|
return $this->tempData ?? $this->data; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* {@inheritDoc} |
257
|
|
|
*/ |
258
|
|
|
public function resetData(): self |
259
|
|
|
{ |
260
|
2 |
|
$this->data = []; |
261
|
2 |
|
$this->tempData = []; |
262
|
|
|
|
263
|
2 |
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Spécifie que la vue actuelle doit étendre une mise en page existante. |
268
|
|
|
*/ |
269
|
|
|
public function extend(string $layout) |
270
|
|
|
{ |
271
|
6 |
|
$this->setLayout($layout); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Commence le contenu d'une section dans la mise en page. |
276
|
|
|
*/ |
277
|
|
|
public function start(string $name, ?string $content = null) |
278
|
|
|
{ |
279
|
|
|
if (null === $content) { |
280
|
6 |
|
$this->sectionStack[] = $name; |
281
|
|
|
|
282
|
6 |
|
ob_start(); |
283
|
|
|
} else { |
284
|
2 |
|
$this->sections[$name] = [$content]; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Commence le contenu d'une section dans la mise en page. |
290
|
|
|
* |
291
|
|
|
* @alias self::start() |
292
|
|
|
*/ |
293
|
|
|
public function section(string $name, ?string $content = null): void |
294
|
|
|
{ |
295
|
6 |
|
$this->start($name, $content); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Commence le contenu d'une section dans la mise en page. |
300
|
|
|
* |
301
|
|
|
* @alias self::start() |
302
|
|
|
*/ |
303
|
|
|
public function begin(string $name, ?string $content = null): void |
304
|
|
|
{ |
305
|
2 |
|
$this->start($name, $content); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Capture la dernière section |
310
|
|
|
* |
311
|
|
|
* @throws RuntimeException |
312
|
|
|
*/ |
313
|
|
|
public function stop() |
314
|
|
|
{ |
315
|
6 |
|
$contents = ob_get_clean(); |
316
|
|
|
|
317
|
|
|
if ($this->sectionStack === []) { |
318
|
2 |
|
throw new RuntimeException('View themes, no current section.'); |
319
|
|
|
} |
320
|
|
|
|
321
|
6 |
|
$section = array_pop($this->sectionStack); |
322
|
|
|
|
323
|
|
|
// Assurez-vous qu'un tableau existe afin que nous puissions stocker plusieurs entrées pour cela. |
324
|
|
|
if (! array_key_exists($section, $this->sections)) { |
325
|
6 |
|
$this->sections[$section] = []; |
326
|
|
|
} |
327
|
|
|
|
328
|
6 |
|
$this->sections[$section][] = $contents; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Capture la dernière section |
333
|
|
|
* |
334
|
|
|
* @throws RuntimeException |
335
|
|
|
* |
336
|
|
|
* @alias self::stop() |
337
|
|
|
*/ |
338
|
|
|
public function endSection(): void |
339
|
|
|
{ |
340
|
6 |
|
$this->stop(); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Capture la dernière section |
345
|
|
|
* |
346
|
|
|
* @throws RuntimeException |
347
|
|
|
* |
348
|
|
|
* @alias self::stop() |
349
|
|
|
*/ |
350
|
|
|
public function end(): void |
351
|
|
|
{ |
352
|
2 |
|
$this->stop(); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Restitue le contenu d'une section. |
357
|
|
|
*/ |
358
|
|
|
public function show(string $sectionName, bool $preserve = false) |
359
|
|
|
{ |
360
|
|
|
if (! isset($this->sections[$sectionName])) { |
361
|
4 |
|
echo ''; |
362
|
|
|
|
363
|
4 |
|
return; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
foreach ($this->sections[$sectionName] as $key => $contents) { |
367
|
6 |
|
echo $contents; |
368
|
|
|
|
369
|
|
|
if (false === $preserve) { |
370
|
6 |
|
unset($this->sections[$sectionName][$key]); |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Affichage rapide du contenu principal |
377
|
|
|
*/ |
378
|
|
|
public function renderView(): void |
379
|
|
|
{ |
380
|
2 |
|
$this->show('content'); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Utilisé dans les vues de mise en page pour inclure des vues supplémentaires. |
385
|
|
|
*/ |
386
|
|
|
public function insert(string $view, ?array $data = [], ?array $options = null, ?bool $saveData = null): string |
387
|
|
|
{ |
388
|
2 |
|
$view = Helpers::ensureExt($view, $this->ext); |
389
|
2 |
|
$view = str_replace(' ', '', $view); |
390
|
|
|
|
391
|
|
|
if ($view[0] !== '/') { |
392
|
2 |
|
$view = $this->retrievePartialPath($view); |
393
|
|
|
} |
394
|
|
|
|
395
|
2 |
|
return $this->addData($data)->render($view, $options, $saveData); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Utilisé dans les vues de mise en page pour inclure des vues supplémentaires. |
400
|
|
|
* |
401
|
|
|
* @alias self::insert() |
402
|
|
|
*/ |
403
|
|
|
public function include(string $view, ?array $data = [], ?array $options = null, ?bool $saveData = null): string |
404
|
|
|
{ |
405
|
2 |
|
return $this->insert($view, $data, $options, $saveData); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Utilisé dans les vues de mise en page pour inclure des vues supplémentaires lorsqu'une condition est remplie. |
410
|
|
|
*/ |
411
|
|
|
public function insertWhen(bool|callable $condition, string $view, ?array $data = [], ?array $options = null, ?bool $saveData = null): string |
412
|
|
|
{ |
413
|
|
|
if (is_callable($condition)) { |
414
|
2 |
|
$condition = $condition(); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
if (true === $condition) { |
418
|
2 |
|
return $this->insert($view, $data, $options, $saveData); |
419
|
|
|
} |
420
|
|
|
|
421
|
2 |
|
return ''; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Utilisé dans les vues de mise en page pour inclure des vues supplémentaires lorsqu'une condition est remplie. |
426
|
|
|
* |
427
|
|
|
* @alias self::insertWhen() |
428
|
|
|
*/ |
429
|
|
|
public function includeWhen(bool|callable $condition, string $view, ?array $data = [], ?array $options = null, ?bool $saveData = null): string |
430
|
|
|
{ |
431
|
2 |
|
return $this->insertWhen($condition, $view, $data, $options, $saveData); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Utilisé dans les vues de mise en page pour inclure des vues supplémentaires lorsqu'une condition n'est pas remplie. |
436
|
|
|
*/ |
437
|
|
|
public function insertUnless(bool|callable $condition, string $view, ?array $data = [], ?array $options = null, ?bool $saveData = null): string |
438
|
|
|
{ |
439
|
|
|
if (is_callable($condition)) { |
440
|
2 |
|
$condition = $condition(); |
441
|
|
|
} |
442
|
|
|
|
443
|
2 |
|
return $this->insertWhen(false === $condition, $view, $data, $options, $saveData); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Utilisé dans les vues de mise en page pour inclure des vues supplémentaires lorsqu'une condition n'est pas remplie. |
448
|
|
|
* |
449
|
|
|
* @alias self::insertUnless() |
450
|
|
|
*/ |
451
|
|
|
public function includeUnless(bool|callable $condition, string $view, ?array $data = [], ?array $options = null, ?bool $saveData = null): string |
452
|
|
|
{ |
453
|
2 |
|
return $this->insertUnless($condition, $view, $data, $options, $saveData); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Utilisé dans les vues de mise en page pour inclure des vues supplémentaires si elle existe. |
458
|
|
|
* |
459
|
|
|
* @alias self::insertIf() |
460
|
|
|
*/ |
461
|
|
|
public function includeIf(string $view, ?array $data = [], ?array $options = null, ?bool $saveData = null): string |
462
|
|
|
{ |
463
|
2 |
|
return $this->insertIf($view, $data, $options, $saveData); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Utilisé dans les vues de mise en page pour inclure des vues supplémentaires si elle existe. |
468
|
|
|
*/ |
469
|
|
|
public function insertIf(string $view, ?array $data = [], ?array $options = null, ?bool $saveData = null): string |
470
|
|
|
{ |
471
|
2 |
|
$view = Helpers::ensureExt($view, $this->ext); |
472
|
2 |
|
$view = str_replace(' ', '', $view); |
473
|
|
|
|
474
|
|
|
if ($view[0] !== '/') { |
475
|
2 |
|
$view = $this->retrievePartialPath($view); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
if (is_file($view)) { |
479
|
2 |
|
return $this->addData($data)->render($view, $options, $saveData); |
480
|
|
|
} |
481
|
|
|
|
482
|
2 |
|
return ''; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Utilisé dans les vues de mise en page pour inclure des vues supplémentaires si elle existe. |
487
|
|
|
* |
488
|
|
|
* @alias self::insertFisrt() |
489
|
|
|
*/ |
490
|
|
|
public function includeFirst(array $views, ?array $data = [], ?array $options = null, ?bool $saveData = null): string |
491
|
|
|
{ |
492
|
2 |
|
return $this->insertFirst($views, $data, $options, $saveData); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Utilisé dans les vues de mise en page pour inclure des vues supplémentaires si elle existe. |
497
|
|
|
*/ |
498
|
|
|
public function insertFirst(array $views, ?array $data = [], ?array $options = null, ?bool $saveData = null): string |
499
|
|
|
{ |
500
|
|
|
foreach ($views as $view) { |
501
|
|
|
if ('' !== $output = $this->includeIf($view, $data, $options, $saveData)) { |
502
|
2 |
|
return $output; |
503
|
|
|
} |
504
|
|
|
} |
505
|
|
|
|
506
|
2 |
|
throw ViewException::invalidFile(implode(' OR ', $views)); |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* Compile de manière conditionnelle une chaîne de classe CSS. |
511
|
|
|
*/ |
512
|
|
|
public function class(array $classes): string |
513
|
|
|
{ |
514
|
|
|
if ($classes === []) { |
515
|
2 |
|
return ''; |
516
|
|
|
} |
517
|
|
|
|
518
|
2 |
|
$class = []; |
519
|
|
|
|
520
|
|
|
foreach ($classes as $key => $value) { |
521
|
|
|
if (is_int($key)) { |
522
|
2 |
|
$class[] = $value; |
523
|
|
|
} elseif (true === $value) { |
524
|
2 |
|
$class[] = $key; |
525
|
|
|
} |
526
|
|
|
} |
527
|
|
|
|
528
|
2 |
|
return 'class="' . implode(' ', $class) . '"'; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Ajoute conditionnellement des styles CSS en ligne à un élément HTML |
533
|
|
|
*/ |
534
|
|
|
public function style(array $styles): string |
535
|
|
|
{ |
536
|
|
|
if ($styles === []) { |
537
|
2 |
|
return ''; |
538
|
|
|
} |
539
|
|
|
|
540
|
2 |
|
$style = []; |
541
|
|
|
|
542
|
|
|
foreach ($styles as $key => $value) { |
543
|
|
|
if (is_int($key)) { |
544
|
2 |
|
$style[] = $value . ';'; |
545
|
|
|
} elseif (true === $value) { |
546
|
2 |
|
$style[] = $key . ';'; |
547
|
|
|
} |
548
|
|
|
} |
549
|
|
|
|
550
|
2 |
|
return 'style="' . implode(' ', $style) . '"'; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* Utiliser pour indiquer facilement si une case à cocher HTML donnée est "cochée". |
555
|
|
|
* Indiquera que la case est cochée si la condition fournie est évaluée à true. |
556
|
|
|
*/ |
557
|
|
|
public function checked(bool|string $condition): string |
558
|
|
|
{ |
559
|
2 |
|
return true === filter_var($condition, FILTER_VALIDATE_BOOLEAN) ? 'checked="checked"' : ''; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* Utiliser pour indiquer si une option de sélection donnée doit être "sélectionnée". |
564
|
|
|
*/ |
565
|
|
|
public function selected(bool|string $condition): string |
566
|
|
|
{ |
567
|
2 |
|
return true === filter_var($condition, FILTER_VALIDATE_BOOLEAN) ? 'selected="selected"' : ''; |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* Utiliser pour indiquer si un élément donné doit être "désactivé". |
572
|
|
|
*/ |
573
|
|
|
public function disabled(bool|string $condition): string |
574
|
|
|
{ |
575
|
2 |
|
return true === filter_var($condition, FILTER_VALIDATE_BOOLEAN) ? 'disabled' : ''; |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Utiliser pour indiquer si un élément donné doit être "readonly". |
580
|
|
|
*/ |
581
|
|
|
public function readonly(bool|string $condition): string |
582
|
|
|
{ |
583
|
2 |
|
return true === filter_var($condition, FILTER_VALIDATE_BOOLEAN) ? 'readonly' : ''; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* Utiliser pour indiquer si un élément donné doit être "obligatoire". |
588
|
|
|
*/ |
589
|
|
|
public function required(bool|string $condition): string |
590
|
|
|
{ |
591
|
2 |
|
return true === filter_var($condition, FILTER_VALIDATE_BOOLEAN) ? 'required' : ''; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
/** |
595
|
|
|
* Génère un champ input caché à utiliser dans les formulaires générés manuellement. |
596
|
|
|
*/ |
597
|
|
|
public function csrf(?string $id): string |
598
|
|
|
{ |
599
|
|
|
return csrf_field($id); |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* Générer un champ de formulaire pour usurper le verbe HTTP utilisé par les formulaires. |
604
|
|
|
*/ |
605
|
|
|
public function method(string $method): string |
606
|
|
|
{ |
607
|
2 |
|
return method_field($method); |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Ajoute un fichier css de librairie a la vue |
612
|
|
|
*/ |
613
|
|
|
public function addLibCss(string ...$src): self |
614
|
|
|
{ |
615
|
2 |
|
$this->_lib_styles = array_merge($this->_lib_styles, $src); |
616
|
|
|
|
617
|
2 |
|
return $this; |
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
/** |
621
|
|
|
* Ajoute un fichier css a la vue |
622
|
|
|
*/ |
623
|
|
|
public function addCss(string ...$src): self |
624
|
|
|
{ |
625
|
2 |
|
$this->_styles = array_merge($this->_styles, $src); |
626
|
|
|
|
627
|
2 |
|
return $this; |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* Compile les fichiers de style de l'instance et genere les link:href vers ceux-ci |
632
|
|
|
*/ |
633
|
|
|
public function stylesBundle(): void |
634
|
|
|
{ |
635
|
|
|
if (! empty($this->_lib_styles)) { |
636
|
2 |
|
lib_styles(array_unique($this->_lib_styles)); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
if (! empty($this->_styles)) { |
640
|
2 |
|
styles(array_unique($this->_styles)); |
641
|
|
|
} |
642
|
|
|
|
643
|
2 |
|
$this->show('css'); |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* Ajoute un fichier js de librairie a la vue |
648
|
|
|
*/ |
649
|
|
|
public function addLibJs(string ...$src): self |
650
|
|
|
{ |
651
|
2 |
|
$this->_lib_scripts = array_merge($this->_lib_scripts, $src); |
652
|
|
|
|
653
|
2 |
|
return $this; |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
/** |
657
|
|
|
* Ajoute un fichier js a la vue |
658
|
|
|
*/ |
659
|
|
|
public function addJs(string ...$src): self |
660
|
|
|
{ |
661
|
2 |
|
$this->_scripts = array_merge($this->_scripts, $src); |
662
|
|
|
|
663
|
2 |
|
return $this; |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
/** |
667
|
|
|
* Compile les fichiers de script de l'instance et genere les link:href vers ceux-ci |
668
|
|
|
*/ |
669
|
|
|
public function scriptsBundle(): void |
670
|
|
|
{ |
671
|
|
|
if (! empty($this->_lib_scripts)) { |
672
|
2 |
|
lib_scripts(array_unique($this->_lib_scripts)); |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
if (! empty($this->_scripts)) { |
676
|
2 |
|
scripts(array_unique($this->_scripts)); |
677
|
|
|
} |
678
|
|
|
|
679
|
2 |
|
$this->show('js'); |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
protected function prepareTemplateData(bool $saveData): void |
683
|
|
|
{ |
684
|
18 |
|
$this->tempData ??= $this->data; |
685
|
|
|
|
686
|
|
|
if ($saveData) { |
687
|
16 |
|
$this->data = $this->tempData; |
688
|
|
|
} |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
private function retrievePartialPath(string $view): string |
692
|
|
|
{ |
693
|
2 |
|
$current_dir = pathinfo($this->renderVars['file'] ?? '', PATHINFO_DIRNAME); |
694
|
|
|
if (file_exists(rtrim($current_dir, DS) . DS . $view)) { |
|
|
|
|
695
|
2 |
|
$view = rtrim($current_dir, DS) . DS . $view; |
696
|
|
|
} elseif (file_exists(rtrim($current_dir, DS) . DS . 'partials' . DS . $view)) { |
697
|
2 |
|
$view = rtrim($current_dir, DS) . DS . 'partials' . DS . $view; |
698
|
|
|
} elseif (file_exists($this->viewPath . 'partials' . DS . $view)) { |
699
|
2 |
|
$view = $this->viewPath . 'partials' . DS . $view; |
700
|
|
|
} elseif (file_exists($this->viewPath . trim(dirname($current_dir), '/\\') . DS . $view)) { |
|
|
|
|
701
|
2 |
|
$view = $this->viewPath . trim(dirname($current_dir), '/\\') . DS . $view; |
702
|
|
|
} elseif (file_exists(VIEW_PATH . 'partials' . DS . $view)) { |
703
|
2 |
|
$view = VIEW_PATH . 'partials' . DS . $view; |
704
|
|
|
} elseif (file_exists(VIEW_PATH . trim(dirname($current_dir), '/\\') . DS . $view)) { |
705
|
2 |
|
$view = VIEW_PATH . trim(dirname($current_dir), '/\\') . DS . $view; |
706
|
|
|
} |
707
|
|
|
|
708
|
2 |
|
return $view; |
709
|
|
|
} |
710
|
|
|
} |
711
|
|
|
|