Completed
Push — master ( 1a818c...84aa0d )
by Timo
90:37 queued 87:52
created

TypoScript::buildConfigurationArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0466

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 22
ccs 12
cts 14
cp 0.8571
rs 9.7998
cc 4
nc 8
nop 3
crap 4.0466
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\ExtendedTemplateService;
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 171
    public function getConfigurationFromPageId($pageId, $path, $language = 0)
33
    {
34 171
        $pageId = $this->getConfigurationPageIdToUse($pageId);
35
36 171
        $cacheId = md5($pageId . '|' . $path . '|' . $language);
37 171
        if (isset($this->configurationObjectCache[$cacheId])) {
38 71
            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 171
        if ($pageId == 0) {
45 2
            return $this->configurationObjectCache[$cacheId] = $this->buildTypoScriptConfigurationFromArray([], $pageId, $language, $path);
46
        }
47
48
        /** @var $cache TwoLevelCache */
49 169
        $cache = GeneralUtility::makeInstance(TwoLevelCache::class, /** @scrutinizer ignore-type */ 'tx_solr_configuration');
50 169
        $configurationArray = $cache->get($cacheId);
51
52
53 169
        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 169
        $configurationArray = $this->buildConfigurationArray($pageId, $path, $language);
60
61 169
        $cache->set($cacheId, $configurationArray);
62
63 169
        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 171
    private function getConfigurationPageIdToUse($pageId)
74
    {
75 171
        $extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class);
76 171
        if ($extensionConfiguration->getIsUseConfigurationFromClosestTemplateEnabled()) {
77
            /** @var $configurationPageResolve ConfigurationPageResolver */
78
            $configurationPageResolver = GeneralUtility::makeInstance(ConfigurationPageResolver::class);
79
            $pageId = $configurationPageResolver->getClosestPageIdWithActiveTemplate($pageId);
80
            return $pageId;
81
        }
82 171
        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 169
    private function buildConfigurationArray($pageId, $path, $language)
94
    {
95 169
        if (is_int($language)) {
0 ignored issues
show
introduced by
The condition is_int($language) is always true.
Loading history...
96 169
            GeneralUtility::makeInstance(FrontendEnvironment::class)->changeLanguageContext((int)$pageId, (int)$language);
97
        }
98 169
        $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageId);
99
        try {
100 169
            $rootLine = $rootlineUtility->get();
101
        } catch (\RuntimeException $e) {
102
            $rootLine = [];
103
        }
104
105
        /** @var $tmpl ExtendedTemplateService */
106 169
        $tmpl = GeneralUtility::makeInstance(ExtendedTemplateService::class);
107 169
        $tmpl->tt_track = false; // Do not log time-performance information
108 169
        $tmpl->runThroughTemplates($rootLine); // This generates the constants/config + hierarchy info for the template.
109 169
        $tmpl->generateConfig();
110
111 169
        $getConfigurationFromInitializedTSFEAndWriteToCache = $tmpl->ext_getSetup($tmpl->setup, $path);
112 169
        $configurationToUse = $getConfigurationFromInitializedTSFEAndWriteToCache[0];
113
114 169
        return is_array($configurationToUse) ? $configurationToUse : [];
115
    }
116
117
    /**
118
     * Builds the configuration object from a config array and returns it.
119
     *
120
     * @param array $configurationToUse
121
     * @param int $pageId
122
     * @param int $languageId
123
     * @param string $typoScriptPath
124
     * @return TypoScriptConfiguration
125
     */
126 171
    private function buildTypoScriptConfigurationFromArray(array $configurationToUse, $pageId, $languageId, $typoScriptPath)
127
    {
128 171
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
129 171
        return $configurationManager->getTypoScriptConfiguration($configurationToUse, $pageId, $languageId, $typoScriptPath);
130
    }
131
132
}