TYPO3 CMS  TYPO3_8-7
ConfigurationManager.php
Go to the documentation of this file.
1 <?php
2 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 
24 
32 {
33 
37  protected $cache;
38 
42  protected $yamlSource;
43 
48  public function injectYamlSource(\TYPO3\CMS\Form\Mvc\Configuration\YamlSource $yamlSource)
49  {
50  $this->yamlSource = $yamlSource;
51  }
52 
60  public function getConfiguration($configurationType, $extensionName = null, $pluginName = null)
61  {
62  switch ($configurationType) {
63  case self::CONFIGURATION_TYPE_YAML_SETTINGS:
64  return $this->getConfigurationFromYamlFile($extensionName);
65  default:
66  return parent::getConfiguration($configurationType, $extensionName, $pluginName);
67  }
68  }
69 
88  protected function getConfigurationFromYamlFile(string $extensionName): array
89  {
90  if (empty($extensionName)) {
92  'Please specify an extension key to load a YAML configuration',
93  1471473377
94  );
95  }
96  $ucFirstExtensioName = ucfirst($extensionName);
97 
98  $typoscriptSettings = $this->getTypoScriptSettings($extensionName);
99 
100  $yamlSettingsFilePaths = isset($typoscriptSettings['yamlConfigurations'])
101  ? ArrayUtility::sortArrayWithIntegerKeys($typoscriptSettings['yamlConfigurations'])
102  : [];
103 
104  $cacheKeySuffix = $extensionName . md5(json_encode($yamlSettingsFilePaths));
105 
106  $yamlSettings = $this->getYamlSettingsFromCache($cacheKeySuffix);
107  if (!empty($yamlSettings)) {
108  return $this->overrideConfigurationByTypoScript($yamlSettings, $extensionName);
109  }
110 
111  $yamlSettings = InheritancesResolverService::create($this->yamlSource->load($yamlSettingsFilePaths))
112  ->getResolvedConfiguration();
113 
114  $yamlSettings = ArrayUtility::removeNullValuesRecursive($yamlSettings);
115  $yamlSettings = is_array($yamlSettings['TYPO3']['CMS'][$ucFirstExtensioName])
116  ? $yamlSettings['TYPO3']['CMS'][$ucFirstExtensioName]
117  : [];
118  $yamlSettings = ArrayUtility::sortArrayWithIntegerKeysRecursive($yamlSettings);
119  $this->setYamlSettingsIntoCache($cacheKeySuffix, $yamlSettings);
120 
121  return $this->overrideConfigurationByTypoScript($yamlSettings, $extensionName);
122  }
123 
129  protected function overrideConfigurationByTypoScript(array $yamlSettings, string $extensionName): array
130  {
131  $typoScript = parent::getConfiguration(self::CONFIGURATION_TYPE_SETTINGS, $extensionName);
132  if (is_array($typoScript['yamlSettingsOverrides']) && !empty($typoScript['yamlSettingsOverrides'])) {
134  $yamlSettings,
135  $typoScript['yamlSettingsOverrides']
136  );
137 
138  if ($this->environmentService->isEnvironmentInFrontendMode()) {
139  $yamlSettings = $this->objectManager->get(TypoScriptService::class)
140  ->resolvePossibleTypoScriptConfiguration($yamlSettings);
141  }
142  }
143  return $yamlSettings;
144  }
145 
149  protected function getCacheFrontend(): FrontendInterface
150  {
151  if ($this->cache === null) {
152  $this->cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('assets');
153  }
154  return $this->cache;
155  }
156 
161  protected function getConfigurationCacheKey(string $cacheKeySuffix): string
162  {
163  return strtolower(self::CONFIGURATION_TYPE_YAML_SETTINGS . '_' . $cacheKeySuffix);
164  }
165 
170  protected function getYamlSettingsFromCache(string $cacheKeySuffix)
171  {
172  return $this->getCacheFrontend()->get(
173  $this->getConfigurationCacheKey($cacheKeySuffix)
174  );
175  }
176 
181  protected function setYamlSettingsIntoCache(
182  string $cacheKeySuffix,
183  array $yamlSettings
184  ) {
185  $this->getCacheFrontend()->set(
186  $this->getConfigurationCacheKey($cacheKeySuffix),
187  $yamlSettings
188  );
189  }
190 
195  protected function getTypoScriptSettings(string $extensionName)
196  {
197  return parent::getConfiguration(
199  $extensionName
200  );
201  }
202 }
getConfiguration($configurationType, $extensionName=null, $pluginName=null)
static sortArrayWithIntegerKeysRecursive(array $array)
static makeInstance($className,... $constructorArguments)
overrideConfigurationByTypoScript(array $yamlSettings, string $extensionName)
setYamlSettingsIntoCache(string $cacheKeySuffix, array $yamlSettings)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
injectYamlSource(\TYPO3\CMS\Form\Mvc\Configuration\YamlSource $yamlSource)
static sortArrayWithIntegerKeys(array $array)
static removeNullValuesRecursive(array $array)