-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPhpEngine.php
466 lines (396 loc) · 12.9 KB
/
PhpEngine.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating;
use Symfony\Component\Templating\Helper\HelperInterface;
use Symfony\Component\Templating\Loader\LoaderInterface;
use Symfony\Component\Templating\Storage\FileStorage;
use Symfony\Component\Templating\Storage\Storage;
use Symfony\Component\Templating\Storage\StringStorage;
trigger_deprecation('symfony/templating', '6.4', '"%s" is deprecated since version 6.4 and will be removed in 7.0. Use Twig instead.', PhpEngine::class);
/**
* PhpEngine is an engine able to render PHP templates.
*
* @implements \ArrayAccess<string, HelperInterface>
*
* @author Fabien Potencier <[email protected]>
*
* @deprecated since Symfony 6.4, use Twig instead
*/
class PhpEngine implements EngineInterface, \ArrayAccess
{
protected $loader;
protected $current;
/**
* @var HelperInterface[]
*/
protected $helpers = [];
protected $parents = [];
protected $stack = [];
protected $charset = 'UTF-8';
protected $cache = [];
protected $escapers = [];
protected static $escaperCache = [];
protected $globals = [];
protected $parser;
private Storage $evalTemplate;
private array $evalParameters;
/**
* @param HelperInterface[] $helpers An array of helper instances
*/
public function __construct(TemplateNameParserInterface $parser, LoaderInterface $loader, array $helpers = [])
{
$this->parser = $parser;
$this->loader = $loader;
$this->addHelpers($helpers);
$this->initializeEscapers();
foreach ($this->escapers as $context => $escaper) {
$this->setEscaper($context, $escaper);
}
}
/**
* @throws \InvalidArgumentException if the template does not exist
*/
public function render(string|TemplateReferenceInterface $name, array $parameters = []): string
{
$storage = $this->load($name);
$key = hash('sha256', serialize($storage));
$this->current = $key;
$this->parents[$key] = null;
// attach the global variables
$parameters = array_replace($this->getGlobals(), $parameters);
// render
if (false === $content = $this->evaluate($storage, $parameters)) {
throw new \RuntimeException(sprintf('The template "%s" cannot be rendered.', $this->parser->parse($name)));
}
// decorator
if ($this->parents[$key]) {
$slots = $this->get('slots');
$this->stack[] = $slots->get('_content');
$slots->set('_content', $content);
$content = $this->render($this->parents[$key], $parameters);
$slots->set('_content', array_pop($this->stack));
}
return $content;
}
public function exists(string|TemplateReferenceInterface $name): bool
{
try {
$this->load($name);
} catch (\InvalidArgumentException) {
return false;
}
return true;
}
public function supports(string|TemplateReferenceInterface $name): bool
{
$template = $this->parser->parse($name);
return 'php' === $template->get('engine');
}
/**
* Evaluates a template.
*
* @throws \InvalidArgumentException
*/
protected function evaluate(Storage $template, array $parameters = []): string|false
{
$this->evalTemplate = $template;
$this->evalParameters = $parameters;
unset($template, $parameters);
if (isset($this->evalParameters['this'])) {
throw new \InvalidArgumentException('Invalid parameter (this).');
}
if (isset($this->evalParameters['view'])) {
throw new \InvalidArgumentException('Invalid parameter (view).');
}
// the view variable is exposed to the require file below
$view = $this;
if ($this->evalTemplate instanceof FileStorage) {
extract($this->evalParameters, \EXTR_SKIP);
unset($this->evalParameters);
ob_start();
require $this->evalTemplate;
unset($this->evalTemplate);
return ob_get_clean();
} elseif ($this->evalTemplate instanceof StringStorage) {
extract($this->evalParameters, \EXTR_SKIP);
unset($this->evalParameters);
ob_start();
eval('; ?>'.$this->evalTemplate.'<?php ;');
unset($this->evalTemplate);
return ob_get_clean();
}
return false;
}
/**
* Gets a helper value.
*
* @throws \InvalidArgumentException if the helper is not defined
*/
public function offsetGet(mixed $name): HelperInterface
{
return $this->get($name);
}
/**
* Returns true if the helper is defined.
*/
public function offsetExists(mixed $name): bool
{
return isset($this->helpers[$name]);
}
/**
* Sets a helper.
*/
public function offsetSet(mixed $name, mixed $value): void
{
$this->set($name, $value);
}
/**
* Removes a helper.
*
* @throws \LogicException
*/
public function offsetUnset(mixed $name): void
{
throw new \LogicException(sprintf('You can\'t unset a helper (%s).', $name));
}
/**
* Adds some helpers.
*
* @param HelperInterface[] $helpers An array of helper
*
* @return void
*/
public function addHelpers(array $helpers)
{
foreach ($helpers as $alias => $helper) {
$this->set($helper, \is_int($alias) ? null : $alias);
}
}
/**
* Sets the helpers.
*
* @param HelperInterface[] $helpers An array of helper
*
* @return void
*/
public function setHelpers(array $helpers)
{
$this->helpers = [];
$this->addHelpers($helpers);
}
/**
* @return void
*/
public function set(HelperInterface $helper, ?string $alias = null)
{
$this->helpers[$helper->getName()] = $helper;
if (null !== $alias) {
$this->helpers[$alias] = $helper;
}
$helper->setCharset($this->charset);
}
/**
* Returns true if the helper if defined.
*/
public function has(string $name): bool
{
return isset($this->helpers[$name]);
}
/**
* Gets a helper value.
*
* @throws \InvalidArgumentException if the helper is not defined
*/
public function get(string $name): HelperInterface
{
if (!isset($this->helpers[$name])) {
throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
}
return $this->helpers[$name];
}
/**
* Decorates the current template with another one.
*
* @return void
*/
public function extend(string $template)
{
$this->parents[$this->current] = $template;
}
/**
* Escapes a string by using the current charset.
*/
public function escape(mixed $value, string $context = 'html'): mixed
{
if (is_numeric($value)) {
return $value;
}
// If we deal with a scalar value, we can cache the result to increase
// the performance when the same value is escaped multiple times (e.g. loops)
if (\is_scalar($value)) {
if (!isset(self::$escaperCache[$context][$value])) {
self::$escaperCache[$context][$value] = $this->getEscaper($context)($value);
}
return self::$escaperCache[$context][$value];
}
return $this->getEscaper($context)($value);
}
/**
* Sets the charset to use.
*
* @return void
*/
public function setCharset(string $charset)
{
if ('UTF8' === $charset = strtoupper($charset)) {
$charset = 'UTF-8'; // iconv on Windows requires "UTF-8" instead of "UTF8"
}
$this->charset = $charset;
foreach ($this->helpers as $helper) {
$helper->setCharset($this->charset);
}
}
/**
* Gets the current charset.
*/
public function getCharset(): string
{
return $this->charset;
}
/**
* Adds an escaper for the given context.
*
* @return void
*/
public function setEscaper(string $context, callable $escaper)
{
$this->escapers[$context] = $escaper;
self::$escaperCache[$context] = [];
}
/**
* Gets an escaper for a given context.
*
* @throws \InvalidArgumentException
*/
public function getEscaper(string $context): callable
{
if (!isset($this->escapers[$context])) {
throw new \InvalidArgumentException(sprintf('No registered escaper for context "%s".', $context));
}
return $this->escapers[$context];
}
/**
* @return void
*/
public function addGlobal(string $name, mixed $value)
{
$this->globals[$name] = $value;
}
/**
* Returns the assigned globals.
*/
public function getGlobals(): array
{
return $this->globals;
}
/**
* Initializes the built-in escapers.
*
* Each function specifies a way for applying a transformation to a string
* passed to it. The purpose is for the string to be "escaped" so it is
* suitable for the format it is being displayed in.
*
* For example, the string: "It's required that you enter a username & password.\n"
* If this were to be displayed as HTML it would be sensible to turn the
* ampersand into '&' and the apostrophe into '&aps;'. However if it were
* going to be used as a string in JavaScript to be displayed in an alert box
* it would be right to leave the string as-is, but c-escape the apostrophe and
* the new line.
*
* For each function there is a define to avoid problems with strings being
* incorrectly specified.
*
* @return void
*/
protected function initializeEscapers()
{
$flags = \ENT_QUOTES | \ENT_SUBSTITUTE;
$this->escapers = [
'html' =>
/**
* Runs the PHP function htmlspecialchars on the value passed.
*
* @param string $value The value to escape
*
* @return string
*/
fn ($value) => // Numbers and Boolean values get turned into strings which can cause problems
// with type comparisons (e.g. === or is_int() etc).
\is_string($value) ? htmlspecialchars($value, $flags, $this->getCharset(), false) : $value,
'js' =>
/**
* A function that escape all non-alphanumeric characters
* into their \xHH or \uHHHH representations.
*
* @param string $value The value to escape
*
* @return string
*/
function ($value) {
if ('UTF-8' != $this->getCharset()) {
$value = iconv($this->getCharset(), 'UTF-8', $value);
}
$callback = function ($matches) {
$char = $matches[0];
// \xHH
if (!isset($char[1])) {
return '\\x'.substr('00'.bin2hex($char), -2);
}
// \uHHHH
$char = iconv('UTF-8', 'UTF-16BE', $char);
return '\\u'.substr('0000'.bin2hex($char), -4);
};
if (null === $value = preg_replace_callback('#[^\p{L}\p{N} ]#u', $callback, $value)) {
throw new \InvalidArgumentException('The string to escape is not a valid UTF-8 string.');
}
if ('UTF-8' != $this->getCharset()) {
$value = iconv('UTF-8', $this->getCharset(), $value);
}
return $value;
},
];
self::$escaperCache = [];
}
/**
* Gets the loader associated with this engine.
*/
public function getLoader(): LoaderInterface
{
return $this->loader;
}
/**
* Loads the given template.
*
* @throws \InvalidArgumentException if the template cannot be found
*/
protected function load(string|TemplateReferenceInterface $name): Storage
{
$template = $this->parser->parse($name);
$key = $template->getLogicalName();
if (isset($this->cache[$key])) {
return $this->cache[$key];
}
$storage = $this->loader->load($template);
if (false === $storage) {
throw new \InvalidArgumentException(sprintf('The template "%s" does not exist.', $template));
}
return $this->cache[$key] = $storage;
}
}