Passed
Pull Request — master (#1318)
by
unknown
32:46
created

injectConfigurationManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Controller\Frontend;
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\Search;
20
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
21
use ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext;
22
use ApacheSolrForTypo3\Solr\System\Service\ConfigurationService;
23
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager as SolrConfigurationManager;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
26
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
27
use TYPO3\CMS\Extbase\Service\TypoScriptService;
28
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
29
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
30
31
/**
32
 * Class AbstractBaseController
33
 *
34
 * @author Frans Saris <[email protected]>
35
 * @author Timo Hund <[email protected]>
36
 * @package ApacheSolrForTypo3\Solr\Controller
37
 */
38
abstract class AbstractBaseController extends ActionController
39
{
40
    /**
41
     * @var ContentObjectRenderer
42
     */
43
    private $contentObjectRenderer;
44
45
    /**
46
     * @var TypoScriptFrontendController
47
     */
48
    protected $typoScriptFrontendController;
49
50
    /**
51
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
52
     */
53
    protected $configurationManager;
54
55
    /**
56
     * @var SolrConfigurationManager
57
     */
58
    private $solrConfigurationManager;
59
60
    /**
61
     * The configuration is private if you need it please get it from the controllerContext.
62
     *
63
     * @var TypoScriptConfiguration
64
     */
65
    protected $typoScriptConfiguration;
66
67
    /**
68
     * @var \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext
69
     */
70
    protected $controllerContext;
71
72
    /**
73
     * @var \ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService
74
     */
75
    protected $searchService;
76
77
    /**
78
     * @var bool
79
     */
80
    protected $resetConfigurationBeforeInitialize = true;
81
82
    /**
83
     * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
84
     * @return void
85
     */
86
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
87
    {
88
        $this->configurationManager = $configurationManager;
89
        $this->contentObjectRenderer = $this->configurationManager->getContentObject();
90
    }
91
92
    /**
93
     * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObjectRenderer
94
     */
95
    public function setContentObjectRenderer($contentObjectRenderer)
96
    {
97
        $this->contentObjectRenderer = $contentObjectRenderer;
98
    }
99
100
    /**
101
     * @return \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
102
     */
103
    public function getContentObjectRenderer()
104
    {
105
        return $this->contentObjectRenderer;
106
    }
107
108
    /**
109
     * @param SolrConfigurationManager $configurationManager
110
     */
111
    public function injectSolrConfigurationManager(SolrConfigurationManager $configurationManager)
112
    {
113
        $this->solrConfigurationManager = $configurationManager;
114
    }
115
116
    /**
117
     * @param boolean $resetConfigurationBeforeInitialize
118
     */
119
    public function setResetConfigurationBeforeInitialize($resetConfigurationBeforeInitialize)
120
    {
121
        $this->resetConfigurationBeforeInitialize = $resetConfigurationBeforeInitialize;
122
    }
123
124
    /**
125
     * Initialize the controller context
126
     *
127
     * @return \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext ControllerContext to be passed to the view
128
     * @api
129
     */
130
    protected function buildControllerContext()
131
    {
132
        /** @var $controllerContext \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext */
133
        $controllerContext = $this->objectManager->get(SolrControllerContext::class);
134
        $controllerContext->setRequest($this->request);
135
        $controllerContext->setResponse($this->response);
136
        if ($this->arguments !== null) {
137
            $controllerContext->setArguments($this->arguments);
138
        }
139
        $controllerContext->setUriBuilder($this->uriBuilder);
140
141
        $controllerContext->setTypoScriptConfiguration($this->typoScriptConfiguration);
142
143
        return $controllerContext;
144
    }
145
146
    /**
147
     * Initialize action
148
     */
149
    protected function initializeAction()
150
    {
151
        // Reset configuration (to reset flexform overrides) if resetting is enabled
152
        if ($this->resetConfigurationBeforeInitialize) {
153
            $this->solrConfigurationManager->reset();
154
        }
155
        /** @var TypoScriptService $typoScriptService */
156
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
157
158
        // Merge settings done by typoscript with solrConfiguration plugin.tx_solr (obsolete when part of ext:solr)
159
        $frameWorkConfiguration = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
160
        $pluginSettings = [];
161
        foreach (['search', 'settings', 'suggest', 'statistics', 'logging', 'general', 'solr', 'view'] as $key) {
162
            if (isset($frameWorkConfiguration[$key])) {
163
                $pluginSettings[$key] = $frameWorkConfiguration[$key];
164
            }
165
        }
166
167
        $this->typoScriptConfiguration = $this->solrConfigurationManager->getTypoScriptConfiguration();
168
        if ($pluginSettings !== []) {
169
            $this->typoScriptConfiguration->mergeSolrConfiguration(
170
                $typoScriptService->convertPlainArrayToTypoScriptArray($pluginSettings),
171
                true,
172
                false
173
            );
174
        }
175
176
        $this->objectManager->get(ConfigurationService::class)
177
            ->overrideConfigurationWithFlexFormSettings(
178
                $this->contentObjectRenderer->data['pi_flexform'],
179
                $this->typoScriptConfiguration
180
            );
181
182
        parent::initializeAction();
183
        $this->typoScriptFrontendController = $GLOBALS['TSFE'];
184
        $this->initializeSettings();
185
        $this->initializeSearch();
186
    }
187
188
    /**
189
     * Inject settings of plugin.tx_solr
190
     *
191
     * @return void
192
     */
193
    protected function initializeSettings()
194
    {
195
        /** @var $typoScriptService TypoScriptService */
196
        $typoScriptService = $this->objectManager->get(TypoScriptService::class);
197
198
        // Make sure plugin.tx_solr.settings are available in the view as {settings}
199
        $this->settings = $typoScriptService->convertTypoScriptArrayToPlainArray(
200
            $this->typoScriptConfiguration->getObjectByPathOrDefault('plugin.tx_solr.settings.', [])
201
        );
202
    }
203
204
    /**
205
     * Initialize the Solr connection and
206
     * test the connection through a ping
207
     */
208
    protected function initializeSearch()
209
    {
210
        /** @var \ApacheSolrForTypo3\Solr\ConnectionManager $solrConnection */
211
        $solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, $this->typoScriptFrontendController->sys_language_uid, $this->typoScriptFrontendController->MP);
212
        $search = GeneralUtility::makeInstance(Search::class, $solrConnection);
213
214
        $this->searchService = GeneralUtility::makeInstance(SearchResultSetService::class, $this->typoScriptConfiguration, $search);
215
    }
216
}
217