Passed
Push — master ( 1e4c67...b68c5d )
by
unknown
19:36 queued 02:18
created

RenderingContext::getUriBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Fluid\Core\Rendering;
17
18
use TYPO3\CMS\Core\Cache\CacheManager;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
21
use TYPO3\CMS\Extbase\Mvc\Request;
22
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
23
use TYPO3\CMS\Extbase\Object\ObjectManager;
24
use TYPO3\CMS\Fluid\Core\Cache\FluidTemplateCache;
25
use TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperResolver;
26
use TYPO3\CMS\Fluid\View\TemplatePaths;
27
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
28
use TYPO3Fluid\Fluid\Core\Parser\Configuration;
29
use TYPO3Fluid\Fluid\Core\Parser\InterceptorInterface;
30
use TYPO3Fluid\Fluid\Core\Parser\TemplateParser;
31
use TYPO3Fluid\Fluid\Core\Parser\TemplateProcessorInterface;
32
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
33
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInvoker;
34
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
35
36
/**
37
 * Class RenderingContext
38
 */
39
class RenderingContext extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext
40
{
41
    /**
42
     * Controller context being passed to the ViewHelper
43
     *
44
     * @var \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
45
     */
46
    protected $controllerContext;
47
48
    /**
49
     * @var Request
50
     */
51
    protected $request;
52
53
    /**
54
     * @var string
55
     */
56
    protected $controllerName = 'Default';
57
58
    /**
59
     * @var string
60
     */
61
    protected $controllerAction = 'Default';
62
63
    /**
64
     * @param \TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer $viewHelperVariableContainer
65
     */
66
    public function injectViewHelperVariableContainer(ViewHelperVariableContainer $viewHelperVariableContainer)
67
    {
68
        $this->viewHelperVariableContainer = $viewHelperVariableContainer;
69
    }
70
71
    public function __construct()
72
    {
73
        // Reproduced partial initialisation from parent::__construct; minus the custom implementations we attach below.
74
        $this->setTemplateParser(new TemplateParser());
75
        if (method_exists($this, 'setTemplateCompiler')) {
76
            $this->setTemplateCompiler(new TemplateCompiler());
77
        }
78
        if (method_exists($this, 'setViewHelperInvoker')) {
79
            $this->setViewHelperInvoker(new ViewHelperInvoker());
80
        }
81
        $this->setViewHelperVariableContainer(new ViewHelperVariableContainer());
82
        $this->setVariableProvider(new StandardVariableProvider());
83
84
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
85
        if (method_exists($this, 'setTemplateProcessors')) {
86
            /** @var TemplateProcessorInterface[] $processors */
87
            $processors = array_map([$objectManager, 'get'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['preProcessors']);
88
            $this->setTemplateProcessors($processors);
89
        }
90
        $this->setExpressionNodeTypes($GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['expressionNodeTypes']);
91
        $this->setTemplatePaths($objectManager->get(TemplatePaths::class));
92
        $this->setViewHelperResolver($objectManager->get(ViewHelperResolver::class));
93
94
        if (method_exists($this, 'setCache')) {
95
            /** @var FluidTemplateCache $cache */
96
            $cache = $objectManager->get(CacheManager::class)->getCache('fluid_template');
97
            if (is_a($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['fluid_template']['frontend'], FluidTemplateCache::class, true)) {
98
                $this->setCache($cache);
99
            }
100
        }
101
    }
102
103
    /**
104
     * Alternative to buildParserConfiguration, called only in Fluid 3.0
105
     *
106
     * @return Configuration
107
     */
108
    public function getParserConfiguration(): Configuration
109
    {
110
        $parserConfiguration = parent::getParserConfiguration();
0 ignored issues
show
introduced by
The method getParserConfiguration() does not exist on TYPO3Fluid\Fluid\Core\Rendering\RenderingContext. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
        /** @scrutinizer ignore-call */ 
111
        $parserConfiguration = parent::getParserConfiguration();
Loading history...
111
        $this->addInterceptorsToParserConfiguration($GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['interceptors'], $parserConfiguration);
112
        return $parserConfiguration;
113
    }
114
115
    /**
116
     * Build parser configuration
117
     *
118
     * @return Configuration
119
     * @throws \InvalidArgumentException if a class not implementing InterceptorInterface was registered
120
     */
121
    public function buildParserConfiguration()
122
    {
123
        $parserConfiguration = parent::buildParserConfiguration();
124
        $this->addInterceptorsToParserConfiguration($GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['interceptors'], $parserConfiguration);
125
        return $parserConfiguration;
126
    }
127
128
    protected function addInterceptorsToParserConfiguration(iterable $interceptors, Configuration $parserConfiguration): void
129
    {
130
        foreach ($interceptors as $className) {
131
            $interceptor = GeneralUtility::makeInstance($className);
132
            if (!$interceptor instanceof InterceptorInterface) {
133
                throw new \InvalidArgumentException('Interceptor "' . $className . '" needs to implement ' . InterceptorInterface::class . '.', 1462869795);
134
            }
135
            $parserConfiguration->addInterceptor($interceptor);
136
        }
137
    }
138
139
    /**
140
     * Get the controller context which will be passed to the ViewHelper
141
     *
142
     * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext The controller context to set
143
     * @deprecated since v11, will be removed in v12
144
     */
145
    public function getControllerContext()
146
    {
147
        // todo: trigger an error as soon as ControllerContext is deprecated
148
149
        if ($this->controllerContext) {
150
            return $this->controllerContext;
151
        }
152
        $controllerContext = GeneralUtility::makeInstance(ObjectManager::class)->get(ControllerContext::class);
153
        if ($this->request) {
154
            $controllerContext->setRequest($this->request);
155
        }
156
        return $controllerContext;
157
    }
158
159
    /**
160
     * @param string $action
161
     */
162
    public function setControllerAction($action)
163
    {
164
        $dotPosition = strpos($action, '.');
165
        if ($dotPosition !== false) {
166
            $action = substr($action, 0, $dotPosition);
167
        }
168
        $this->controllerAction = $action;
169
        if ($this->request) {
170
            $this->request->setControllerActionName(lcfirst($action));
171
        }
172
    }
173
174
    /**
175
     * @param string $controllerName
176
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException
177
     */
178
    public function setControllerName($controllerName)
179
    {
180
        $this->controllerName = $controllerName;
181
        if ($this->request instanceof Request) {
0 ignored issues
show
introduced by
$this->request is always a sub-type of TYPO3\CMS\Extbase\Mvc\Request.
Loading history...
182
            $this->request->setControllerName($controllerName);
183
        }
184
    }
185
186
    /**
187
     * @return string
188
     */
189
    public function getControllerName()
190
    {
191
        return $this->request instanceof Request ? $this->request->getControllerName() : $this->controllerName;
0 ignored issues
show
introduced by
$this->request is always a sub-type of TYPO3\CMS\Extbase\Mvc\Request.
Loading history...
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function getControllerAction()
198
    {
199
        return $this->request instanceof Request ? $this->request->getControllerActionName() : $this->controllerAction;
0 ignored issues
show
introduced by
$this->request is always a sub-type of TYPO3\CMS\Extbase\Mvc\Request.
Loading history...
200
    }
201
202
    /**
203
     * Set the controller context which will be passed to the ViewHelper
204
     *
205
     * @param \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext The controller context to set
206
     */
207
    public function setControllerContext(ControllerContext $controllerContext)
208
    {
209
        $request = $controllerContext->getRequest();
210
        $this->controllerContext = $controllerContext;
211
        $this->setRequest($request);
212
    }
213
214
    /**
215
     * @param Request $request
216
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException
217
     * @throws \TYPO3\CMS\Extbase\Object\Exception
218
     * @internal this might change to use a PSR-7 compliant request
219
     */
220
    public function setRequest(Request $request): void
221
    {
222
        $this->request = $request;
223
        $this->setControllerAction($request->getControllerActionName());
224
        // Check if Request is using a sub-package key; in which case we translate this
225
        // for our RenderingContext as an emulated plain old sub-namespace controller.
226
        $controllerName = $request->getControllerName();
227
        if ($request->getControllerSubpackageKey() && !strpos($controllerName, '\\')) {
228
            $this->setControllerName($request->getControllerSubpackageKey() . '\\' . $controllerName);
229
        } else {
230
            $this->setControllerName($controllerName);
231
        }
232
        // Also ensure that controller context is filled, if not set yet.
233
        if ($this->controllerContext === null) {
234
            $this->controllerContext = GeneralUtility::makeInstance(ObjectManager::class)->get(ControllerContext::class);
235
            $this->controllerContext->setRequest($request);
236
        }
237
    }
238
239
    /**
240
     * @return Request
241
     * @internal this might change to use a PSR-7 compliant request
242
     */
243
    public function getRequest(): Request
244
    {
245
        return $this->request;
246
    }
247
248
    /**
249
     * @return UriBuilder
250
     * @internal this is subject to change
251
     */
252
    public function getUriBuilder(): UriBuilder
253
    {
254
        $uriBuilder = GeneralUtility::makeInstance(ObjectManager::class)->get(UriBuilder::class);
255
        $uriBuilder->setRequest($this->request);
256
        return $uriBuilder;
257
    }
258
}
259