2 declare(strict_types
=1);
3 namespace TYPO3\CMS\Install\Service
;
6 * This file is part of the TYPO3 CMS project.
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.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
18 use TYPO3\CMS\Core\Configuration\ConfigurationManager
;
19 use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader
;
20 use TYPO3\CMS\Core\Messaging\FlashMessage
;
21 use TYPO3\CMS\Core\Messaging\FlashMessageQueue
;
22 use TYPO3\CMS\Core\Utility\GeneralUtility
;
25 * Service handling bulk read and write of LocalConfiguration values.
27 * Used by "Configure global settings" / "All configuration" view.
29 class LocalConfigurationValueService
33 * Get up configuration data. Prepares main TYPO3_CONF_VARS
34 * array to be displayed and merges is with the description file
36 * @return array Configuration data
38 public function getCurrentConfigurationData(): array
40 $configurationManager = GeneralUtility
::makeInstance(ConfigurationManager
::class);
41 $localConfiguration = $configurationManager->getMergedLocalConfiguration();
44 $commentArray = $this->getDefaultConfigArrayComments();
46 foreach ($localConfiguration as $sectionName => $section) {
47 if (isset($commentArray[$sectionName])) {
48 $data[$sectionName] = $this->recursiveConfigurationFetching(
50 isset($GLOBALS['TYPO3_CONF_VARS'][$sectionName]) ?
$GLOBALS['TYPO3_CONF_VARS'][$sectionName] : null,
51 $commentArray[$sectionName]
62 * Because configuration entries can be at any sub-array level, we need
63 * to check entries recursively.
65 * @param array $sections
66 * @param array|null $sectionsFromCurrentConfiguration
67 * @param array $descriptions
71 protected function recursiveConfigurationFetching(array $sections, array $sectionsFromCurrentConfiguration, array $descriptions, array $path = []): array
75 foreach ($sections as $key => $value) {
76 if (!isset($descriptions['items'][$key])) {
77 // @todo should we do something here?
81 $descriptionInfo = $descriptions['items'][$key];
82 $descriptionType = $descriptionInfo['type'];
87 if ($descriptionType === 'container') {
88 $data = array_merge($data, $this->recursiveConfigurationFetching($value, $descriptionInfo, $newPath));
89 } elseif (!preg_match('/[' . LF
. CR
. ']/', (string)$value) ||
$descriptionType === 'multiline') {
91 $itemData['key'] = implode('/', $newPath);
92 $itemData['path'] = '[' . implode('][', $newPath) . ']';
93 $itemData['fieldType'] = $descriptionInfo['type'];
94 $itemData['description'] = $descriptionInfo['description'];
95 $itemData['allowedValues'] = $descriptionInfo['allowedValues'];
96 $itemData['differentValueInCurrentConfiguration'] = (!isset($descriptionInfo['compareValuesWithCurrentConfiguration']) ||
97 $descriptionInfo['compareValuesWithCurrentConfiguration']) &&
98 isset($sectionsFromCurrentConfiguration[$key]) &&
99 $value !== $sectionsFromCurrentConfiguration[$key];
100 switch ($descriptionType) {
102 $itemData['type'] = 'textarea';
103 $itemData['value'] = str_replace(['\' . LF . \'', '\' . LF . \''], [LF
, LF
], $value);
106 $itemData['type'] = 'checkbox';
107 $itemData['value'] = $value ?
'1' : '0';
108 $itemData['checked'] = (bool)$value;
111 $itemData['type'] = 'number';
112 $itemData['value'] = (int)$value;
115 $itemData['type'] = 'input';
116 // @todo The line below should be improved when the array handling is introduced in the global settings manager.
117 $itemData['value'] = is_array($value)
118 ?
implode(',', $value)
121 // Check if the setting is a PHP error code, will trigger a view helper in fluid
123 $itemData['type'] = 'input';
124 $itemData['value'] = $value;
125 $itemData['phpErrorCode'] = true;
128 $itemData['type'] = 'input';
129 $itemData['value'] = $value;
140 * Store changed values in LocalConfiguration
142 * @param array $valueList Nested array with key['key'] value
143 * @return FlashMessageQueue
145 public function updateLocalConfigurationValues(array $valueList): FlashMessageQueue
147 $messageQueue = new FlashMessageQueue('install');
148 $configurationPathValuePairs = [];
149 $commentArray = $this->getDefaultConfigArrayComments();
150 $configurationManager = GeneralUtility
::makeInstance(ConfigurationManager
::class);
151 foreach ($valueList as $path => $value) {
152 $oldValue = $configurationManager->getConfigurationValueByPath($path);
153 $pathParts = explode('/', $path);
154 $descriptionData = $commentArray[$pathParts[0]];
156 while ($part = next($pathParts)) {
157 $descriptionData = $descriptionData['items'][$part];
160 $dataType = $descriptionData['type'];
162 if ($dataType === 'multiline') {
163 // Force Unix line breaks in text areas
164 $value = str_replace(CR
, '', $value);
165 // Preserve line breaks
166 $value = str_replace(LF
, '\' . LF . \'', $value);
169 if ($dataType === 'bool') {
170 // When submitting settings in the Install Tool, values that default to "FALSE" or "TRUE"
171 // in EXT:core/Configuration/DefaultConfiguration.php will be sent as "0" resp. "1".
172 $value = $value === '1';
173 $valueHasChanged = (bool)$oldValue !== $value;
174 } elseif ($dataType === 'int') {
175 // Cast integer values to integers (but only for values that can not contain a string as well)
176 $value = (int)$value;
177 $valueHasChanged = (int)$oldValue !== $value;
178 } elseif ($dataType === 'array') {
179 $oldValueAsString = is_array($oldValue)
180 ?
implode(',', $oldValue)
182 $valueHasChanged = $oldValueAsString !== $value;
183 $value = GeneralUtility
::trimExplode(',', $value, true);
185 $valueHasChanged = (string)$oldValue !== (string)$value;
188 // Save if value changed
189 if ($valueHasChanged) {
190 $configurationPathValuePairs[$path] = $value;
192 if (is_bool($value)) {
193 $messageBody = 'New value = ' . ($value ?
'true' : 'false');
194 } elseif (empty($value)) {
195 $messageBody = 'New value = <i>none</i>';
196 } elseif (is_array($value)) {
197 $messageBody = "New value = ['" . implode("', '", $value) . "']";
199 $messageBody = 'New value = ' . $value;
202 $messageQueue->enqueue(new FlashMessage(
208 if (!empty($messageQueue)) {
209 $configurationManager->setLocalConfigurationValuesByPathValuePairs($configurationPathValuePairs);
211 return $messageQueue;
215 * Returns an array of available sections and their description
219 public function getSpeakingSectionNames(): array
224 'EXT' => 'Extension Installation',
226 'GFX' => 'Image Processing',
227 'HTTP' => 'Connection',
234 * Read descriptions from description file
238 protected function getDefaultConfigArrayComments(): array
240 $configurationManager = GeneralUtility
::makeInstance(ConfigurationManager
::class);
241 $fileName = $configurationManager->getDefaultConfigurationDescriptionFileLocation();
242 $fileLoader = GeneralUtility
::makeInstance(YamlFileLoader
::class);
243 return $fileLoader->load($fileName);