Completed
Push — master ( 653dde...3bddf0 )
by
unknown
181:02 queued 144:47
created

RenderingContext::getParserConfiguration()   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
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\ViewHelper\ViewHelperResolver;
22
use TYPO3\CMS\Fluid\View\TemplatePaths;
23
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
24
use TYPO3Fluid\Fluid\Core\Parser\Configuration;
25
use TYPO3Fluid\Fluid\Core\Parser\InterceptorInterface;
26
use TYPO3Fluid\Fluid\Core\Parser\TemplateParser;
27
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
28
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInvoker;
29
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
30
use TYPO3Fluid\Fluid\View\ViewInterface;
31
32
/**
33
 * Class RenderingContext
34
 */
35
class RenderingContext extends \TYPO3Fluid\Fluid\Core\Rendering\RenderingContext
36
{
37
    /**
38
     * Controller context being passed to the ViewHelper
39
     *
40
     * @var \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
41
     */
42
    protected $controllerContext;
43
44
    /**
45
     * @var string
46
     */
47
    protected $controllerName = 'Default';
48
49
    /**
50
     * @var string
51
     */
52
    protected $controllerAction = 'Default';
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($this));
0 ignored issues
show
Unused Code introduced by
The call to TYPO3Fluid\Fluid\Core\Pa...teParser::__construct() has too many arguments starting with $this. ( Ignorable by Annotation )

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

75
            $this->setTemplateParser(/** @scrutinizer ignore-call */ new TemplateParser($this));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
76
            if (method_exists($this, 'setTemplateCompiler')) {
77
                $this->setTemplateCompiler(new TemplateCompiler());
78
            }
79
            if (method_exists($this, 'setViewHelperInvoker')) {
80
                $this->setViewHelperInvoker(new ViewHelperInvoker());
81
            }
82
            $this->setViewHelperVariableContainer(new ViewHelperVariableContainer());
83
            $this->setVariableProvider(new StandardVariableProvider());
84
        }
85
86
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
87
        if (method_exists($this, 'setTemplateProcessors')) {
88
            $this->setTemplateProcessors(array_map([$objectManager, 'get'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['preProcessors']));
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
     */
144
    public function getControllerContext()
145
    {
146
        return $this->controllerContext;
147
    }
148
149
    /**
150
     * @param string $action
151
     */
152
    public function setControllerAction($action)
153
    {
154
        $dotPosition = strpos($action, '.');
155
        if ($dotPosition !== false) {
156
            $action = substr($action, 0, $dotPosition);
157
        }
158
        $this->controllerAction = $action;
159
        if ($this->controllerContext) {
160
            $this->controllerContext->getRequest()->setControllerActionName(lcfirst($action));
161
        }
162
    }
163
164
    /**
165
     * @param string $controllerName
166
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException
167
     */
168
    public function setControllerName($controllerName)
169
    {
170
        $this->controllerName = $controllerName;
171
        if ($this->controllerContext) {
172
            $this->controllerContext->getRequest()->setControllerName($controllerName);
173
        }
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getControllerName()
180
    {
181
        return $this->controllerContext ? $this->controllerContext->getRequest()->getControllerName() : $this->controllerName;
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getControllerAction()
188
    {
189
        return $this->controllerContext ? $this->controllerContext->getRequest()->getControllerActionName() : $this->controllerAction;
190
    }
191
192
    /**
193
     * Set the controller context which will be passed to the ViewHelper
194
     *
195
     * @param \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext The controller context to set
196
     */
197
    public function setControllerContext(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext $controllerContext)
198
    {
199
        $request = $controllerContext->getRequest();
200
        $this->controllerContext = $controllerContext;
201
        $this->setControllerAction($request->getControllerActionName());
202
        // Check if Request is using a sub-package key; in which case we translate this
203
        // for our RenderingContext as an emulated plain old sub-namespace controller.
204
        $controllerName = $request->getControllerName();
205
        if ($request->getControllerSubpackageKey() && !strpos($controllerName, '\\')) {
206
            $this->setControllerName($request->getControllerSubpackageKey() . '\\' . $controllerName);
207
        } else {
208
            $this->setControllerName($controllerName);
209
        }
210
    }
211
}
212