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