[BUGFIX] Use late static binding for compilable viewhelpers
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / SwitchViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
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
14 use TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler;
15 use TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode;
16 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
17 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\ChildNodeAccessInterface;
19 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
20
21 /**
22 * Switch view helper which can be used to render content depending on a value or expression.
23 * Implements what a basic switch()-PHP-method does.
24 *
25 * = Examples =
26 *
27 * <code title="Simple Switch statement">
28 * <f:switch expression="{person.gender}">
29 * <f:case value="male">Mr.</f:case>
30 * <f:case value="female">Mrs.</f:case>
31 * <f:case default="TRUE">Mrs. or Mr.</f:case>
32 * </f:switch>
33 * </code>
34 * <output>
35 * Mr. / Mrs. (depending on the value of {person.gender}) or if no value evaluates to TRUE, default case
36 * </output>
37 *
38 * Note: Using this view helper can be a sign of weak architecture. If you end up using it extensively
39 * you might want to consider restructuring your controllers/actions and/or use partials and sections.
40 * E.g. the above example could be achieved with <f:render partial="title.{person.gender}" /> and the partials
41 * "title.male.html", "title.female.html", ...
42 * Depending on the scenario this can be easier to extend and possibly contains less duplication.
43 *
44 * @api
45 */
46 class SwitchViewHelper extends AbstractViewHelper implements ChildNodeAccessInterface, CompilableInterface {
47
48 /**
49 * An array of \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode
50 * @var array
51 */
52 private $childNodes = array();
53
54 /**
55 * @var mixed
56 */
57 protected $backupSwitchExpression = NULL;
58
59 /**
60 * @var bool
61 */
62 protected $backupBreakState = FALSE;
63
64 /**
65 * Setter for ChildNodes - as defined in ChildNodeAccessInterface
66 *
67 * @param array $childNodes Child nodes of this syntax tree node
68 * @return void
69 */
70 public function setChildNodes(array $childNodes) {
71 $this->childNodes = $childNodes;
72 }
73
74 /**
75 * @param mixed $expression
76 * @return string the rendered string
77 * @api
78 */
79 public function render($expression) {
80 return static::renderStatic(
81 array(
82 'expression' => $expression
83 ),
84 $this->buildRenderChildrenClosure(),
85 $this->renderingContext
86 );
87 }
88
89 /**
90 * Default implementation for CompilableInterface. See CompilableInterface
91 * for a detailed description of this method.
92 *
93 * @param array $arguments
94 * @param \Closure $renderChildrenClosure
95 * @param RenderingContextInterface $renderingContext
96 * @return mixed
97 * @see \TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface
98 */
99 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
100 $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
101
102 $stackValue = array(
103 'expression' => $arguments['expression'],
104 'break' => FALSE
105 );
106
107 if ($viewHelperVariableContainer->exists(SwitchViewHelper::class, 'stateStack')) {
108 $stateStack = $viewHelperVariableContainer->get(SwitchViewHelper::class, 'stateStack');
109 } else {
110 $stateStack = array();
111 }
112 $stateStack[] = $stackValue;
113 $viewHelperVariableContainer->addOrUpdate(SwitchViewHelper::class, 'stateStack', $stateStack);
114
115 $result = $renderChildrenClosure();
116
117 $stateStack = $viewHelperVariableContainer->get(SwitchViewHelper::class, 'stateStack');
118 array_pop($stateStack);
119 $viewHelperVariableContainer->addOrUpdate(SwitchViewHelper::class, 'stateStack', $stateStack);
120
121 return $result;
122 }
123
124 }