Passed
Pull Request — release-11.0.x (#3051)
by
unknown
41:12
created

TypoScript::buildConfigurationArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 9.8333
cc 4
nc 8
nop 3
crap 4
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
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
        /** @var $tmpl TemplateService */
106 178
        $tmpl = GeneralUtility::makeInstance(TemplateService::class);
107 178
        $tmpl->tt_track = false; // Do not log time-performance information
108 178
        $tmpl->start($rootLine); // This generates the constants/config + hierarchy info for the template.
109 178
110
        $getConfigurationFromInitializedTSFEAndWriteToCache = $this->ext_getSetup($tmpl->setup, $path);
111 178
        $configurationToUse = $getConfigurationFromInitializedTSFEAndWriteToCache[0];
112 178
113
        return is_array($configurationToUse) ? $configurationToUse : [];
114 178
    }
115
116
117
    /**
118
     * @param array $theSetup
119
     * @param string $theKey
120
     * @return array
121
     */
122
    public function ext_getSetup($theSetup, $theKey)
123
    {
124
        $parts = explode('.', $theKey, 2);
125
        if ((string)$parts[0] !== '' && is_array($theSetup[$parts[0] . '.'])) {
126 179
            if (trim($parts[1]) !== '') {
127
                return $this->ext_getSetup($theSetup[$parts[0] . '.'], trim($parts[1]));
128 179
            }
129 179
            return [$theSetup[$parts[0] . '.'], $theSetup[$parts[0]]];
130
        }
131
        if (trim($theKey) !== '') {
132
            return [[], $theSetup[$theKey]];
133
        }
134
        return [$theSetup, ''];
135
    }
136
137
    /**
138
     * Builds the configuration object from a config array and returns it.
139
     *
140
     * @param array $configurationToUse
141
     * @param int $pageId
142
     * @param int $languageId
143
     * @param string $typoScriptPath
144
     * @return TypoScriptConfiguration
145
     */
146
    protected function buildTypoScriptConfigurationFromArray(array $configurationToUse, $pageId, $languageId, $typoScriptPath)
147
    {
148
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
149
        return $configurationManager->getTypoScriptConfiguration($configurationToUse, $pageId, $languageId, $typoScriptPath);
150
    }
151
152
}
153