Passed
Pull Request — release-11.0.x (#3058)
by Rafael
43:44
created

TypoScript::buildConfigurationArray()   B

Complexity

Conditions 8
Paths 96

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 29
c 3
b 0
f 0
dl 0
loc 44
ccs 17
cts 17
cp 1
rs 8.2114
cc 8
nc 96
nop 3
crap 8
1
<?php
2
namespace ApacheSolrForTypo3\Solr\FrontendEnvironment;
3
4
use ApacheSolrForTypo3\Solr\FrontendEnvironment;
5
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
6
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
7
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationPageResolver;
8
use ApacheSolrForTypo3\Solr\System\Configuration\ExtensionConfiguration;
9
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
10
use TYPO3\CMS\Core\SingletonInterface;
11
use TYPO3\CMS\Core\TypoScript\TemplateService;
12
use TYPO3\CMS\Core\Utility\RootlineUtility;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
use TYPO3\CMS\Frontend\Page\PageRepository;
15
16
class TypoScript implements SingletonInterface
17
{
18
19
    private $configurationObjectCache = [];
20
21
22
    /**
23
     * Loads the TypoScript configuration for a given page id and language.
24
     * Language usage may be disabled to get the default TypoScript
25
     * configuration.
26
     *
27
     * @param int $pageId Id of the (root) page to get the Solr configuration from.
28
     * @param string $path The TypoScript configuration path to retrieve.
29
     * @param int $language System language uid, optional, defaults to 0
30
     * @return TypoScriptConfiguration The Solr configuration for the requested tree.
31
     */
32 179
    public function getConfigurationFromPageId($pageId, $path, $language = 0)
33
    {
34 179
        $pageId = $this->getConfigurationPageIdToUse($pageId);
35
36 179
        $cacheId = md5($pageId . '|' . $path . '|' . $language);
37 179
        if (isset($this->configurationObjectCache[$cacheId])) {
38 70
            return $this->configurationObjectCache[$cacheId];
39
        }
40
41
        // If we're on UID 0, we cannot retrieve a configuration currently.
42
        // getRootline() below throws an exception (since #typo3-60 )
43
        // as UID 0 cannot have any parent rootline by design.
44 179
        if ($pageId == 0) {
45 1
            return $this->configurationObjectCache[$cacheId] = $this->buildTypoScriptConfigurationFromArray([], $pageId, $language, $path);
46
        }
47
48
        /** @var $cache TwoLevelCache */
49 178
        $cache = GeneralUtility::makeInstance(TwoLevelCache::class, /** @scrutinizer ignore-type */ 'tx_solr_configuration');
50 178
        $configurationArray = $cache->get($cacheId);
51
52
53 178
        if (!empty($configurationArray)) {
54
            // we have a cache hit and can return it.
55
            return $this->configurationObjectCache[$cacheId] = $this->buildTypoScriptConfigurationFromArray($configurationArray, $pageId, $language, $path);
56
        }
57
58
        // we have nothing in the cache. We need to build the configurationToUse
59 178
        $configurationArray = $this->buildConfigurationArray($pageId, $path, $language);
60
61 178
        $cache->set($cacheId, $configurationArray);
62
63 178
        return $this->configurationObjectCache[$cacheId] = $this->buildTypoScriptConfigurationFromArray($configurationArray, $pageId, $language, $path);
64
    }
65
66
    /**
67
     * This method retrieves the closest pageId where a configuration is located, when this
68
     * feature is enabled.
69
     *
70
     * @param int $pageId
71
     * @return int
72
     */
73 179
    private function getConfigurationPageIdToUse($pageId)
74
    {
75 179
        $extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class);
76 179
        if ($extensionConfiguration->getIsUseConfigurationFromClosestTemplateEnabled()) {
77
            /** @var $configurationPageResolve ConfigurationPageResolver */
78
            $configurationPageResolver = GeneralUtility::makeInstance(ConfigurationPageResolver::class);
79
            $pageId = $configurationPageResolver->getClosestPageIdWithActiveTemplate($pageId);
80
            return $pageId;
81
        }
82 179
        return $pageId;
83
    }
84
85
    /**
86
     * builds an configuration array, containing the solr configuration.
87
     *
88
     * @param integer $pageId
89
     * @param string $path
90
     * @param integer $language
91
     * @return array
92
     */
93 178
    protected function buildConfigurationArray($pageId, $path, $language)
94
    {
95 178
        if (is_int($language)) {
0 ignored issues
show
introduced by
The condition is_int($language) is always true.
Loading history...
96 178
            GeneralUtility::makeInstance(FrontendEnvironment::class)->changeLanguageContext((int)$pageId, (int)$language);
97
        }
98 178
        $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageId);
99
        try {
100 178
            $rootLine = $rootlineUtility->get();
101 1
        } catch (\RuntimeException $e) {
102 1
            $rootLine = [];
103
        }
104
105
        $initializedTsfe = false;
106 178
        $initializedPageSelect = false;
107 178
        if (empty($GLOBALS['TSFE']->sys_page)) {
108 178
            /** @var $pageSelect PageRepository */
109 178
            $pageSelect = GeneralUtility::makeInstance(PageRepository::class);
110
            if (empty($GLOBALS['TSFE'])) {
111 178
                $GLOBALS['TSFE'] = new \stdClass();
112 178
                $GLOBALS['TSFE']->tmpl = new \stdClass();
113
                $GLOBALS['TSFE']->tmpl->rootLine = $rootLine;
114 178
                $GLOBALS['TSFE']->id = $pageId;
115
                $initializedTsfe = true;
116
            }
117
            $GLOBALS['TSFE']->sys_page = $pageSelect;
118
            $initializedPageSelect = true;
119
        }
120
121
        /** @var $tmpl TemplateService */
122
        $tmpl = GeneralUtility::makeInstance(TemplateService::class);
123
        $tmpl->tt_track = false; // Do not log time-performance information
124
        $tmpl->start($rootLine); // This generates the constants/config + hierarchy info for the template.
125
126 179
        $getConfigurationFromInitializedTSFEAndWriteToCache = $this->ext_getSetup($tmpl->setup, $path);
127
        $configurationToUse = $getConfigurationFromInitializedTSFEAndWriteToCache[0];
128 179
129 179
        if ($initializedPageSelect) {
130
            $GLOBALS['TSFE']->sys_page = null;
131
        }
132
        if ($initializedTsfe) {
133
            unset($GLOBALS['TSFE']);
134
        }
135
136
        return is_array($configurationToUse) ? $configurationToUse : [];
137
    }
138
139
140
    /**
141
     * @param array $theSetup
142
     * @param string $theKey
143
     * @return array
144
     */
145
    public function ext_getSetup($theSetup, $theKey)
146
    {
147
        $parts = explode('.', $theKey, 2);
148
        if ((string)$parts[0] !== '' && is_array($theSetup[$parts[0] . '.'])) {
149
            if (trim($parts[1]) !== '') {
150
                return $this->ext_getSetup($theSetup[$parts[0] . '.'], trim($parts[1]));
151
            }
152
            return [$theSetup[$parts[0] . '.'], $theSetup[$parts[0]]];
153
        }
154
        if (trim($theKey) !== '') {
155
            return [[], $theSetup[$theKey]];
156
        }
157
        return [$theSetup, ''];
158
    }
159
160
    /**
161
     * Builds the configuration object from a config array and returns it.
162
     *
163
     * @param array $configurationToUse
164
     * @param int $pageId
165
     * @param int $languageId
166
     * @param string $typoScriptPath
167
     * @return TypoScriptConfiguration
168
     */
169
    protected function buildTypoScriptConfigurationFromArray(array $configurationToUse, $pageId, $languageId, $typoScriptPath)
170
    {
171
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
172
        return $configurationManager->getTypoScriptConfiguration($configurationToUse, $pageId, $languageId, $typoScriptPath);
173
    }
174
175
}
176