[BUGFIX] Do not override template processors of fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Core / Rendering / RenderingContext.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Core\Rendering;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Cache\CacheManager;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Object\ObjectManager;
20 use TYPO3\CMS\Fluid\Core\Cache\FluidTemplateCache;
21 use TYPO3\CMS\Fluid\Core\Parser\InterceptorInterface;
22 use TYPO3\CMS\Fluid\Core\Parser\PreProcessor\XmlnsNamespaceTemplatePreProcessor;
23 use TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\Expression\LegacyNamespaceExpressionNode;
24 use TYPO3\CMS\Fluid\Core\Variables\CmsVariableProvider;
25 use TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperResolver;
26 use TYPO3\CMS\Fluid\View\TemplatePaths;
27 use TYPO3Fluid\Fluid\Core\Parser\Configuration;
28 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\Expression\CastingExpressionNode;
29 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\Expression\MathExpressionNode;
30 use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\Expression\TernaryExpressionNode;
31 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
32 use TYPO3Fluid\Fluid\View\ViewInterface;
33
34 /**
35 * Class RenderingContext
36 */
37 class RenderingContext extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext
38 {
39 /**
40 * Template Variable Container. Contains all variables available through object accessors in the template
41 *
42 * @var \TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer
43 */
44 protected $templateVariableContainer;
45
46 /**
47 * Object manager which is bubbled through. The ViewHelperNode cannot get an ObjectManager injected because
48 * the whole syntax tree should be cacheable
49 *
50 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
51 */
52 protected $objectManager;
53
54 /**
55 * Controller context being passed to the ViewHelper
56 *
57 * @var \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
58 */
59 protected $controllerContext;
60
61 /**
62 * ViewHelper Variable Container
63 *
64 * @var \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer
65 */
66 protected $viewHelperVariableContainer;
67
68 /**
69 * Use legacy behavior? Can be overridden using setLegacyMode().
70 *
71 * @var bool
72 */
73 protected $legacyMode = false;
74
75 /**
76 * List of class names implementing ExpressionNodeInterface
77 * which will be consulted when an expression does not match
78 * any built-in parser expression types.
79 *
80 * @var string
81 */
82 protected $expressionNodeTypes = [
83 LegacyNamespaceExpressionNode::class,
84 CastingExpressionNode::class,
85 MathExpressionNode::class,
86 TernaryExpressionNode::class
87 ];
88
89 /**
90 * Alternative ExpressionNodeInterface implementers for use
91 * when put into legacy mode.
92 *
93 * @var string
94 */
95 protected $legacyExpressionNodeTypes = [
96 LegacyNamespaceExpressionNode::class
97 ];
98
99 /**
100 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
101 */
102 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
103 {
104 $this->objectManager = $objectManager;
105 }
106
107 /**
108 * @param \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer $viewHelperVariableContainer
109 */
110 public function injectViewHelperVariableContainer(\TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer $viewHelperVariableContainer)
111 {
112 $this->viewHelperVariableContainer = $viewHelperVariableContainer;
113 }
114
115 /**
116 * @param ViewInterface $view
117 */
118 public function __construct(ViewInterface $view = null)
119 {
120 parent::__construct($view);
121
122 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
123 if ($view) {
124 $this->view = $view;
125 }
126 $this->setTemplatePaths($objectManager->get(TemplatePaths::class));
127 $this->setViewHelperResolver($objectManager->get(ViewHelperResolver::class));
128 $this->setVariableProvider($objectManager->get(CmsVariableProvider::class));
129 $this->setTemplateProcessors(
130 array_merge(
131 parent::getTemplateProcessors(),
132 [
133 $objectManager->get(XmlnsNamespaceTemplatePreProcessor::class),
134 ]
135 )
136 );
137 /** @var FluidTemplateCache $cache */
138 $cache = $objectManager->get(CacheManager::class)->getCache('fluid_template');
139 if (is_a($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['fluid_template']['frontend'], FluidTemplateCache::class, true)) {
140 $this->setCache($cache);
141 }
142 }
143
144 /**
145 * Build parser configuration
146 *
147 * @return Configuration
148 * @throws \InvalidArgumentException if a class not implementing InterceptorInterface was registered
149 */
150 public function buildParserConfiguration()
151 {
152 $parserConfiguration = parent::buildParserConfiguration();
153 if (!empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['interceptors'])) {
154 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['interceptors'] as $className) {
155 $interceptor = GeneralUtility::makeInstance($className);
156 if (!$interceptor instanceof InterceptorInterface) {
157 throw new \InvalidArgumentException('Interceptor "' . $className . '" needs to implement ' . InterceptorInterface::class . '.', 1462869795);
158 }
159 $parserConfiguration->addInterceptor($interceptor);
160 }
161 }
162
163 return $parserConfiguration;
164 }
165
166 /**
167 * Set legacy compatibility mode on/off by boolean.
168 * If set to FALSE, the ViewHelperResolver will only load a limited sub-set of ExpressionNodes,
169 * making Fluid behave like the legacy version of the CMS core extension.
170 *
171 * @param bool $legacyMode
172 * @return void
173 */
174 public function setLegacyMode($legacyMode)
175 {
176 $this->legacyMode = $legacyMode;
177 }
178
179 /**
180 * @return string
181 */
182 public function getExpressionNodeTypes()
183 {
184 return $this->legacyMode ? $this->legacyExpressionNodeTypes : $this->expressionNodeTypes;
185 }
186
187 /**
188 * Returns the object manager. Only the ViewHelperNode should do this.
189 *
190 * @return \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
191 */
192 public function getObjectManager()
193 {
194 return $this->objectManager;
195 }
196
197 /**
198 * Get the template variable container (DEPRECATED; use getVariableProvider instead)
199 *
200 * @deprecated since TYPO3 CMS 8, will be removed in TYPO3 CMS 9 - use getVariableProvider instead
201 * @see getVariableProvider
202 * @return \TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer The Template Variable Container
203 */
204 public function getTemplateVariableContainer()
205 {
206 GeneralUtility::deprecationLog(
207 'getTemplateVariableContainer is deprecated since TYPO3 CMS 8, will be removed in TYPO3 CMS 9' .
208 ' - use getVariableProvider instead'
209 );
210 return $this->variableProvider;
211 }
212
213 /**
214 * Get the controller context which will be passed to the ViewHelper
215 *
216 * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext The controller context to set
217 */
218 public function getControllerContext()
219 {
220 return $this->controllerContext;
221 }
222
223 /**
224 * @param string $action
225 * @return void
226 */
227 public function setControllerAction($action)
228 {
229 $action = lcfirst(pathinfo($action, PATHINFO_FILENAME));
230 parent::setControllerAction($action);
231 $this->controllerContext->getRequest()->setControllerActionName($action);
232 }
233
234 /**
235 * @param string $controllerName
236 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException
237 */
238 public function setControllerName($controllerName)
239 {
240 parent::setControllerName($controllerName);
241 $this->controllerContext->getRequest()->setControllerName($controllerName);
242 }
243
244 /**
245 * Set the controller context which will be passed to the ViewHelper
246 *
247 * @param \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext The controller context to set
248 */
249 public function setControllerContext(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext)
250 {
251 $request = $controllerContext->getRequest();
252 $this->controllerContext = $controllerContext;
253 $this->setControllerAction($request->getControllerActionName());
254 // Check if Request is using a sub-package key; in which case we translate this
255 // for our RenderingContext as an emulated plain old sub-namespace controller.
256 $controllerName = $request->getControllerName();
257 if ($request->getControllerSubpackageKey() && !strpos($controllerName, '\\')) {
258 $this->setControllerName($request->getControllerSubpackageKey() . '\\' . $controllerName);
259 } else {
260 $this->setControllerName($controllerName);
261 }
262 }
263
264 /**
265 * Get the ViewHelperVariableContainer
266 *
267 * @return \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer
268 */
269 public function getViewHelperVariableContainer()
270 {
271 return $this->viewHelperVariableContainer;
272 }
273 }