|
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
|
|
|
public function getConfigurationFromPageId($pageId, $path, $language = 0) |
|
33
|
|
|
{ |
|
34
|
|
|
$pageId = $this->getConfigurationPageIdToUse($pageId); |
|
35
|
|
|
|
|
36
|
|
|
$cacheId = md5($pageId . '|' . $path . '|' . $language); |
|
37
|
|
|
if (isset($this->configurationObjectCache[$cacheId])) { |
|
38
|
|
|
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
|
|
|
if ($pageId == 0) { |
|
45
|
|
|
return $this->configurationObjectCache[$cacheId] = $this->buildTypoScriptConfigurationFromArray([], $pageId, $language, $path); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** @var $cache TwoLevelCache */ |
|
49
|
|
|
$cache = GeneralUtility::makeInstance(TwoLevelCache::class, /** @scrutinizer ignore-type */ 'tx_solr_configuration'); |
|
50
|
|
|
$configurationArray = $cache->get($cacheId); |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
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
|
|
|
$configurationArray = $this->buildConfigurationArray($pageId, $path, $language); |
|
60
|
|
|
|
|
61
|
|
|
$cache->set($cacheId, $configurationArray); |
|
62
|
|
|
|
|
63
|
|
|
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
|
|
|
private function getConfigurationPageIdToUse($pageId) |
|
74
|
|
|
{ |
|
75
|
|
|
$extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class); |
|
76
|
|
|
if ($extensionConfiguration->getIsUseConfigurationFromClosestTemplateEnabled()) { |
|
77
|
|
|
/** @var $configurationPageResolve ConfigurationPageResolver */ |
|
78
|
|
|
$configurationPageResolver = GeneralUtility::makeInstance(ConfigurationPageResolver::class); |
|
79
|
|
|
$pageId = $configurationPageResolver->getClosestPageIdWithActiveTemplate($pageId); |
|
80
|
|
|
return $pageId; |
|
81
|
|
|
} |
|
82
|
|
|
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
|
|
|
private function buildConfigurationArray($pageId, $path, $language) |
|
94
|
|
|
{ |
|
95
|
|
|
if (is_int($language)) { |
|
|
|
|
|
|
96
|
|
|
GeneralUtility::makeInstance(FrontendEnvironment::class)->changeLanguageContext((int)$pageId, (int)$language); |
|
97
|
|
|
} |
|
98
|
|
|
$rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageId); |
|
|
|
|
|
|
99
|
|
|
try { |
|
100
|
|
|
$rootLine = $rootlineUtility->get(); |
|
101
|
|
|
} catch (\RuntimeException $e) { |
|
102
|
|
|
$rootLine = []; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** @var $tmpl ExtendedTemplateService */ |
|
106
|
|
|
$tmpl = GeneralUtility::makeInstance(ExtendedTemplateService::class); |
|
107
|
|
|
$tmpl->tt_track = false; // Do not log time-performance information |
|
108
|
|
|
$tmpl->runThroughTemplates($rootLine); // This generates the constants/config + hierarchy info for the template. |
|
109
|
|
|
$tmpl->generateConfig(); |
|
110
|
|
|
|
|
111
|
|
|
$getConfigurationFromInitializedTSFEAndWriteToCache = $tmpl->ext_getSetup($tmpl->setup, $path); |
|
112
|
|
|
$configurationToUse = $getConfigurationFromInitializedTSFEAndWriteToCache[0]; |
|
113
|
|
|
|
|
114
|
|
|
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
|
|
|
private function buildTypoScriptConfigurationFromArray(array $configurationToUse, $pageId, $languageId, $typoScriptPath) |
|
127
|
|
|
{ |
|
128
|
|
|
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class); |
|
129
|
|
|
return $configurationManager->getTypoScriptConfiguration($configurationToUse, $pageId, $languageId, $typoScriptPath); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
} |