TYPO3 CMS  TYPO3_7-6
AbstractConditionViewHelper.php
Go to the documentation of this file.
1 <?php
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 
38 {
44  private $childNodes = [];
45 
52  public function setChildNodes(array $childNodes)
53  {
54  $this->childNodes = $childNodes;
55  }
56 
60  public function __construct()
61  {
62  $this->registerArgument('then', 'mixed', 'Value to be returned if the condition if met.', false);
63  $this->registerArgument('else', 'mixed', 'Value to be returned if the condition if not met.', false);
64  }
65 
72  public function render()
73  {
74  if (static::evaluateCondition($this->arguments)) {
75  return $this->renderThenChild();
76  } else {
77  return $this->renderElseChild();
78  }
79  }
80 
89  protected function renderThenChild()
90  {
91  $hasEvaluated = true;
92  $result = static::renderStaticThenChild($this->arguments, $hasEvaluated);
93  if ($hasEvaluated) {
94  return $result;
95  }
96 
97  $elseViewHelperEncountered = false;
98  foreach ($this->childNodes as $childNode) {
99  if ($childNode instanceof ViewHelperNode
100  && $childNode->getViewHelperClassName() === ThenViewHelper::class) {
101  $data = $childNode->evaluate($this->renderingContext);
102  return $data;
103  }
104  if ($childNode instanceof ViewHelperNode
105  && $childNode->getViewHelperClassName() === ElseViewHelper::class) {
106  $elseViewHelperEncountered = true;
107  }
108  }
109 
110  if ($elseViewHelperEncountered) {
111  return '';
112  } else {
113  return $this->renderChildren();
114  }
115  }
116 
125  protected static function renderStaticThenChild($arguments, &$hasEvaluated)
126  {
127  if (isset($arguments['then'])) {
128  return $arguments['then'];
129  }
130  if (isset($arguments['__thenClosure'])) {
131  $thenClosure = $arguments['__thenClosure'];
132  return $thenClosure();
133  } elseif (isset($arguments['__elseClosure'])) {
134  return '';
135  }
136 
137  $hasEvaluated = false;
138  }
139 
148  protected function renderElseChild()
149  {
150  $hasEvaluated = true;
151  $result = static::renderStaticElseChild($this->arguments, $hasEvaluated);
152  if ($hasEvaluated) {
153  return $result;
154  }
155 
156  foreach ($this->childNodes as $childNode) {
157  if ($childNode instanceof ViewHelperNode
158  && $childNode->getViewHelperClassName() === ElseViewHelper::class) {
159  return $childNode->evaluate($this->renderingContext);
160  }
161  }
162 
163  return '';
164  }
165 
174  protected static function renderStaticElseChild($arguments, &$hasEvaluated)
175  {
176  if (isset($arguments['else'])) {
177  return $arguments['else'];
178  }
179  if (isset($arguments['__elseClosure'])) {
180  $elseClosure = $arguments['__elseClosure'];
181  return $elseClosure();
182  }
183 
184  $hasEvaluated = false;
185  }
186 
199  public function compile($argumentsVariableName, $renderChildrenClosureVariableName, &$initializationPhpCode, \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode, \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler)
200  {
201  foreach ($syntaxTreeNode->getChildNodes() as $childNode) {
202  if ($childNode instanceof ViewHelperNode
203  && $childNode->getViewHelperClassName() === ThenViewHelper::class) {
204  $childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode);
205  $initializationPhpCode .= sprintf('%s[\'__thenClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . LF;
206  }
207  if ($childNode instanceof ViewHelperNode
208  && $childNode->getViewHelperClassName() === ElseViewHelper::class) {
209  $childNodesAsClosure = $templateCompiler->wrapChildNodesInClosure($childNode);
210  $initializationPhpCode .= sprintf('%s[\'__elseClosure\'] = %s;', $argumentsVariableName, $childNodesAsClosure) . LF;
211  }
212  }
213 
214  return sprintf('%s::renderStatic(%s, %s, $renderingContext)',
215  get_class($this), $argumentsVariableName, $renderChildrenClosureVariableName);
216  }
217 
228  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)
229  {
230  $hasEvaluated = true;
231  if (static::evaluateCondition($arguments)) {
232  $result = static::renderStaticThenChild($arguments, $hasEvaluated);
233  if ($hasEvaluated) {
234  return $result;
235  }
236 
237  return $renderChildrenClosure();
238  } else {
239  $result = static::renderStaticElseChild($arguments, $hasEvaluated);
240  if ($hasEvaluated) {
241  return $result;
242  }
243  }
244 
245  return '';
246  }
247 
254  protected static function evaluateCondition($arguments = null)
255  {
256  return isset($arguments['condition']) && $arguments['condition'];
257  }
258 }
static renderStatic(array $arguments, \Closure $renderChildrenClosure, \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)
registerArgument($name, $type, $description, $required=false, $defaultValue=null)
compile($argumentsVariableName, $renderChildrenClosureVariableName, &$initializationPhpCode, \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $syntaxTreeNode, \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler $templateCompiler)