e7d58325d1775d1e42d37d69c4474472bb58e174
[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\Variables\CmsVariableProvider;
23 use TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperResolver;
24 use TYPO3\CMS\Fluid\View\TemplatePaths;
25 use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
26 use TYPO3Fluid\Fluid\Core\Parser\Configuration;
27 use TYPO3Fluid\Fluid\Core\Parser\TemplateParser;
28 use TYPO3Fluid\Fluid\Core\Parser\TemplateProcessor\EscapingModifierTemplateProcessor;
29 use TYPO3Fluid\Fluid\Core\Parser\TemplateProcessor\NamespaceDetectionTemplateProcessor;
30 use TYPO3Fluid\Fluid\Core\Parser\TemplateProcessor\PassthroughSourceModifierTemplateProcessor;
31 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInvoker;
32 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
33 use TYPO3Fluid\Fluid\View\ViewInterface;
34
35 /**
36 * Class RenderingContext
37 */
38 class RenderingContext extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext
39 {
40 /**
41 * Template Variable Container. Contains all variables available through object accessors in the template
42 *
43 * @var \TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer
44 */
45 protected $templateVariableContainer;
46
47 /**
48 * Object manager which is bubbled through. The ViewHelperNode cannot get an ObjectManager injected because
49 * the whole syntax tree should be cacheable
50 *
51 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
52 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
53 */
54 protected $objectManager;
55
56 /**
57 * Controller context being passed to the ViewHelper
58 *
59 * @var \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
60 */
61 protected $controllerContext;
62
63 /**
64 * Use legacy behavior? Can be overridden using setLegacyMode().
65 *
66 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
67 * @var bool
68 */
69 protected $legacyMode = false;
70
71 /**
72 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
73 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
74 */
75 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
76 {
77 $this->objectManager = $objectManager;
78 }
79
80 /**
81 * @param \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer $viewHelperVariableContainer
82 */
83 public function injectViewHelperVariableContainer(\TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer $viewHelperVariableContainer)
84 {
85 $this->viewHelperVariableContainer = $viewHelperVariableContainer;
86 }
87
88 /**
89 * @param ViewInterface $view
90 */
91 public function __construct(ViewInterface $view = null)
92 {
93 if ($view !== null) {
94 // Note: if $view is received here this indicates internal framework instancing
95 // and it is safe to call the parent constructor. Custom, non-view-providing
96 // usages will only perform the initialisation below (which is sufficient mind you!)
97 parent::__construct($view);
98 } else {
99 // Reproduced partial initialisation from parent::__construct; minus the custom
100 // implementations we attach below.
101 $this->setTemplateParser(new TemplateParser());
102 $this->setTemplateCompiler(new TemplateCompiler());
103 $this->setViewHelperInvoker(new ViewHelperInvoker());
104 $this->setViewHelperVariableContainer(new ViewHelperVariableContainer());
105 $this->setTemplateProcessors(
106 [
107 new EscapingModifierTemplateProcessor(),
108 new PassthroughSourceModifierTemplateProcessor(),
109 new NamespaceDetectionTemplateProcessor()
110 ]
111 );
112 }
113
114 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
115 $this->setTemplatePaths($objectManager->get(TemplatePaths::class));
116 $this->setViewHelperResolver($objectManager->get(ViewHelperResolver::class));
117 $this->setVariableProvider($objectManager->get(CmsVariableProvider::class));
118
119 /** @var FluidTemplateCache $cache */
120 $cache = $objectManager->get(CacheManager::class)->getCache('fluid_template');
121 if (is_a($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['fluid_template']['frontend'], FluidTemplateCache::class, true)) {
122 $this->setCache($cache);
123 }
124 }
125
126 /**
127 * Build parser configuration
128 *
129 * @return Configuration
130 * @throws \InvalidArgumentException if a class not implementing InterceptorInterface was registered
131 */
132 public function buildParserConfiguration()
133 {
134 $parserConfiguration = parent::buildParserConfiguration();
135 if (!empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['interceptors'])) {
136 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['interceptors'] as $className) {
137 $interceptor = GeneralUtility::makeInstance($className);
138 if (!$interceptor instanceof InterceptorInterface) {
139 throw new \InvalidArgumentException('Interceptor "' . $className . '" needs to implement ' . InterceptorInterface::class . '.', 1462869795);
140 }
141 $parserConfiguration->addInterceptor($interceptor);
142 }
143 }
144
145 return $parserConfiguration;
146 }
147
148 /**
149 * Set legacy compatibility mode on/off by boolean.
150 * If set to FALSE, the ViewHelperResolver will only load a limited sub-set of ExpressionNodes,
151 * making Fluid behave like the legacy version of the CMS core extension.
152 *
153 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
154 * @param bool $legacyMode
155 */
156 public function setLegacyMode($legacyMode)
157 {
158 GeneralUtility::logDeprecatedFunction();
159 $this->legacyMode = $legacyMode;
160 }
161
162 /**
163 * Returns the object manager. Only the ViewHelperNode should do this.
164 *
165 * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
166 * @return \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
167 */
168 public function getObjectManager()
169 {
170 return $this->objectManager;
171 }
172
173 /**
174 * Get the template variable container (DEPRECATED; use getVariableProvider instead)
175 *
176 * @deprecated since TYPO3 CMS 8, will be removed in TYPO3 CMS 9 - use getVariableProvider instead
177 * @see getVariableProvider
178 * @return \TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer The Template Variable Container
179 */
180 public function getTemplateVariableContainer()
181 {
182 GeneralUtility::deprecationLog(
183 'getTemplateVariableContainer is deprecated since TYPO3 CMS 8, will be removed in TYPO3 CMS 9' .
184 ' - use getVariableProvider instead'
185 );
186 return $this->variableProvider;
187 }
188
189 /**
190 * Get the controller context which will be passed to the ViewHelper
191 *
192 * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext The controller context to set
193 */
194 public function getControllerContext()
195 {
196 return $this->controllerContext;
197 }
198
199 /**
200 * @param string $action
201 */
202 public function setControllerAction($action)
203 {
204 $dotPosition = strpos($action, '.');
205 if ($dotPosition !== false) {
206 $action = substr($action, 0, $dotPosition);
207 }
208 parent::setControllerAction($action);
209 $this->controllerContext->getRequest()->setControllerActionName(lcfirst($action));
210 }
211
212 /**
213 * @param string $controllerName
214 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException
215 */
216 public function setControllerName($controllerName)
217 {
218 parent::setControllerName($controllerName);
219 $this->controllerContext->getRequest()->setControllerName($controllerName);
220 }
221
222 /**
223 * Set the controller context which will be passed to the ViewHelper
224 *
225 * @param \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext The controller context to set
226 */
227 public function setControllerContext(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext)
228 {
229 $request = $controllerContext->getRequest();
230 $this->controllerContext = $controllerContext;
231 $this->setControllerAction($request->getControllerActionName());
232 // Check if Request is using a sub-package key; in which case we translate this
233 // for our RenderingContext as an emulated plain old sub-namespace controller.
234 $controllerName = $request->getControllerName();
235 if ($request->getControllerSubpackageKey() && !strpos($controllerName, '\\')) {
236 $this->setControllerName($request->getControllerSubpackageKey() . '\\' . $controllerName);
237 } else {
238 $this->setControllerName($controllerName);
239 }
240 }
241 }