‪TYPO3CMS  ‪main
ConfigurationManager.php
Go to the documentation of this file.
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 
19 
20 use Psr\Http\Message\ServerRequestInterface;
26 use ‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager as ExtbaseConfigurationManager;
28 
35 class ‪ConfigurationManager extends ExtbaseConfigurationManager implements ‪ConfigurationManagerInterface
36 {
40  protected ‪$cache;
41 
45  protected ‪$yamlSource;
46 
51  {
52  $this->yamlSource = ‪$yamlSource;
53  }
54 
62  public function ‪getConfiguration(string $configurationType, ?string $extensionName = null, ?string $pluginName = null): array
63  {
64  switch ($configurationType) {
66  return $this->‪getConfigurationFromYamlFile($extensionName);
67  default:
68  return parent::getConfiguration($configurationType, $extensionName, $pluginName);
69  }
70  }
71 
89  protected function ‪getConfigurationFromYamlFile(string $extensionName): array
90  {
91  if (empty($extensionName)) {
93  'Please specify an extension key to load a YAML configuration',
94  1471473377
95  );
96  }
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 = ArrayUtility::sortArrayWithIntegerKeysRecursive($yamlSettings);
116  $this->‪setYamlSettingsIntoCache($cacheKeySuffix, $yamlSettings);
117 
118  return $this->‪overrideConfigurationByTypoScript($yamlSettings, $extensionName);
119  }
120 
121  protected function ‪overrideConfigurationByTypoScript(array $yamlSettings, string $extensionName): array
122  {
123  $typoScript = parent::getConfiguration(self::CONFIGURATION_TYPE_SETTINGS, $extensionName);
124  if (is_array($typoScript['yamlSettingsOverrides'] ?? null) && !empty($typoScript['yamlSettingsOverrides'])) {
125  $yamlSettingsOverrides = $typoScript['yamlSettingsOverrides'];
126  if ((‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
127  && ‪ApplicationType::fromRequest(‪$GLOBALS['TYPO3_REQUEST'])->isFrontend()
128  ) {
129  $yamlSettingsOverrides = GeneralUtility::makeInstance(TypoScriptService::class)
130  ->resolvePossibleTypoScriptConfiguration($yamlSettingsOverrides);
131  }
132 
133  ArrayUtility::mergeRecursiveWithOverrule(
134  $yamlSettings,
135  $yamlSettingsOverrides
136  );
137  }
138  return $yamlSettings;
139  }
140 
141  protected function ‪getCacheFrontend(): ‪FrontendInterface
142  {
143  if ($this->cache === null) {
144  $this->cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('assets');
145  }
146  return ‪$this->cache;
147  }
148 
149  protected function ‪getConfigurationCacheKey(string $cacheKeySuffix): string
150  {
151  return strtolower(self::CONFIGURATION_TYPE_YAML_SETTINGS . '_' . $cacheKeySuffix);
152  }
153 
157  protected function ‪getYamlSettingsFromCache(string $cacheKeySuffix)
158  {
159  return $this->‪getCacheFrontend()->get(
160  $this->‪getConfigurationCacheKey($cacheKeySuffix)
161  );
162  }
163 
164  protected function ‪setYamlSettingsIntoCache(
165  string $cacheKeySuffix,
166  array $yamlSettings
167  ) {
168  $this->‪getCacheFrontend()->set(
169  $this->‪getConfigurationCacheKey($cacheKeySuffix),
170  $yamlSettings
171  );
172  }
173 
177  protected function ‪getTypoScriptSettings(string $extensionName)
178  {
179  return parent::getConfiguration(
181  $extensionName
182  );
183  }
184 }
‪TYPO3\CMS\Form\Mvc\Configuration
Definition: ConfigurationManager.php:18
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager\$cache
‪FrontendInterface $cache
Definition: ConfigurationManager.php:39
‪TYPO3\CMS\Form\Mvc\Configuration\Exception\ExtensionNameRequiredException
Definition: ExtensionNameRequiredException.php:28
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager\getConfiguration
‪array getConfiguration(string $configurationType, ?string $extensionName=null, ?string $pluginName=null)
Definition: ConfigurationManager.php:60
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:32
‪TYPO3\CMS\Core\Utility\ArrayUtility\removeNullValuesRecursive
‪static removeNullValuesRecursive(array $array)
Definition: ArrayUtility.php:222
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager\overrideConfigurationByTypoScript
‪overrideConfigurationByTypoScript(array $yamlSettings, string $extensionName)
Definition: ConfigurationManager.php:119
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager\getTypoScriptSettings
‪array getTypoScriptSettings(string $extensionName)
Definition: ConfigurationManager.php:175
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager\$yamlSource
‪TYPO3 CMS Form Mvc Configuration YamlSource $yamlSource
Definition: ConfigurationManager.php:43
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager\setYamlSettingsIntoCache
‪setYamlSettingsIntoCache(string $cacheKeySuffix, array $yamlSettings)
Definition: ConfigurationManager.php:162
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_SETTINGS
‪const CONFIGURATION_TYPE_SETTINGS
Definition: ConfigurationManagerInterface.php:29
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager\getYamlSettingsFromCache
‪mixed getYamlSettingsFromCache(string $cacheKeySuffix)
Definition: ConfigurationManager.php:155
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource
Definition: YamlSource.php:44
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager\getConfigurationCacheKey
‪getConfigurationCacheKey(string $cacheKeySuffix)
Definition: ConfigurationManager.php:147
‪TYPO3\CMS\Form\Mvc\Configuration\InheritancesResolverService\create
‪static create(array $configuration=[])
Definition: InheritancesResolverService.php:97
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager\getCacheFrontend
‪getCacheFrontend()
Definition: ConfigurationManager.php:139
‪TYPO3\CMS\Core\Http\fromRequest
‪@ fromRequest
Definition: ApplicationType.php:66
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_YAML_SETTINGS
‪const CONFIGURATION_TYPE_YAML_SETTINGS
Definition: ConfigurationManagerInterface.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager\injectYamlSource
‪injectYamlSource(YamlSource $yamlSource)
Definition: ConfigurationManager.php:48
‪TYPO3\CMS\Core\Http\ApplicationType
‪ApplicationType
Definition: ApplicationType.php:55
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager\getConfigurationFromYamlFile
‪getConfigurationFromYamlFile(string $extensionName)
Definition: ConfigurationManager.php:87
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:36
‪TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:27