[!!!][TASK] Remove deprecated code from ext: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\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 * Controller context being passed to the ViewHelper
49 *
50 * @var \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
51 */
52 protected $controllerContext;
53
54 /**
55 * @param \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer $viewHelperVariableContainer
56 */
57 public function injectViewHelperVariableContainer(\TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer $viewHelperVariableContainer)
58 {
59 $this->viewHelperVariableContainer = $viewHelperVariableContainer;
60 }
61
62 /**
63 * @param ViewInterface $view
64 */
65 public function __construct(ViewInterface $view = null)
66 {
67 if ($view !== null) {
68 // Note: if $view is received here this indicates internal framework instancing
69 // and it is safe to call the parent constructor. Custom, non-view-providing
70 // usages will only perform the initialisation below (which is sufficient mind you!)
71 parent::__construct($view);
72 } else {
73 // Reproduced partial initialisation from parent::__construct; minus the custom
74 // implementations we attach below.
75 $this->setTemplateParser(new TemplateParser());
76 $this->setTemplateCompiler(new TemplateCompiler());
77 $this->setViewHelperInvoker(new ViewHelperInvoker());
78 $this->setViewHelperVariableContainer(new ViewHelperVariableContainer());
79 $this->setTemplateProcessors(
80 [
81 new EscapingModifierTemplateProcessor(),
82 new PassthroughSourceModifierTemplateProcessor(),
83 new NamespaceDetectionTemplateProcessor()
84 ]
85 );
86 }
87
88 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
89 $this->setTemplatePaths($objectManager->get(TemplatePaths::class));
90 $this->setViewHelperResolver($objectManager->get(ViewHelperResolver::class));
91 $this->setVariableProvider($objectManager->get(CmsVariableProvider::class));
92
93 /** @var FluidTemplateCache $cache */
94 $cache = $objectManager->get(CacheManager::class)->getCache('fluid_template');
95 if (is_a($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['fluid_template']['frontend'], FluidTemplateCache::class, true)) {
96 $this->setCache($cache);
97 }
98 }
99
100 /**
101 * Build parser configuration
102 *
103 * @return Configuration
104 * @throws \InvalidArgumentException if a class not implementing InterceptorInterface was registered
105 */
106 public function buildParserConfiguration()
107 {
108 $parserConfiguration = parent::buildParserConfiguration();
109 if (!empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['interceptors'])) {
110 foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['interceptors'] as $className) {
111 $interceptor = GeneralUtility::makeInstance($className);
112 if (!$interceptor instanceof InterceptorInterface) {
113 throw new \InvalidArgumentException('Interceptor "' . $className . '" needs to implement ' . InterceptorInterface::class . '.', 1462869795);
114 }
115 $parserConfiguration->addInterceptor($interceptor);
116 }
117 }
118
119 return $parserConfiguration;
120 }
121
122 /**
123 * Get the controller context which will be passed to the ViewHelper
124 *
125 * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext The controller context to set
126 */
127 public function getControllerContext()
128 {
129 return $this->controllerContext;
130 }
131
132 /**
133 * @param string $action
134 */
135 public function setControllerAction($action)
136 {
137 $dotPosition = strpos($action, '.');
138 if ($dotPosition !== false) {
139 $action = substr($action, 0, $dotPosition);
140 }
141 parent::setControllerAction($action);
142 $this->controllerContext->getRequest()->setControllerActionName(lcfirst($action));
143 }
144
145 /**
146 * @param string $controllerName
147 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException
148 */
149 public function setControllerName($controllerName)
150 {
151 parent::setControllerName($controllerName);
152 $this->controllerContext->getRequest()->setControllerName($controllerName);
153 }
154
155 /**
156 * Set the controller context which will be passed to the ViewHelper
157 *
158 * @param \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext The controller context to set
159 */
160 public function setControllerContext(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext)
161 {
162 $request = $controllerContext->getRequest();
163 $this->controllerContext = $controllerContext;
164 $this->setControllerAction($request->getControllerActionName());
165 // Check if Request is using a sub-package key; in which case we translate this
166 // for our RenderingContext as an emulated plain old sub-namespace controller.
167 $controllerName = $request->getControllerName();
168 if ($request->getControllerSubpackageKey() && !strpos($controllerName, '\\')) {
169 $this->setControllerName($request->getControllerSubpackageKey() . '\\' . $controllerName);
170 } else {
171 $this->setControllerName($controllerName);
172 }
173 }
174 }