-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractAdapter.php
More file actions
291 lines (242 loc) · 7.41 KB
/
AbstractAdapter.php
File metadata and controls
291 lines (242 loc) · 7.41 KB
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
<?php
/**
* This file is part of Blitz PHP framework.
*
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace BlitzPHP\View\Adapters;
use BlitzPHP\Contracts\Autoloader\LocatorInterface;
use BlitzPHP\Contracts\View\RendererInterface;
use BlitzPHP\Exceptions\ViewException;
use BlitzPHP\Utilities\Helpers;
use BlitzPHP\View\ViewDecoratorTrait;
abstract class AbstractAdapter implements RendererInterface
{
use ViewDecoratorTrait;
/**
* Données mises à la disposition des vues.
*
* @var array
*/
protected $data = [];
/**
* Les variables de rendu
*
* @var array
*/
protected $renderVars = [];
/**
* Le répertoire de base dans lequel rechercher nos vues.
*
* @var string
*/
protected $viewPath = '';
/**
* Extension des fichiers de vue
*/
protected string $ext = '';
/**
* Instance de Locator lorsque nous devons tenter de trouver une vue qui n'est pas à l'emplacement standard.
*/
protected LocatorInterface $locator;
/**
* Le nom de la mise en page utilisée, le cas échéant.
* Défini par la méthode "extend" utilisée dans les vues.
*
* @var string|null
*/
protected $layout;
/**
* Les statistiques sur nos performances ici
*
* @var array
*/
protected $performanceData = [];
/**
* {@inheritDoc}
*
* @param array $config Configuration actuelle de l'adapter
* @param string|null $viewPath Dossier principal dans lequel les vues doivent être cherchées
* @param bool $debug Devrions-nous stocker des informations sur les performances ?
*/
public function __construct(protected array $config, $viewPath = null, protected bool $debug = BLITZ_DEBUG)
{
helper('assets');
if (is_string($viewPath) && is_dir($viewPath = rtrim($viewPath, '\\/ ') . DS)) {
$this->viewPath = $viewPath;
}
$this->locator = service('locator');
$this->ext = preg_replace('#^\.#', '', $config['extension'] ?? $this->ext);
}
/**
* {@inheritDoc}
*/
public function setData(array $data = [], ?string $context = null): self
{
if ($context !== null && $context !== '' && $context !== '0') {
$data = esc($data, $context);
}
$this->data = $data;
return $this;
}
/**
* {@inheritDoc}
*/
public function getData(): array
{
return $this->data;
}
/**
* {@inheritDoc}
*/
public function addData(array $data = [], ?string $context = null): self
{
if ($context !== null && $context !== '' && $context !== '0') {
$data = esc($data, $context);
}
$this->data = array_merge($this->data, $data);
return $this;
}
/**
* {@inheritDoc}
*/
public function setVar(string $name, $value = null, ?string $context = null): self
{
if ($context !== null && $context !== '' && $context !== '0') {
$value = esc($value, $context);
}
$this->data[$name] = $value;
return $this;
}
/**
* Définit plusieurs éléments de données de vue à la fois.
*/
public function with(array|string $key, mixed $value = null, ?string $context = null): self
{
if (is_array($key)) {
$context = $value;
} else {
$key = [$key => $value];
}
return $this->addData($key, $context);
}
/**
* {@inheritDoc}
*/
public function resetData(): self
{
$this->data = [];
return $this;
}
/**
* {@inheritDoc}
*/
public function setLayout(?string $layout): self
{
$this->layout = $layout;
return $this;
}
/**
* {@inheritDoc}
*/
public function renderString(string $view, ?array $options = null, bool $saveData = false): string
{
return $this->render($view, $options, $saveData);
}
/**
* {@inheritDoc}
*/
public function getPerformanceData(): array
{
return $this->performanceData;
}
/**
* Consigne les données de performances pour le rendu d'une vue.
*/
protected function logPerformance(float $start, float $end, string $view)
{
if ($this->debug) {
$this->performanceData[] = [
'start' => $start,
'end' => $end,
'view' => $view,
];
}
}
/**
* Recupère ou modifie le titre de la page
*
* @return self|string
*/
public function title(?string $title = null)
{
if ($title === null || $title === '' || $title === '0') {
return $this->getData()['title'] ?? '';
}
return $this->setVar('title', $title);
}
/**
* Recupère ou modifie les elements de balises "meta"
*
* @return self|string
*/
public function meta(string $key, ?string $value = null)
{
$meta = $this->getData()['meta'] ?? [];
if ($value === null || $value === '' || $value === '0') {
return $meta[$key] ?? '';
}
$meta[$key] = esc($value);
return $this->setVar('meta', $meta);
}
/**
* {@inheritDoc}
*/
public function exists(string $view, ?string $ext = null, array $options = []): bool
{
try {
return str_contains($this->getRenderedFile($options, $view, $ext), 'Views');
} catch (ViewException) {
return false;
}
}
/**
* Recupere le chemin absolue du fichier de vue a rendre
*/
protected function getRenderedFile(?array $options, string $view, ?string $ext = null): string
{
$options = (array) $options;
$ext ??= $this->ext;
$viewPath = $options['viewPath'] ?? $this->viewPath;
$file = ! empty($viewPath) ? str_replace('/', DS, rtrim($viewPath, '/\\') . DS . ltrim($view, '/\\')) : $view;
$file = Helpers::ensureExt($file, $ext);
if (! is_file($file)) {
$file = $this->locator->locateFile($view, 'Views', $ext);
}
$file = realpath($file);
// locateFile renverra une chaîne vide si le fichier est introuvable.
if (! is_file($file)) {
throw ViewException::invalidFile($view);
}
return $file;
}
/**
* Construit la sortie en fonction d'un nom de fichier et de tout données déjà définies.
*
* Options valides :
* - cache Nombre de secondes à mettre en cache pour
* - cache_name Nom à utiliser pour le cache
*
* @param string $view Nom de fichier de la source de la vue
* @param array|null $options Réservé à des utilisations tierces car
* il peut être nécessaire de transmettre des
* informations supplémentaires à d'autres moteurs de modèles.
* @param bool|null $saveData Si vrai, enregistre les données pour les appels suivants,
* si faux, nettoie les données après affichage,
* si null, utilise le paramètre de configuration.
*/
abstract public function render(string $view, ?array $options = null, ?bool $saveData = null): string;
}