1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\FrontendEnvironment; |
3
|
|
|
|
4
|
|
|
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache; |
5
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager; |
6
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
7
|
|
|
use Doctrine\DBAL\Driver\Exception as DBALDriverException; |
8
|
|
|
use TYPO3\CMS\Core\Error\Http\InternalServerErrorException; |
9
|
|
|
use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException; |
10
|
|
|
use TYPO3\CMS\Core\Exception\SiteNotFoundException; |
11
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
12
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
13
|
|
|
|
14
|
|
|
class TypoScript implements SingletonInterface |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
private $configurationObjectCache = []; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Loads the TypoScript configuration for a given page id and language. |
22
|
|
|
* Language usage may be disabled to get the default TypoScript |
23
|
|
|
* configuration. |
24
|
|
|
* |
25
|
|
|
* @param int $pageId The page id of the (root) page to get the Solr configuration from. |
26
|
|
|
* @param string $path The TypoScript configuration path to retrieve. |
27
|
|
|
* @param int $language System language uid, optional, defaults to 0 |
28
|
|
|
* @param int|null $rootPageId |
29
|
|
|
* |
30
|
|
|
* @return TypoScriptConfiguration The Solr configuration for the requested tree. |
31
|
|
|
* |
32
|
|
|
* @throws DBALDriverException |
33
|
|
|
*/ |
34
|
145 |
|
public function getConfigurationFromPageId(int $pageId, string $path, int $language = 0, ?int $rootPageId = null): TypoScriptConfiguration |
35
|
|
|
{ |
36
|
145 |
|
$cacheId = md5($pageId . '|' . $path . '|' . $language); |
37
|
145 |
|
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. |
42
|
|
|
// TSFE can not be initialized for UID = 0 |
43
|
|
|
// getRootline() below throws an exception (since #typo3-60 ) |
44
|
|
|
// as UID 0 cannot have any parent rootline by design. |
45
|
145 |
|
if ($pageId === 0 && $rootPageId === null) { |
46
|
|
|
return $this->configurationObjectCache[$cacheId] = $this->buildTypoScriptConfigurationFromArray([], $pageId, $language, $path); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/* @var TwoLevelCache $cache */ |
50
|
145 |
|
$cache = GeneralUtility::makeInstance(TwoLevelCache::class, /** @scrutinizer ignore-type */ 'tx_solr_configuration'); |
51
|
145 |
|
$configurationArray = $cache->get($cacheId); |
52
|
|
|
|
53
|
|
|
|
54
|
145 |
|
if (!empty($configurationArray)) { |
55
|
|
|
// we have a cache hit and can return it. |
56
|
|
|
return $this->configurationObjectCache[$cacheId] = $this->buildTypoScriptConfigurationFromArray($configurationArray, $pageId, $language, $path); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// we have nothing in the cache. We need to build the configurationToUse |
60
|
145 |
|
$configurationArray = $this->buildConfigurationArray($pageId, $path, $language); |
61
|
|
|
|
62
|
145 |
|
$cache->set($cacheId, $configurationArray); |
63
|
|
|
|
64
|
145 |
|
return $this->configurationObjectCache[$cacheId] = $this->buildTypoScriptConfigurationFromArray($configurationArray, $pageId, $language, $path); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Builds a configuration array, containing the solr configuration. |
69
|
|
|
* |
70
|
|
|
* @param int $pageId |
71
|
|
|
* @param string $path |
72
|
|
|
* @param int $language |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
* |
76
|
|
|
* @throws DBALDriverException |
77
|
|
|
*/ |
78
|
144 |
|
protected function buildConfigurationArray(int $pageId, string $path, int $language): array |
79
|
|
|
{ |
80
|
|
|
/* @var Tsfe $tsfeManager */ |
81
|
144 |
|
$tsfeManager = GeneralUtility::makeInstance(Tsfe::class); |
82
|
|
|
try { |
83
|
144 |
|
$tsfe = $tsfeManager->getTsfeByPageIdAndLanguageId($pageId, $language); |
84
|
4 |
|
} catch (InternalServerErrorException | ServiceUnavailableException | SiteNotFoundException | Exception\Exception $e) { |
85
|
|
|
// @todo logging! |
86
|
4 |
|
return []; |
87
|
|
|
} |
88
|
143 |
|
$getConfigurationFromInitializedTSFEAndWriteToCache = $this->ext_getSetup($tsfe->tmpl->setup ?? [], $path); |
89
|
143 |
|
return $getConfigurationFromInitializedTSFEAndWriteToCache[0] ?? []; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $theSetup |
95
|
|
|
* @param string $theKey |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
143 |
|
public function ext_getSetup(array $theSetup, string $theKey): array |
99
|
|
|
{ |
100
|
143 |
|
$parts = explode('.', $theKey, 2); |
101
|
143 |
|
if ((string)$parts[0] !== '' && is_array($theSetup[$parts[0] . '.'])) { |
102
|
|
|
if (trim($parts[1]) !== '') { |
103
|
|
|
return $this->ext_getSetup($theSetup[$parts[0] . '.'], trim($parts[1])); |
104
|
|
|
} |
105
|
|
|
return [$theSetup[$parts[0] . '.'], $theSetup[$parts[0]]]; |
106
|
|
|
} |
107
|
143 |
|
if (trim($theKey) !== '') { |
108
|
|
|
return [[], $theSetup[$theKey]]; |
109
|
|
|
} |
110
|
143 |
|
return [$theSetup, '']; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Builds the configuration object from a config array and returns it. |
115
|
|
|
* |
116
|
|
|
* @param array $configurationToUse |
117
|
|
|
* @param int $pageId |
118
|
|
|
* @param int $languageId |
119
|
|
|
* @param string $typoScriptPath |
120
|
|
|
* @return TypoScriptConfiguration |
121
|
|
|
*/ |
122
|
144 |
|
protected function buildTypoScriptConfigurationFromArray(array $configurationToUse, int $pageId, int $languageId, string $typoScriptPath): TypoScriptConfiguration |
123
|
|
|
{ |
124
|
144 |
|
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class); |
125
|
144 |
|
return $configurationManager->getTypoScriptConfiguration($configurationToUse, $pageId, $languageId, $typoScriptPath); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|