forked from TYPO3/Fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitchViewHelper.php
More file actions
213 lines (194 loc) · 7.46 KB
/
SwitchViewHelper.php
File metadata and controls
213 lines (194 loc) · 7.46 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
<?php
namespace TYPO3Fluid\Fluid\ViewHelpers;
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\NodeInterface;
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* Switch ViewHelper which can be used to render content depending on a value or expression.
* Implements what a basic PHP ``switch()`` does.
*
* An optional default case can be specified which is rendered if none of the
* ``case`` conditions matches.
*
* Using this ViewHelper can be a sign of weak architecture. If you end up using it extensively
* you might want to consider restructuring your controllers/actions and/or use partials and sections.
* E.g. the above example could be achieved with :html:`<f:render partial="title.{person.gender}" />`
* and the partials "title.male.html", "title.female.html", ...
* Depending on the scenario this can be easier to extend and possibly contains less duplication.
*
* Examples
* ========
*
* Simple Switch statement
* -----------------------
*
* ::
*
* <f:switch expression="{person.gender}">
* <f:case value="male">Mr.</f:case>
* <f:case value="female">Mrs.</f:case>
* <f:defaultCase>Mr. / Mrs.</f:defaultCase>
* </f:switch>
*
* Output::
*
* "Mr.", "Mrs." or "Mr. / Mrs." (depending on the value of {person.gender})
*
* @api
*/
class SwitchViewHelper extends AbstractViewHelper
{
/**
* @var boolean
*/
protected $escapeOutput = false;
/**
* @var mixed
*/
protected $backupSwitchExpression = null;
/**
* @var boolean
*/
protected $backupBreakState = false;
/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('expression', 'mixed', 'Expression to switch', true);
}
/**
* @return string the rendered string
* @api
*/
public function render()
{
$expression = $this->arguments['expression'];
$this->backupSwitchState();
$variableContainer = $this->renderingContext->getViewHelperVariableContainer();
$variableContainer->addOrUpdate(SwitchViewHelper::class, 'switchExpression', $expression);
$variableContainer->addOrUpdate(SwitchViewHelper::class, 'break', false);
$content = $this->retrieveContentFromChildNodes($this->viewHelperNode->getChildNodes());
if ($variableContainer->exists(SwitchViewHelper::class, 'switchExpression')) {
$variableContainer->remove(SwitchViewHelper::class, 'switchExpression');
}
if ($variableContainer->exists(SwitchViewHelper::class, 'break')) {
$variableContainer->remove(SwitchViewHelper::class, 'break');
}
$this->restoreSwitchState();
return $content;
}
/**
* @param NodeInterface[] $childNodes
* @return mixed
*/
protected function retrieveContentFromChildNodes(array $childNodes)
{
$content = null;
$defaultCaseViewHelperNode = null;
foreach ($childNodes as $childNode) {
if ($this->isDefaultCaseNode($childNode)) {
$defaultCaseViewHelperNode = $childNode;
}
if (!$this->isCaseNode($childNode)) {
continue;
}
$content = $childNode->evaluate($this->renderingContext);
if ($this->viewHelperVariableContainer->get(SwitchViewHelper::class, 'break') === true) {
$defaultCaseViewHelperNode = null;
break;
}
}
if ($defaultCaseViewHelperNode !== null) {
$content = $defaultCaseViewHelperNode->evaluate($this->renderingContext);
}
return $content;
}
/**
* @param NodeInterface $node
* @return boolean
*/
protected function isDefaultCaseNode(NodeInterface $node)
{
return ($node instanceof ViewHelperNode && $node->getViewHelperClassName() === DefaultCaseViewHelper::class);
}
/**
* @param NodeInterface $node
* @return boolean
*/
protected function isCaseNode(NodeInterface $node)
{
return ($node instanceof ViewHelperNode && $node->getViewHelperClassName() === CaseViewHelper::class);
}
/**
* Backups "switch expression" and "break" state of a possible parent switch ViewHelper to support nesting
*
* @return void
*/
protected function backupSwitchState()
{
if ($this->renderingContext->getViewHelperVariableContainer()->exists(SwitchViewHelper::class, 'switchExpression')) {
$this->backupSwitchExpression = $this->renderingContext->getViewHelperVariableContainer()->get(SwitchViewHelper::class, 'switchExpression');
}
if ($this->renderingContext->getViewHelperVariableContainer()->exists(SwitchViewHelper::class, 'break')) {
$this->backupBreakState = $this->renderingContext->getViewHelperVariableContainer()->get(SwitchViewHelper::class, 'break');
}
}
/**
* Restores "switch expression" and "break" states that might have been backed up in backupSwitchState() before
*
* @return void
*/
protected function restoreSwitchState()
{
if ($this->backupSwitchExpression !== null) {
$this->renderingContext->getViewHelperVariableContainer()->addOrUpdate(SwitchViewHelper::class, 'switchExpression', $this->backupSwitchExpression);
}
if ($this->backupBreakState !== false) {
$this->renderingContext->getViewHelperVariableContainer()->addOrUpdate(SwitchViewHelper::class, 'break', true);
}
}
/**
* Compiles the node structure to a native switch
* statement which evaluates closures for each
* case comparison and renders child node closures
* only when value matches.
*
* @param string $argumentsName
* @param string $closureName
* @param string $initializationPhpCode
* @param ViewHelperNode $node
* @param TemplateCompiler $compiler
* @return string
*/
public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
{
$phpCode = 'call_user_func_array(function($arguments) use ($renderingContext, $self) {' . PHP_EOL .
'switch ($arguments[\'expression\']) {' . PHP_EOL;
foreach ($node->getChildNodes() as $childNode) {
if ($this->isDefaultCaseNode($childNode)) {
$childrenClosure = $compiler->wrapChildNodesInClosure($childNode);
$phpCode .= sprintf('default: return call_user_func(%s);', $childrenClosure) . PHP_EOL;
} elseif ($this->isCaseNode($childNode)) {
/** @var ViewHelperNode $childNode */
$valueClosure = $compiler->wrapViewHelperNodeArgumentEvaluationInClosure($childNode, 'value');
$childrenClosure = $compiler->wrapChildNodesInClosure($childNode);
$phpCode .= sprintf(
'case call_user_func(%s): return call_user_func(%s);',
$valueClosure,
$childrenClosure,
$compiler->getNodeConverter()->convert($childNode)
) . PHP_EOL;
}
}
$phpCode .= '}' . PHP_EOL;
$phpCode .= sprintf('}, array(%s))', $argumentsName);
return $phpCode;
}
}