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