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