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\Autoloader\Locator; |
15
|
|
|
use BlitzPHP\Container\Services; |
16
|
|
|
use BlitzPHP\Exceptions\ViewException; |
17
|
|
|
use BlitzPHP\Utilities\Helpers; |
18
|
|
|
use BlitzPHP\View\RendererInterface; |
19
|
|
|
|
20
|
|
|
abstract class AbstractAdapter implements RendererInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Données mises à la disposition des vues. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $data = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Les variables de rendu |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $renderVars = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Le répertoire de base dans lequel rechercher nos vues. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $viewPath = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Instance de Locator lorsque nous devons tenter de trouver une vue qui n'est pas à l'emplacement standard. |
45
|
|
|
*/ |
46
|
|
|
protected ?Locator $locator = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Le nom de la mise en page utilisée, le cas échéant. |
50
|
|
|
* Défini par la méthode "extend" utilisée dans les vues. |
51
|
|
|
* |
52
|
|
|
* @var string|null |
53
|
|
|
*/ |
54
|
|
|
protected $layout; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Les statistiques sur nos performances ici |
58
|
|
|
* |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected $performanceData = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
* |
66
|
|
|
* @param array $config Configuration actuelle de l'adapter |
67
|
|
|
* @param bool $debug Devrions-nous stocker des informations sur les performances ? |
68
|
|
|
*/ |
69
|
|
|
public function __construct(protected array $config, $viewPathLocator = null, protected bool $debug = BLITZ_DEBUG) |
70
|
|
|
{ |
71
|
|
|
helper('assets'); |
72
|
|
|
|
73
|
|
|
if (! empty($viewPathLocator)) { |
74
|
|
|
if (is_string($viewPathLocator)) { |
75
|
|
|
$this->viewPath = rtrim($viewPathLocator, '\\/ ') . DS; |
76
|
|
|
} elseif ($viewPathLocator instanceof Locator) { |
77
|
|
|
$this->locator = $viewPathLocator; |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
|
|
$this->locator = Services::locator(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public function setData(array $data = [], ?string $context = null): self |
88
|
|
|
{ |
89
|
|
|
if ($context) { |
90
|
|
|
$data = esc($data, $context); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->data = $data; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
|
|
public function getData(): array |
102
|
|
|
{ |
103
|
|
|
return $this->data; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
|
|
public function addData(array $data = [], ?string $context = null): self |
110
|
|
|
{ |
111
|
|
|
if ($context) { |
112
|
|
|
$data = esc($data, $context); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->data = array_merge($this->data, $data); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritDoc} |
122
|
|
|
*/ |
123
|
|
|
public function setVar(string $name, $value = null, ?string $context = null): self |
124
|
|
|
{ |
125
|
|
|
if ($context) { |
126
|
|
|
$value = esc($value, $context); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->data[$name] = $value; |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Définit plusieurs éléments de données de vue à la fois. |
136
|
|
|
*/ |
137
|
|
|
public function with(array|string $key, mixed $value = null, ?string $context = null): self |
138
|
|
|
{ |
139
|
|
|
if (is_array($key)) { |
|
|
|
|
140
|
|
|
$context = $value; |
141
|
|
|
} else { |
142
|
|
|
$key = [$key => $value]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->addData($key, $context); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritDoc} |
150
|
|
|
*/ |
151
|
|
|
public function resetData(): self |
152
|
|
|
{ |
153
|
|
|
$this->data = []; |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritDoc} |
160
|
|
|
*/ |
161
|
|
|
public function setLayout(?string $layout): self |
162
|
|
|
{ |
163
|
|
|
$this->layout = $layout; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritDoc} |
170
|
|
|
*/ |
171
|
|
|
public function renderString(string $view, ?array $options = null, bool $saveData = false): string |
172
|
|
|
{ |
173
|
|
|
return $this->render($view, $options, $saveData); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritDoc} |
178
|
|
|
*/ |
179
|
|
|
public function getPerformanceData(): array |
180
|
|
|
{ |
181
|
|
|
return $this->performanceData; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Consigne les données de performances pour le rendu d'une vue. |
186
|
|
|
*/ |
187
|
|
|
protected function logPerformance(float $start, float $end, string $view) |
188
|
|
|
{ |
189
|
|
|
if ($this->debug) { |
190
|
|
|
$this->performanceData[] = [ |
191
|
|
|
'start' => $start, |
192
|
|
|
'end' => $end, |
193
|
|
|
'view' => $view, |
194
|
|
|
]; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Recupère ou modifie le titre de la page |
200
|
|
|
* |
201
|
|
|
* @return self|string |
202
|
|
|
*/ |
203
|
|
|
public function title(?string $title = null) |
204
|
|
|
{ |
205
|
|
|
if (empty($title)) { |
206
|
|
|
return $this->getData()['title'] ?? ''; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $this->setVar('title', $title); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Recupère ou modifie les elements de balises "meta" |
214
|
|
|
* |
215
|
|
|
* @return self|string |
216
|
|
|
*/ |
217
|
|
|
public function meta(string $key, ?string $value = null) |
218
|
|
|
{ |
219
|
|
|
$meta = $this->getData()['meta'] ?? []; |
220
|
|
|
|
221
|
|
|
if (empty($value)) { |
222
|
|
|
return $meta[$key] ?? ''; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$meta[$key] = esc($value); |
226
|
|
|
|
227
|
|
|
return $this->setVar('meta', $meta); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Recupere le chemin absolue du fichier de vue a rendre |
232
|
|
|
*/ |
233
|
|
|
protected function getRenderedFile(?array $options, string $view, string $ext = 'php'): string |
234
|
|
|
{ |
235
|
|
|
$options = (array) $options; |
236
|
|
|
|
237
|
|
|
$viewPath = $options['viewPath'] ?? $this->viewPath; |
238
|
|
|
if (! empty($viewPath)) { |
239
|
|
|
$file = str_replace('/', DS, rtrim($viewPath, '/\\') . DS . ltrim($view, '/\\')); |
240
|
|
|
} else { |
241
|
|
|
$file = $view; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$file = Helpers::ensureExt($file, $ext); |
245
|
|
|
|
246
|
|
|
if (! is_file($file) && $this->locator instanceof Locator) { |
247
|
|
|
$file = $this->locator->locateFile($view, 'Views', empty($ext) ? 'php' : $ext); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$file = realpath($file); |
|
|
|
|
251
|
|
|
|
252
|
|
|
// locateFile renverra une chaîne vide si le fichier est introuvable. |
253
|
|
|
if (! is_file($file)) { |
254
|
|
|
throw ViewException::invalidFile($view); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $file; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Construit la sortie en fonction d'un nom de fichier et de tout données déjà définies. |
262
|
|
|
* |
263
|
|
|
* Options valides : |
264
|
|
|
* - cache Nombre de secondes à mettre en cache pour |
265
|
|
|
* - cache_name Nom à utiliser pour le cache |
266
|
|
|
* |
267
|
|
|
* @param string $view Nom de fichier de la source de la vue |
268
|
|
|
* @param array|null $options Réservé à des utilisations tierces car |
269
|
|
|
* il peut être nécessaire de transmettre des |
270
|
|
|
* informations supplémentaires à d'autres moteurs de modèles. |
271
|
|
|
* @param bool|null $saveData Si vrai, enregistre les données pour les appels suivants, |
272
|
|
|
* si faux, nettoie les données après affichage, |
273
|
|
|
* si null, utilise le paramètre de configuration. |
274
|
|
|
*/ |
275
|
|
|
abstract public function render(string $view, ?array $options = null, ?bool $saveData = null): string; |
276
|
|
|
} |
277
|
|
|
|