Failed Conditions
Push — master ( 5f60a5...9b80eb )
by Rafael
21:42
created

AbstractWidgetViewHelper::injectExtensionService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1.037
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Widget;
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 ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext;
18
use ApacheSolrForTypo3\Solr\Widget\WidgetRequest as SolrFluidWidgetRequest;
19
use TYPO3\CMS\Extbase\Mvc\Web\Response;
20
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
21
use TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode;
22
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\ChildNodeAccessInterface;
23
use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetViewHelper as AbstractCoreWidgetViewHelper;
24
use TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder;
25
use TYPO3\CMS\Fluid\Core\Widget\Exception\MissingControllerException;
26
use TYPO3\CMS\Fluid\Core\Widget\WidgetRequest as CoreWidgetRequest;
27
use TYPO3\CMS\Fluid\Core\Widget\WidgetContext;
28
use TYPO3\CMS\Extbase\Service\ExtensionService;
29
30
31
/**
32
 * Class AbstractWidgetViewHelper
33
 *
34
 * This is almost a exact copy of \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
35
 * because we need to switch the request object to overrule the widget prefix for url params
36
 * todo: find cleaner way to let widget listen to tx_solr[] instead of tx_solr[@widget]
37
 *
38
 * @author Frans Saris <[email protected]>
39
 * @author Timo Hund <[email protected]>
40
 * @package ApacheSolrForTypo3\Solr\Widget
41
 */
42
abstract class AbstractWidgetViewHelper extends AbstractCoreWidgetViewHelper implements ChildNodeAccessInterface
43
{
44
45
    /**
46
     * The Controller associated to this widget.
47
     * This needs to be filled by the individual subclass by an @inject
48
     * annotation.
49
     *
50
     * @var AbstractWidgetController
51
     * @api
52
     */
53
    protected $controller;
54
55
    /**
56
     * If set to TRUE, it is an AJAX widget.
57
     *
58
     * @var boolean
59
     * @api
60
     */
61
    protected $ajaxWidget = false;
62
63
    /**
64
     * @var AjaxWidgetContextHolder
65
     */
66
    private $ajaxWidgetContextHolder;
67
68
    /**
69
     * @var ObjectManagerInterface
70
     */
71
    protected $objectManager;
72
73
    /**
74
     * @var ExtensionService
75
     */
76
    protected $extensionService;
77
78
    /**
79
     * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
80
     */
81
    private $widgetContext;
82
83
    /**
84
     * @param ExtensionService $extensionService
85
     */
86 35
    public function injectExtensionService(ExtensionService $extensionService)
87
    {
88 35
        $this->extensionService = $extensionService;
89 35
    }
90
91
    /**
92
     * @param AjaxWidgetContextHolder $ajaxWidgetContextHolder
93
     * @return void
94
     */
95 35
    public function injectAjaxWidgetContextHolder(AjaxWidgetContextHolder $ajaxWidgetContextHolder)
96
    {
97 35
        $this->ajaxWidgetContextHolder = $ajaxWidgetContextHolder;
98 35
    }
99
100
    /**
101
     * @param ObjectManagerInterface $objectManager
102
     * @return void
103
     */
104 35
    public function injectObjectManager(ObjectManagerInterface $objectManager)
105
    {
106 35
        $this->objectManager = $objectManager;
107 35
        $this->widgetContext = $this->objectManager->get(WidgetContext::class);
108 35
    }
109
110
    /**
111
     * Initialize the arguments of the ViewHelper, and call the render() method of the ViewHelper.
112
     *
113
     * @return string the rendered ViewHelper.
114
     */
115 34
    public function initializeArgumentsAndRender()
116
    {
117 34
        $this->validateArguments();
118 34
        $this->initialize();
119 34
        $this->initializeWidgetContext();
120 34
        return $this->callRenderMethod();
121
    }
122
123
    /**
124
     * @todo The fallback on $this->controllerContext is only needed for TYPO3 8 backwards compatibility and can be dropped when TYPO3 8 is not supported anymore
125
     * @return SolrControllerContext
126
     * @throws \InvalidArgumentException
127
     */
128 34
    protected function getControllerContext()
129
    {
130 34
        $controllerContext = null;
131 34
        if (!is_null($this->controllerContext)) {
0 ignored issues
show
Bug introduced by
The property controllerContext does not exist on ApacheSolrForTypo3\Solr\...bstractWidgetViewHelper. Did you mean controller?
Loading history...
132 34
            $controllerContext = $this->controllerContext;
133
        } elseif (method_exists($this->renderingContext, 'getControllerContext')) {
134
            $controllerContext = $this->renderingContext->getControllerContext();
135
        }
136
137 34
        if (!$controllerContext instanceof SolrControllerContext) {
138
            throw new \InvalidArgumentException('No valid SolrControllerContext found', 1512998673);
139
        }
140
141 34
        return $controllerContext;
142
    }
143
144
    /**
145
     * Initialize the Widget Context, before the Render method is called.
146
     *
147
     * @return void
148
     */
149 34
    private function initializeWidgetContext()
150
    {
151 34
        $this->widgetContext->setWidgetConfiguration($this->getWidgetConfiguration());
152 34
        $this->initializeWidgetIdentifier();
153 34
        $this->widgetContext->setControllerObjectName(get_class($this->controller));
154
155 34
        $extensionName = $this->getControllerContext()->getRequest()->getControllerExtensionName();
156 34
        $pluginName = $this->getControllerContext()->getRequest()->getPluginName();
157 34
        $this->widgetContext->setParentExtensionName($extensionName);
158 34
        $this->widgetContext->setParentPluginName($pluginName);
159 34
        $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
160 34
        $this->widgetContext->setParentPluginNamespace($pluginNamespace);
161 34
        $this->widgetContext->setWidgetViewHelperClassName(get_class($this));
162
163 34
        if ($this->ajaxWidget === true) {
164
            $this->ajaxWidgetContextHolder->store($this->widgetContext);
165
        }
166 34
    }
167
168
    /**
169
     * Stores the syntax tree child nodes in the Widget Context, so they can be
170
     * rendered with <f:widget.renderChildren> lateron.
171
     *
172
     * @param array $childNodes The SyntaxTree Child nodes of this ViewHelper.
173
     * @return void
174
     */
175 35
    public function setChildNodes(array $childNodes)
176
    {
177 35
        $rootNode = $this->objectManager->get(RootNode::class);
178 35
        foreach ($childNodes as $childNode) {
179 35
            $rootNode->addChildNode($childNode);
180
        }
181 35
        $this->widgetContext->setViewHelperChildNodes($rootNode, $this->renderingContext);
182 35
    }
183
184
    /**
185
     * Generate the configuration for this widget. Override to adjust.
186
     *
187
     * @return array
188
     * @api
189
     */
190 34
    protected function getWidgetConfiguration()
191
    {
192 34
        return $this->arguments;
193
    }
194
195
    /**
196
     * Initiate a sub request to $this->controller. Make sure to fill $this->controller
197
     * via Dependency Injection.
198
     *
199
     * @return \TYPO3\CMS\Extbase\Mvc\ResponseInterface the response of this request.
200
     * @throws MissingControllerException
201
     */
202 34
    protected function initiateSubRequest()
203
    {
204 34
        if (!$this->controller instanceof AbstractWidgetController) {
0 ignored issues
show
introduced by
The condition ! $this->controller inst...bstractWidgetController can never be true.
Loading history...
205
            if (isset($this->controller)) {
206
                throw new MissingControllerException('initiateSubRequest() can not be called if there is no valid controller extending TYPO3\\CMS\\Fluid\\Core\\Widget\\AbstractWidgetController. Got "' . get_class($this->controller) . '" in class "' . get_class($this) . '".', 1289422564);
207
            }
208
            throw new MissingControllerException('initiateSubRequest() can not be called if there is no controller inside $this->controller. Make sure to add a corresponding injectController method to your WidgetViewHelper class "' . get_class($this) . '".', 1284401632);
209
        }
210
            /** @var $subRequest \ApacheSolrForTypo3\Solr\Widget\WidgetRequest */
211 34
        $subRequest = $this->objectManager->get(SolrFluidWidgetRequest::class);
212 34
        $subRequest->setWidgetContext($this->widgetContext);
213
214 34
        $this->passArgumentsToSubRequest($subRequest);
215 34
        $subResponse = $this->objectManager->get(Response::class);
216 34
        $this->controller->processRequest($subRequest, $subResponse);
217 34
        return $subResponse;
218
    }
219
220
    /**
221
     * Pass the arguments of the widget to the subrequest.
222
     *
223
     * @param CoreWidgetRequest $subRequest
224
     * @return void
225
     */
226 34
    private function passArgumentsToSubRequest(CoreWidgetRequest $subRequest)
227
    {
228 34
        $arguments = $this->getControllerContext()->getRequest()->getArguments();
229
230 34
        if (isset($arguments)) {
231 34
            if (isset($arguments['action'])) {
232
                $subRequest->setControllerActionName($arguments['action']);
233
                unset($arguments['action']);
234
            }
235 34
            $subRequest->setArguments($arguments);
236
        }
237 34
    }
238
239
    /**
240
     * The widget identifier is unique on the current page, and is used
241
     * in the URI as a namespace for the widget's arguments.
242
     *
243
     * @return string the widget identifier for this widget
244
     * @return void
245
     * @todo clean up, and make it somehow more routing compatible.
246
     */
247 34
    private function initializeWidgetIdentifier()
248
    {
249 34
        $this->widgetContext->setWidgetIdentifier('');
250 34
    }
251
}
252