[TASK] Sync CMS Fluid with Flow Fluid 1.1 (part1)
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / Parser / ParsingState.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\Parser;
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 /**
15 * Stores all information relevant for one parsing pass - that is, the root node,
16 * and the current stack of open nodes (nodeStack) and a variable container used
17 * for PostParseFacets.
18 */
19 class ParsingState implements \TYPO3\CMS\Fluid\Core\Parser\ParsedTemplateInterface {
20
21 /**
22 * Root node reference
23 *
24 * @var \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode
25 */
26 protected $rootNode;
27
28 /**
29 * Array of node references currently open.
30 *
31 * @var array
32 */
33 protected $nodeStack = array();
34
35 /**
36 * Variable container where ViewHelpers implementing the PostParseFacet can
37 * store things in.
38 *
39 * @var \TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer
40 */
41 protected $variableContainer;
42
43 /**
44 * The layout name of the current template or NULL if the template does not contain a layout definition
45 *
46 * @var \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode
47 */
48 protected $layoutNameNode;
49
50 /**
51 * @var boolean
52 */
53 protected $compilable = TRUE;
54
55 /**
56 * Injects a variable container. ViewHelpers implementing the PostParse
57 * Facet can store information inside this variableContainer.
58 *
59 * @param \TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer $variableContainer
60 * @return void
61 */
62 public function injectVariableContainer(\TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer $variableContainer) {
63 $this->variableContainer = $variableContainer;
64 }
65
66 /**
67 * Set root node of this parsing state
68 *
69 * @param \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $rootNode
70 * @return void
71 */
72 public function setRootNode(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $rootNode) {
73 $this->rootNode = $rootNode;
74 }
75
76 /**
77 * Get root node of this parsing state.
78 *
79 * @return \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode The root node
80 */
81 public function getRootNode() {
82 return $this->rootNode;
83 }
84
85 /**
86 * Render the parsed template with rendering context
87 *
88 * @param \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext The rendering context to use
89 * @return string Rendered string
90 */
91 public function render(\TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext) {
92 return $this->rootNode->evaluate($renderingContext);
93 }
94
95 /**
96 * Push a node to the node stack. The node stack holds all currently open
97 * templating tags.
98 *
99 * @param \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $node Node to push to node stack
100 * @return void
101 */
102 public function pushNodeToStack(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $node) {
103 array_push($this->nodeStack, $node);
104 }
105
106 /**
107 * Get the top stack element, without removing it.
108 *
109 * @return \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode the top stack element.
110 */
111 public function getNodeFromStack() {
112 return $this->nodeStack[count($this->nodeStack) - 1];
113 }
114
115 /**
116 * Pop the top stack element (=remove it) and return it back.
117 *
118 * @return \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode the top stack element, which was removed.
119 */
120 public function popNodeFromStack() {
121 return array_pop($this->nodeStack);
122 }
123
124 /**
125 * Count the size of the node stack
126 *
127 * @return integer Number of elements on the node stack (i.e. number of currently open Fluid tags)
128 */
129 public function countNodeStack() {
130 return count($this->nodeStack);
131 }
132
133 /**
134 * Returns a variable container which will be then passed to the postParseFacet.
135 *
136 * @return \TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer The variable container or NULL if none has been set yet
137 * @todo Rename to getPostParseVariableContainer
138 */
139 public function getVariableContainer() {
140 return $this->variableContainer;
141 }
142
143 /**
144 * @param \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $layoutNameNode name of the layout that is defined in this template via <f:layout name="..." />
145 * @return void
146 */
147 public function setLayoutNameNode(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode $layoutNameNode) {
148 $this->layoutNameNode = $layoutNameNode;
149 }
150
151 /**
152 * @return \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode
153 */
154 public function getLayoutNameNode() {
155 return $this->layoutNameNode;
156 }
157
158 /**
159 * Returns TRUE if the current template has a template defined via <f:layout name="..." />
160 *
161 * @see getLayoutName()
162 * @return boolean
163 */
164 public function hasLayout() {
165 return $this->layoutNameNode !== NULL;
166 }
167
168 /**
169 * Returns the name of the layout that is defined within the current template via <f:layout name="..." />
170 * If no layout is defined, this returns NULL
171 * This requires the current rendering context in order to be able to evaluate the layout name
172 *
173 * @param \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
174 * @return string
175 * @throws \TYPO3\CMS\Fluid\View\Exception
176 */
177 public function getLayoutName(\TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext) {
178 if (!$this->hasLayout()) {
179 return NULL;
180 }
181 $layoutName = $this->layoutNameNode->evaluate($renderingContext);
182 if (!empty($layoutName)) {
183 return $layoutName;
184 }
185 throw new \TYPO3\CMS\Fluid\View\Exception('The layoutName could not be evaluated to a string', 1296805368);
186 }
187
188 /**
189 * @return boolean
190 */
191 public function isCompilable() {
192 return $this->compilable;
193 }
194
195 /**
196 * @param boolean $compilable
197 */
198 public function setCompilable($compilable) {
199 $this->compilable = $compilable;
200 }
201
202 /**
203 * @return boolean
204 */
205 public function isCompiled() {
206 return FALSE;
207 }
208 }
209
210 ?>