[TASK] BACK_PATH BE controller
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / ConfigurationUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\TypoScript\ConfigurationForm;
18
19 /**
20 * Utility for dealing with ext_emconf and ext_conf_template settings
21 */
22 class ConfigurationUtility implements \TYPO3\CMS\Core\SingletonInterface {
23
24 /**
25 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
26 */
27 protected $objectManager;
28
29 /**
30 * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
31 */
32 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) {
33 $this->objectManager = $objectManager;
34 }
35
36 /**
37 * Get default configuration from ext_conf_template of an extension
38 * and save as initial configuration to LocalConfiguration ['EXT']['extConf'].
39 *
40 * Used by the InstallUtility to initialize local extension config.
41 *
42 * @param string $extensionKey Extension key
43 * @return void
44 */
45 public function saveDefaultConfiguration($extensionKey) {
46 $currentConfiguration = $this->getCurrentConfiguration($extensionKey);
47 $nestedConfiguration = $this->convertValuedToNestedConfiguration($currentConfiguration);
48 $this->writeConfiguration($nestedConfiguration, $extensionKey);
49 }
50
51 /**
52 * Writes extension specific configuration to LocalConfiguration file
53 * in array ['EXT']['extConf'][$extensionKey].
54 *
55 * Removes core cache files afterwards.
56 *
57 * This low level method expects a nested configuration array that
58 * was already merged with default configuration and maybe new form values.
59 *
60 * @param array $configuration Configuration to save
61 * @param string $extensionKey Extension key
62 * @return void
63 */
64 public function writeConfiguration(array $configuration = array(), $extensionKey) {
65 /** @var $configurationManager \TYPO3\CMS\Core\Configuration\ConfigurationManager */
66 $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
67 $configurationManager->setLocalConfigurationValueByPath('EXT/extConf/' . $extensionKey, serialize($configuration));
68 }
69
70 /**
71 * Get current configuration of an extension. Will return the configuration as a valued object
72 *
73 * @param string $extensionKey
74 * @return array
75 */
76 public function getCurrentConfiguration($extensionKey) {
77 $mergedConfiguration = $this->getDefaultConfigurationFromExtConfTemplateAsValuedArray($extensionKey);
78 $currentExtensionConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey]);
79 $currentExtensionConfig = is_array($currentExtensionConfig) ? $currentExtensionConfig : array();
80 $currentExtensionConfig = $this->convertNestedToValuedConfiguration($currentExtensionConfig);
81 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule(
82 $mergedConfiguration,
83 $currentExtensionConfig
84 );
85 return $mergedConfiguration;
86 }
87
88 /**
89 * Create a flat array of configuration options from
90 * ext_conf_template.txt of an extension using core's typoscript parser.
91 *
92 * Generates an array from the typoscript style constants and
93 * adds meta data like TSConstantEditor comments
94 *
95 * Result is an array, with configuration item as array keys,
96 * and item properties as key-value sub-array:
97 *
98 * array(
99 * 'fooOption' => array(
100 * 'type' => 'string',
101 * 'value' => 'foo',
102 * ...
103 * ),
104 * 'barOption' => array(
105 * 'type' => boolean,
106 * 'default_value' => 0,
107 * ...
108 * ),
109 * ...
110 * )
111 *
112 * @param string $extensionKey Extension key
113 * @return array
114 */
115 public function getDefaultConfigurationFromExtConfTemplateAsValuedArray($extensionKey) {
116 $rawConfigurationString = $this->getDefaultConfigurationRawString($extensionKey);
117
118 $theConstants = array();
119
120 if ((string)$rawConfigurationString !== '') {
121 $extensionPathInformation = $this->getExtensionPathInformation($extensionKey);
122
123 /** @var ConfigurationForm $tsStyleConfig */
124 $tsStyleConfig = $this->objectManager->get(ConfigurationForm::class);
125 $tsStyleConfig->doNotSortCategoriesBeforeMakingForm = TRUE;
126
127 $theConstants = $tsStyleConfig->ext_initTSstyleConfig(
128 $rawConfigurationString,
129 $extensionPathInformation['siteRelPath'],
130 PATH_site . $extensionPathInformation['siteRelPath']
131 );
132
133 // Loop through configuration items, see if it is assigned to a sub category
134 // and add the sub category label to the item property if so.
135 foreach ($theConstants as $configurationOptionName => $configurationOption) {
136 if (
137 array_key_exists('subcat_name', $configurationOption)
138 && isset($tsStyleConfig->subCategories[$configurationOption['subcat_name']])
139 && isset($tsStyleConfig->subCategories[$configurationOption['subcat_name']][0])
140 ) {
141 $theConstants[$configurationOptionName]['subcat_label'] = $tsStyleConfig->subCategories[$configurationOption['subcat_name']][0];
142 }
143 }
144
145 // Set up the additional descriptions
146 if (isset($tsStyleConfig->setup['constants']['TSConstantEditor.'])) {
147 foreach ($tsStyleConfig->setup['constants']['TSConstantEditor.'] as $category => $highlights) {
148 $theConstants['__meta__'][rtrim($category, '.')]['highlightText'] = $highlights['description'];
149 foreach ($highlights as $highlightNumber => $value) {
150 if (rtrim($category, '.') == $theConstants[$value]['cat']) {
151 $theConstants[$value]['highlight'] = $highlightNumber;
152 }
153 }
154 }
155 }
156 }
157
158 return $theConstants;
159 }
160
161 /**
162 * @param string $extensionKey
163 * @return mixed
164 */
165 protected function getExtensionPathInformation($extensionKey) {
166 return $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey];
167 }
168
169 /**
170 * Return content of an extensions ext_conf_template.txt file if
171 * the file exists, empty string if file does not exist.
172 *
173 * @param string $extensionKey Extension key
174 * @return string
175 */
176 protected function getDefaultConfigurationRawString($extensionKey) {
177 $rawString = '';
178 $extConfTemplateFileLocation = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(
179 'EXT:' . $extensionKey . '/ext_conf_template.txt',
180 FALSE
181 );
182 if (file_exists($extConfTemplateFileLocation)) {
183 $rawString = file_get_contents($extConfTemplateFileLocation);
184 }
185 return $rawString;
186 }
187
188 /**
189 * Converts a valued configuration to a nested configuration.
190 *
191 * array('first.second' => array('value' => 1))
192 * will become
193 * array('first.' => array('second' => ))
194 *
195 * @param array $valuedConfiguration
196 * @return array
197 */
198 public function convertValuedToNestedConfiguration(array $valuedConfiguration) {
199 $nestedConfiguration = array();
200 foreach ($valuedConfiguration as $name => $section) {
201 $path = str_replace('.', './', $name);
202 $nestedConfiguration = \TYPO3\CMS\Core\Utility\ArrayUtility::setValueByPath($nestedConfiguration, $path, $section['value'], '/');
203 }
204 return $nestedConfiguration;
205 }
206
207 /**
208 * Convert a nested configuration to a valued configuration
209 *
210 * array('first.' => array('second' => 1))
211 * will become
212 * array('first.second' => array('value' => 1)
213 * @param array $nestedConfiguration
214 * @return array
215 */
216 public function convertNestedToValuedConfiguration(array $nestedConfiguration) {
217 $flatExtensionConfig = \TYPO3\CMS\Core\Utility\ArrayUtility::flatten($nestedConfiguration);
218 $valuedCurrentExtensionConfig = array();
219 foreach ($flatExtensionConfig as $key => $value) {
220 $valuedCurrentExtensionConfig[$key]['value'] = $value;
221 }
222 return $valuedCurrentExtensionConfig;
223 }
224
225 }