-
Notifications
You must be signed in to change notification settings - Fork 91
/
AbstractExpressionNode.php
139 lines (126 loc) · 4.25 KB
/
AbstractExpressionNode.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
<?php
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
namespace TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\Expression;
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
use TYPO3Fluid\Fluid\Core\Parser;
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\AbstractNode;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
/**
* Base class for nodes based on (shorthand) expressions.
*/
abstract class AbstractExpressionNode extends AbstractNode implements ExpressionNodeInterface
{
/**
* Contents of the text node
*
* @var string
*/
protected $expression;
/**
* @var array
*/
protected $matches = [];
/**
* Constructor.
*
* @param string $expression The original expression that created this node.
* @param array $matches Matches extracted from expression
* @throws Parser\Exception
*/
public function __construct($expression, array $matches)
{
$this->expression = trim($expression, " \t\n\r\0\x0b");
$this->matches = $matches;
}
/**
* Evaluates the expression stored in this node, in the context of $renderingcontext.
*
* @param RenderingContextInterface $renderingContext
* @return string the text stored in this node/subtree.
*/
public function evaluate(RenderingContextInterface $renderingContext)
{
try {
return static::evaluateExpression($renderingContext, $this->expression, $this->matches);
} catch (ExpressionException $exception) {
return $renderingContext->getErrorHandler()->handleExpressionError($exception);
}
}
/**
* Compiles the ExpressionNode, returning an array with
* exactly two keys which contain strings:
*
* - "initialization" which contains variable initializations
* - "execution" which contains the execution (that uses the variables)
*
* The expression and matches can be read from the local
* instance - and the RenderingContext and other APIs
* can be accessed via the TemplateCompiler.
*
* @param TemplateCompiler $templateCompiler
* @return array
*/
public function compile(TemplateCompiler $templateCompiler)
{
$handlerClass = get_class($this);
$expressionVariable = $templateCompiler->variableName('string');
$matchesVariable = $templateCompiler->variableName('array');
$initializationPhpCode = sprintf('// Rendering %s node' . chr(10), $handlerClass);
$initializationPhpCode .= sprintf('%s = \'%s\';', $expressionVariable, $this->getExpression()) . chr(10);
$initializationPhpCode .= sprintf('%s = %s;', $matchesVariable, var_export($this->getMatches(), true)) . chr(10);
return [
'initialization' => $initializationPhpCode,
'execution' => sprintf(
'\%s::evaluateExpression($renderingContext, %s, %s)',
$handlerClass,
$expressionVariable,
$matchesVariable,
),
];
}
/**
* Getter for returning the expression before parsing.
*
* @return string
*/
public function getExpression()
{
return $this->expression;
}
/**
* @return array
*/
public function getMatches()
{
return $this->matches;
}
/**
* @param string $part
* @return string
*/
protected static function trimPart($part)
{
return trim($part, " \t\n\r\0\x0b{}");
}
/**
* @param mixed $candidate
* @param RenderingContextInterface $renderingContext
* @return mixed
*/
protected static function getTemplateVariableOrValueItself($candidate, RenderingContextInterface $renderingContext)
{
$variables = $renderingContext->getVariableProvider()->getAll();
$standardVariableProvider = new StandardVariableProvider();
$standardVariableProvider->setSource($variables);
$suspect = $standardVariableProvider->getByPath($candidate);
return $suspect ?? $candidate;
}
public function convert(TemplateCompiler $templateCompiler): array
{
return $this->compile($templateCompiler);
}
}