Issues (202)

Classes/Controller/AbstractBaseController.php (7 issues)

1
<?php
2
namespace ApacheSolrForTypo3\Solr\Controller;
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\ConnectionManager;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService;
19
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequestBuilder;
20
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
21
use ApacheSolrForTypo3\Solr\Search;
22
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
23
use ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext;
24
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
25
use ApacheSolrForTypo3\Solr\System\Service\ConfigurationService;
26
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager as SolrConfigurationManager;
27
use ApacheSolrForTypo3\Solr\Util;
28
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
31
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
32
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
33
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
34
35
/**
36
 * Class AbstractBaseController
37
 *
38
 * @author Frans Saris <[email protected]>
39
 * @author Timo Hund <[email protected]>
40
 */
41
abstract class AbstractBaseController extends ActionController
42
{
43
    /**
44
     * @var ContentObjectRenderer
45
     */
46
    private $contentObjectRenderer;
47
48
    /**
49
     * @var TypoScriptFrontendController
50
     */
51
    protected $typoScriptFrontendController;
52
53
    /**
54
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
55
     */
56
    protected $configurationManager;
57
58
    /**
59
     * @var SolrConfigurationManager
60
     */
61
    private $solrConfigurationManager;
62
63
    /**
64
     * The configuration is private if you need it please get it from the controllerContext.
65
     *
66
     * @var TypoScriptConfiguration
67
     */
68
    protected $typoScriptConfiguration;
69
70
    /**
71
     * @var \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext
72
     */
73
    protected $controllerContext;
74
75
    /**
76
     * @var \ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService
77
     */
78
    protected $searchService;
79
80
    /**
81
     * @var \ApacheSolrForTypo3\Solr\Domain\Search\SearchRequestBuilder
82
     */
83
    protected $searchRequestBuilder;
84
85
    /**
86
     * @var bool
87
     */
88
    protected $resetConfigurationBeforeInitialize = true;
89
90
    /**
91
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
92
     * @return void
93 41
     */
94
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
95 41
    {
96 41
        $this->configurationManager = $configurationManager;
97 41
        // @extensionScannerIgnoreLine
98
        $this->contentObjectRenderer = $this->configurationManager->getContentObject();
99
    }
100
101
    /**
102 1
     * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObjectRenderer
103
     */
104 1
    public function setContentObjectRenderer($contentObjectRenderer)
105 1
    {
106
        $this->contentObjectRenderer = $contentObjectRenderer;
107
    }
108
109
    /**
110
     * @return \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
111
     */
112
    public function getContentObjectRenderer()
113
    {
114
        return $this->contentObjectRenderer;
115
    }
116
117
    /**
118 41
     * @param SolrConfigurationManager $configurationManager
119
     */
120 41
    public function injectSolrConfigurationManager(SolrConfigurationManager $configurationManager)
121 41
    {
122
        $this->solrConfigurationManager = $configurationManager;
123
    }
124
125
    /**
126 22
     * @param boolean $resetConfigurationBeforeInitialize
127
     */
128 22
    public function setResetConfigurationBeforeInitialize($resetConfigurationBeforeInitialize)
129 22
    {
130
        $this->resetConfigurationBeforeInitialize = $resetConfigurationBeforeInitialize;
131
    }
132
133
    /**
134
     * Initialize the controller context
135
     *
136
     * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext ControllerContext to be passed to the view
137 41
     * @api
138
     */
139
    protected function buildControllerContext()
140 41
    {
141 41
        /** @var $controllerContext \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext */
142 41
        $controllerContext = $this->objectManager->get(SolrControllerContext::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

142
        $controllerContext = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(SolrControllerContext::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
143 41
        $controllerContext->setRequest($this->request);
144 41
        $controllerContext->setResponse($this->response);
145
        if ($this->arguments !== null) {
146 41
            $controllerContext->setArguments($this->arguments);
147
        }
148 41
        $controllerContext->setUriBuilder($this->uriBuilder);
149
150 41
        $controllerContext->setTypoScriptConfiguration($this->typoScriptConfiguration);
151
152
        return $controllerContext;
153
    }
154
155
    /**
156 41
     * Initialize action
157
     */
158
    protected function initializeAction()
159 41
    {
160 19
        // Reset configuration (to reset flexform overrides) if resetting is enabled
161
        if ($this->resetConfigurationBeforeInitialize) {
162
            $this->solrConfigurationManager->reset();
163 41
        }
164
        /** @var TypoScriptService $typoScriptService */
165
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

165
        $typoScriptService = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(TypoScriptService::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
166 41
167 41
        // Merge settings done by typoscript with solrConfiguration plugin.tx_solr (obsolete when part of ext:solr)
168 41
        $frameWorkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
169 41
        $pluginSettings = [];
170 41
        foreach (['search', 'settings', 'suggest', 'statistics', 'logging', 'general', 'solr', 'view'] as $key) {
171
            if (isset($frameWorkConfiguration[$key])) {
172
                $pluginSettings[$key] = $frameWorkConfiguration[$key];
173
            }
174 41
        }
175 41
176
        $this->typoScriptConfiguration = $this->solrConfigurationManager->getTypoScriptConfiguration();
177
        if ($pluginSettings !== []) {
178
            $this->typoScriptConfiguration->mergeSolrConfiguration(
179
                $typoScriptService->convertPlainArrayToTypoScriptArray($pluginSettings),
180
                true,
181
                false
182
            );
183 41
        }
184 41
185 41
        $this->objectManager->get(ConfigurationService::class)
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

185
        /** @scrutinizer ignore-deprecated */ $this->objectManager->get(ConfigurationService::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
186 41
            ->overrideConfigurationWithFlexFormSettings(
187
                $this->contentObjectRenderer->data['pi_flexform'],
188
                $this->typoScriptConfiguration
189 41
            );
190 41
191 41
        parent::initializeAction();
192 41
        $this->typoScriptFrontendController = $GLOBALS['TSFE'];
193 41
        $this->initializeSettings();
194
195
        if ($this->actionMethodName !== 'solrNotAvailableAction') {
196
            $this->initializeSearch();
197
        }
198
    }
199
200 41
    /**
201
     * Inject settings of plugin.tx_solr
202
     *
203 41
     * @return void
204
     */
205
    protected function initializeSettings()
206 41
    {
207 41
        /** @var $typoScriptService TypoScriptService */
208
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

208
        $typoScriptService = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(TypoScriptService::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
209 41
210
        // Make sure plugin.tx_solr.settings are available in the view as {settings}
211
        $this->settings = $typoScriptService->convertTypoScriptArrayToPlainArray(
212
            $this->typoScriptConfiguration->getObjectByPathOrDefault('plugin.tx_solr.settings.', [])
213
        );
214
    }
215 41
216
    /**
217
     * Initialize the Solr connection and
218 41
     * test the connection through a ping
219 41
     */
220
    protected function initializeSearch()
221 41
    {
222 41
        /** @var \ApacheSolrForTypo3\Solr\ConnectionManager $solrConnection */
223
        try {
224
            $solrConnection = $this->objectManager->get(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, Util::getLanguageUid(), $this->typoScriptFrontendController->MP);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

224
            $solrConnection = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, Util::getLanguageUid(), $this->typoScriptFrontendController->MP);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
225
            $search = $this->objectManager->get(Search::class, $solrConnection);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

225
            $search = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(Search::class, $solrConnection);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
226
227 36
            $this->searchService = $this->objectManager->get(
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

227
            $this->searchService = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
228
                SearchResultSetService::class,
229 36
                /** @scrutinizer ignore-type */ $this->typoScriptConfiguration,
230 36
                /** @scrutinizer ignore-type */ $search
231
            );
232
        } catch (NoSolrConnectionFoundException $e) {
233 36
            $this->handleSolrUnavailable();
234
        }
235
    }
236
237
    /**
238
     * @return SearchRequestBuilder
239
     */
240
    protected function getSearchRequestBuilder()
241 2
    {
242
        if ($this->searchRequestBuilder === null) {
243 2
            $this->searchRequestBuilder = GeneralUtility::makeInstance(SearchRequestBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration);
244
        }
245 2
246 2
        return $this->searchRequestBuilder;
247
    }
248 2
249
    /**
250
     * Called when the solr server is unavailable.
251
     *
252
     * @return void
253
     */
254
    protected function handleSolrUnavailable()
255
    {
256
        if ($this->typoScriptConfiguration->getLoggingExceptions()) {
257
            /** @var SolrLogManager $logger */
258
            $logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
259
            $logger->log(SolrLogManager::ERROR, 'Solr server is not available');
260
        }
261
    }
262
263
    /**
264
     * Emits signal for various actions
265
     *
266
     * @param string $className Name of the class containing the signal
267
     * @param string $signalName Name of the signal slot
268
     * @param array $signalArguments arguments for the signal slot
269
     *
270
     * @return array
271
     */
272
    protected function emitActionSignal($className, $signalName, array $signalArguments)
273
    {
274
        return $this->signalSlotDispatcher->dispatch($className, $signalName, $signalArguments)[0];
275
    }
276
}
277