-
Notifications
You must be signed in to change notification settings - Fork 611
/
FallbackNameExpression.php
86 lines (79 loc) · 2.78 KB
/
FallbackNameExpression.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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\web\twig\nodes;
use craft\helpers\Template;
use Twig\Compiler;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
/**
* Class NamespaceNode
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.4.0
*/
class FallbackNameExpression extends NameExpression
{
public function __construct(string $name, array $attributes = [], int $lineno = 0)
{
$attributes += [
'name' => $name,
'is_defined_test' => false,
'ignore_strict_check' => false,
'always_defined' => false,
];
Node::__construct([], $attributes, $lineno);
}
public function compile(Compiler $compiler): void
{
// no special handling for _self/etc.,or always-defined variables
if ($this->isSpecial() || $this->getAttribute('always_defined')) {
parent::compile($compiler);
return;
}
$name = $this->getAttribute('name');
$compiler->addDebugInfo($this);
if ($this->getAttribute('is_defined_test')) {
$compiler
->raw('(array_key_exists(')
->string($name)
->raw(sprintf(', $context) || %s::fallbackExists(', Template::class))
->string($name)
->raw('))');
} elseif ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
$compiler
->raw('(isset($context[')
->string($name)
->raw(']) || array_key_exists(')
->string($name)
->raw(', $context) ? $context[')
->string($name)
->raw(sprintf('] : (%s::fallbackExists(', Template::class))
->string($name)
->raw(sprintf(') ? %s::fallback(', Template::class))
->string($name)
->raw(') : null))');
} else {
$compiler
->raw('(isset($context[')
->string($name)
->raw(']) || array_key_exists(')
->string($name)
->raw(', $context) ? $context[')
->string($name)
->raw(sprintf('] : (%s::fallbackExists(', Template::class))
->string($name)
->raw(sprintf(') ? %s::fallback(', Template::class))
->string($name)
->raw(') : (function () { throw new RuntimeError(\'Variable ')
->string($name)
->raw(' does not exist.\', ')
->repr($this->lineno)
->raw(', $this->source); })()')
->raw('))');
}
}
}