Failed Conditions
Pull Request — main (#3637)
by Benni
34:26
created

TypoScript::getConfigurationFromPageId()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 34
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 34
ccs 12
cts 13
cp 0.9231
rs 9.5555
cc 5
nc 4
nop 4
crap 5.0113
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\FrontendEnvironment;
19
20
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
21
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
22
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
23
use Doctrine\DBAL\Exception as DBALException;
24
use TYPO3\CMS\Core\Error\Http\InternalServerErrorException;
25
use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException;
26
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
27
use TYPO3\CMS\Core\SingletonInterface;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Utility\GeneralUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
30
/**
31
 * Class TypoScript
32
 */
33
class TypoScript implements SingletonInterface
34
{
35
    /**
36
     * Holds the TypoScript values for given page-id language and TypoScript path.
37
     */
38
    private array $configurationObjectCache = [];
39
40
    /**
41
     * Loads the TypoScript configuration for a given page-id and language.
42
     * Language usage may be disabled to get the default TypoScript
43
     * configuration.
44
     *
45
     * @param int $pageId The page id of the (root) page to get the Solr configuration from.
46
     * @param string $path The TypoScript configuration path to retrieve.
47
     * @param int $language System language uid, optional, defaults to 0
48
     *
49
     * @return TypoScriptConfiguration The Solr configuration for the requested tree.
50
     *
51
     * @throws DBALException
52
     */
53 203
    public function getConfigurationFromPageId(
54
        int $pageId,
55
        string $path,
56
        int $language = 0,
57
        ?int $rootPageId = null
58
    ): TypoScriptConfiguration {
59 203
        $cacheId = md5($pageId . '|' . $path . '|' . $language);
60 203
        if (isset($this->configurationObjectCache[$cacheId])) {
61 94
            return $this->configurationObjectCache[$cacheId];
62
        }
63
64
        // If we're on UID 0, we cannot retrieve a configuration.
65
        // TSFE can not be initialized for UID = 0
66
        // getRootline() below throws an exception (since #typo3-60 )
67
        // as UID 0 cannot have any parent rootline by design.
68 203
        if ($pageId === 0 && $rootPageId === null) {
69
            return $this->configurationObjectCache[$cacheId] = $this->buildTypoScriptConfigurationFromArray([], $pageId, $language, $path);
70
        }
71
72
        /* @var TwoLevelCache $cache */
73 203
        $cache = GeneralUtility::makeInstance(TwoLevelCache::class, /** @scrutinizer ignore-type */ 'tx_solr_configuration');
74 203
        $configurationArray = $cache->get($cacheId);
75
76 203
        if (!empty($configurationArray)) {
77
            // we have a cache hit and can return it.
78 10
            return $this->configurationObjectCache[$cacheId] = $this->buildTypoScriptConfigurationFromArray($configurationArray, $pageId, $language, $path);
79
        }
80
81
        // we have nothing in the cache. We need to build the configurationToUse
82 203
        $configurationArray = $this->buildConfigurationArray($pageId, $path, $language);
83
84 203
        $cache->set($cacheId, $configurationArray);
85
86 203
        return $this->configurationObjectCache[$cacheId] = $this->buildTypoScriptConfigurationFromArray($configurationArray, $pageId, $language, $path);
87
    }
88
89
    /**
90
     * Builds a configuration array, containing the solr configuration.
91
     *
92
     * @throws DBALException
93
     */
94 202
    protected function buildConfigurationArray(int $pageId, string $path, int $language): array
95
    {
96
        /* @var Tsfe $tsfeManager */
97 202
        $tsfeManager = GeneralUtility::makeInstance(Tsfe::class);
98
        try {
99 202
            $tsfe = $tsfeManager->getTsfeByPageIdAndLanguageId($pageId, $language);
100 6
        } catch (InternalServerErrorException | ServiceUnavailableException | SiteNotFoundException | Exception\Exception $e) {
101
            // @todo logging!
102 6
            return [];
103
        }
104 202
        $getConfigurationFromInitializedTSFEAndWriteToCache = $this->ext_getSetup($tsfe->tmpl->setup ?? [], $path);
105 202
        return $getConfigurationFromInitializedTSFEAndWriteToCache[0] ?? [];
106
    }
107
108
    /**
109
     * Adapted from TYPO3 core
110
     * @see sysext:core/Classes/TypoScript/ExtendedTemplateService until TYPO3 v11
111
     */
112 202
    public function ext_getSetup(array $theSetup, string $theKey): array
113
    {
114
        // 'a.b.c' --> ['a', 'b.c']
115 202
        $parts = explode('.', $theKey, 2);
116 202
        if ($parts[0] !== '' && is_array($theSetup[$parts[0] . '.'])) {
117
            if (trim($parts[1] ?? '') !== '') {
118
                // Current path segment is a sub array, check it recursively by applying the rest of the key
119
                return $this->ext_getSetup($theSetup[$parts[0] . '.'], trim($parts[1] ?? ''));
120
            }
121
            // No further path to evaluate, return current setup and the value for the current path segment - if any
122
            return [$theSetup[$parts[0] . '.'], $theSetup[$parts[0]] ?? ''];
123
        }
124 202
        if (trim($theKey) !== '') {
125
            return [[], $theSetup[$theKey]];
126
        }
127 202
        return [$theSetup, ''];
128
    }
129
130
    /**
131
     * Builds the configuration object from a config array and returns it.
132
     */
133 202
    protected function buildTypoScriptConfigurationFromArray(array $configurationToUse, int $pageId, int $languageId, string $typoScriptPath): TypoScriptConfiguration
134
    {
135 202
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
136 202
        return $configurationManager->getTypoScriptConfiguration($configurationToUse, $pageId, $languageId, $typoScriptPath);
137
    }
138
}
139