[TASK] Unify spelling of "YAML" throughout the core
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Mvc / Configuration / ConfigurationManager.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Mvc\Configuration;
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 use TYPO3\CMS\Core\Utility\ArrayUtility;
19 use TYPO3\CMS\Extbase\Configuration\ConfigurationManager as ExtbaseConfigurationManager;
20 use TYPO3\CMS\Form\Mvc\Configuration\Exception\ExtensionNameRequiredException;
21
22 /**
23 * Extend the ExtbaseConfigurationManager to read YAML configurations.
24 *
25 * Scope: frontend / backend
26 * @internal
27 */
28 class ConfigurationManager extends ExtbaseConfigurationManager implements ConfigurationManagerInterface
29 {
30 /**
31 * @var \TYPO3\CMS\Form\Mvc\Configuration\YamlSource
32 */
33 protected $yamlSource;
34
35 /**
36 * 1st level configuration cache
37 *
38 * @var array
39 */
40 protected $configurationCache = [];
41
42 /**
43 * @param \TYPO3\CMS\Form\Mvc\Configuration\YamlSource $yamlSource
44 * @internal
45 */
46 public function injectYamlSource(\TYPO3\CMS\Form\Mvc\Configuration\YamlSource $yamlSource)
47 {
48 $this->yamlSource = $yamlSource;
49 }
50
51 /**
52 * @param string $configurationType The kind of configuration to fetch - must be one of the CONFIGURATION_TYPE_* constants
53 * @param string $extensionName if specified, the configuration for the given extension will be returned.
54 * @param string $pluginName if specified, the configuration for the given plugin will be returned.
55 * @return array The configuration
56 * @internal
57 */
58 public function getConfiguration($configurationType, $extensionName = null, $pluginName = null)
59 {
60 switch ($configurationType) {
61 case self::CONFIGURATION_TYPE_YAML_SETTINGS:
62 return $this->getConfigurationFromYamlFile($extensionName);
63 default:
64 return parent::getConfiguration($configurationType, $extensionName, $pluginName);
65 }
66 }
67
68 /**
69 * Load and parse YAML files which are configured within the TypoScript
70 * path plugin.tx_extensionkey.settings.yamlConfigurations
71 *
72 * The following steps will be done:
73 *
74 * * Convert each singe YAML file into an array
75 * * merge this arrays together
76 * * resolve all declared inheritances
77 * * remove all keys if their values are NULL
78 * * return all configuration paths within TYPO3.CMS
79 * * sort by array keys, if all keys within the current nesting level are numerical keys
80 * * resolve possible TypoScript settings in FE mode
81 *
82 * @param string $extensionName
83 * @return array
84 * @throws ExtensionNameRequiredException
85 */
86 protected function getConfigurationFromYamlFile(string $extensionName): array
87 {
88 if (empty($extensionName)) {
89 throw new ExtensionNameRequiredException(
90 'Please specify an extension key to load a YAML configuration',
91 1471473377
92 );
93 }
94 $ucFirstExtensioName = ucfirst($extensionName);
95
96 // 1st level cache
97 $configurationCacheKey = strtolower(self::CONFIGURATION_TYPE_YAML_SETTINGS . '|' . $extensionName);
98 if (isset($this->configurationCache[$configurationCacheKey])) {
99 return $this->configurationCache[$configurationCacheKey];
100 }
101
102 $typoscriptSettings = parent::getConfiguration(
103 ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
104 $extensionName
105 );
106 $yamlSettingsFilePaths = isset($typoscriptSettings['yamlConfigurations'])
107 ? ArrayUtility::sortArrayWithIntegerKeys($typoscriptSettings['yamlConfigurations'])
108 : [];
109 $yamlSettings = InheritancesResolverService::create($this->yamlSource->load($yamlSettingsFilePaths))
110 ->getResolvedConfiguration();
111
112 $yamlSettings = ArrayUtility::removeNullValuesRecursive($yamlSettings);
113 $yamlSettings = is_array($yamlSettings['TYPO3']['CMS'][$ucFirstExtensioName])
114 ? $yamlSettings['TYPO3']['CMS'][$ucFirstExtensioName]
115 : [];
116 $yamlSettings = ArrayUtility::sortArrayWithIntegerKeysRecursive($yamlSettings);
117 $yamlSettings = $this->overrideConfigurationByTypoScript($yamlSettings, $extensionName);
118
119 // 1st level cache
120 $this->configurationCache[$configurationCacheKey] = $yamlSettings;
121 return $yamlSettings;
122 }
123
124 /**
125 * @param array $yamlSettings
126 * @param string $extensionName
127 * @return array
128 */
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'])) {
133 ArrayUtility::mergeRecursiveWithOverrule(
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 }