TYPO3  7.6
AbstractConditionViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\ViewHelper;
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
17 
39 {
45  private $childNodes = array();
46 
53  public function setChildNodes(array $childNodes)
54  {
55  $this->childNodes = $childNodes;
56  }
57 
61  public function __construct()
62  {
63  $this->registerArgument('then', 'mixed', 'Value to be returned if the condition if met.', false);
64  $this->registerArgument('else', 'mixed', 'Value to be returned if the condition if not met.', false);
65  }
66 
73  public function render()
74  {
75  if (static::evaluateCondition($this->arguments)) {
76  return $this->renderThenChild();
77  } else {
78  return $this->renderElseChild();
79  }
80  }
81 
90  protected function renderThenChild()
91  {
92  $hasEvaluated = true;
93  $result = static::renderStaticThenChild($this->arguments, $hasEvaluated);
94  if ($hasEvaluated) {
95  return $result;
96  }
97 
98  $elseViewHelperEncountered = false;
99  foreach ($this->childNodes as $childNode) {
100  if ($childNode instanceof ViewHelperNode
101  && $childNode->getViewHelperClassName() === ThenViewHelper::class) {
102  $data = $childNode->evaluate($this->renderingContext);
103  return $data;
104  }
105  if ($childNode instanceof ViewHelperNode
106  && $childNode->getViewHelperClassName() === ElseViewHelper::class) {
107  $elseViewHelperEncountered = true;
108  }
109  }
110 
111  if ($elseViewHelperEncountered) {
112  return '';
113  } else {
114  return $this->renderChildren();
115  }
116  }
117 
126  protected static function renderStaticThenChild($arguments, &$hasEvaluated)
127  {
128  if (isset($arguments['then'])) {
129  return $arguments['then'];
130  }
131  if (isset($arguments['__thenClosure'])) {
132  $thenClosure = $arguments['__thenClosure'];
133  return $thenClosure();
134  } elseif (isset($arguments['__elseClosure'])) {
135  return '';
136  }
137 
138  $hasEvaluated = false;
139  }
140 
149  protected function renderElseChild()
150  {
151  $hasEvaluated = true;
152  $result = static::renderStaticElseChild($this->arguments, $hasEvaluated);
153  if ($hasEvaluated) {
154  return $result;
155  }
156 
157  foreach ($this->childNodes as $childNode) {
158  if ($childNode instanceof ViewHelperNode
159  && $childNode->getViewHelperClassName() === ElseViewHelper::class) {
160  return $childNode->evaluate($this->renderingContext);
161  }
162  }
163 
164  return '';
165  }
166 
167 
176  protected static function renderStaticElseChild($arguments, &$hasEvaluated)
177  {
178  if (isset($arguments['else'])) {
179  return $arguments['else'];
180  }
181  if (isset($arguments['__elseClosure'])) {
182  $elseClosure = $arguments['__elseClosure'];
183  return $elseClosure();
184  }
185 
186  $hasEvaluated = false;
187  }
188 
201  public function compile($argumentsVariableName, $renderChildrenClosureVariableName, &$initializationPhpCode, \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode, \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler)
202  {
203  foreach ($syntaxTreeNode->getChildNodes() as $childNode) {
204  if ($childNode instanceof ViewHelperNode
205  && $childNode->getViewHelperClassName() === ThenViewHelper::class) {
206  $childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode);
207  $initializationPhpCode .= sprintf('%s[\'__thenClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . LF;
208  }
209  if ($childNode instanceof ViewHelperNode
210  && $childNode->getViewHelperClassName() === ElseViewHelper::class) {
211  $childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode);
212  $initializationPhpCode .= sprintf('%s[\'__elseClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . LF;
213  }
214  }
215 
216  return sprintf('%s::renderStatic(%s, %s, $renderingContext)',
217  get_class($this), $argumentsVariableName, $renderChildrenClosureVariableName);
218  }
219 
230  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)
231  {
232  $hasEvaluated = true;
233  if (static::evaluateCondition($arguments)) {
234  $result = static::renderStaticThenChild($arguments, $hasEvaluated);
235  if ($hasEvaluated) {
236  return $result;
237  }
238 
239  return $renderChildrenClosure();
240  } else {
241  $result = static::renderStaticElseChild($arguments, $hasEvaluated);
242  if ($hasEvaluated) {
243  return $result;
244  }
245  }
246 
247  return '';
248  }
249 
256  protected static function evaluateCondition($arguments = null)
257  {
258  return (isset($arguments['condition']) && $arguments['condition']);
259  }
260 }