[BUGFIX] EM 6.0 parse configuration type 'options' correctly
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Domain / Repository / ConfigurationItemRepository.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Domain\Repository;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Susanne Moog, <typo3@susannemoog.de>
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 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * A repository for extension configuration items
28 *
29 * @author Susanne Moog <typo3@susannemoog.de>
30 * @package Extension Manager
31 * @subpackage Repository
32 */
33 class ConfigurationItemRepository {
34
35 /**
36 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
37 */
38 protected $objectManager;
39
40 /**
41 * @var \TYPO3\CMS\Core\Configuration\ConfigurationManager
42 */
43 protected $configurationManager;
44
45 /**
46 * Injects the object manager
47 *
48 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
49 * @return void
50 */
51 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
52 $this->objectManager = $objectManager;
53 }
54
55 /**
56 * Inject configuration manager
57 *
58 * @param \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager
59 * @return void
60 */
61 public function injectConfigurationManager(\TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager) {
62 $this->configurationManager = $configurationManager;
63 }
64
65 /**
66 * Find configuration options by extension
67 *
68 * @param array $extension array with extension information
69 * @return null|\SplObjectStorage
70 */
71 public function findByExtension(array $extension) {
72 $configRaw = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl(PATH_site . $extension['siteRelPath'] . '/ext_conf_template.txt');
73 $configurationObjectStorage = NULL;
74 if ($configRaw) {
75 $configurationObjectStorage = $this->convertRawConfigurationToObject($configRaw, $extension);
76 }
77 return $configurationObjectStorage;
78 }
79
80 /**
81 * Converts the raw configuration file content to an configuration object storage
82 *
83 * @param string $configRaw
84 * @param array $extension array with extension information
85 * @return \SplObjectStorage
86 */
87 protected function convertRawConfigurationToObject($configRaw, array $extension) {
88 $defaultConfiguration = $this->createArrayFromConstants($configRaw, $extension);
89 $metaInformation = $this->addMetaInformation($defaultConfiguration);
90 $configuration = $this->mergeWithExistingConfiguration($defaultConfiguration, $extension);
91 $hierarchicConfiguration = array();
92 foreach ($configuration as $configurationOption) {
93 $hierarchicConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($this->buildConfigurationArray($configurationOption, $extension), $hierarchicConfiguration);
94 }
95 $configurationObjectStorage = $this->convertHierarchicArrayToObject(\TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($hierarchicConfiguration, $metaInformation));
96 return $configurationObjectStorage;
97 }
98
99 /**
100 * Builds a configuration array from each line (option) of the config file
101 *
102 * @param string $configurationOption config file line representing one setting
103 * @param array $extension
104 * @return array
105 */
106 protected function buildConfigurationArray($configurationOption, $extension) {
107 $hierarchicConfiguration = array();
108 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($configurationOption['type'], 'user')) {
109 $configurationOption = $this->extractInformationForConfigFieldsOfTypeUser($configurationOption);
110 } elseif (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($configurationOption['type'], 'options')) {
111 $configurationOption = $this->extractInformationForConfigFieldsOfTypeOptions($configurationOption);
112 }
113 if (\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($configurationOption['label'], $extension['key'])) {
114 $configurationOption['label'] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($configurationOption['label'], $extension['key']);
115 }
116 $configurationOption['labels'] = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(':', $configurationOption['label'], FALSE, 2);
117 $configurationOption['subcat_name'] = $configurationOption['subcat_name'] ? $configurationOption['subcat_name'] : '__default';
118 $hierarchicConfiguration[$configurationOption['cat']][$configurationOption['subcat_name']][$configurationOption['name']] = $configurationOption;
119 return $hierarchicConfiguration;
120 }
121
122 /**
123 * Extracts additional information for fields of type "options"
124 * Extracts "type", "label" and values information
125 *
126 * @param array $configurationOption
127 * @return array
128 */
129 protected function extractInformationForConfigFieldsOfTypeOptions(array $configurationOption) {
130 preg_match('/options\[(.*)\]/is', $configurationOption['type'], $typeMatches);
131 $optionItems = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $typeMatches[1]);
132 foreach ($optionItems as $optionItem) {
133 $optionPair = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('=', $optionItem);
134 if (count($optionPair) === 2) {
135 $configurationOption['generic'][$optionPair[0]] = $optionPair[1];
136 } else {
137 $configurationOption['generic'][$optionPair[0]] = $optionPair[0];
138 }
139 }
140 $configurationOption['type'] = 'options';
141 return $configurationOption;
142 }
143
144 /**
145 * Extract additional information for fields of type "user"
146 * Extracts "type" and the function to be called
147 *
148 * @param array $configurationOption
149 * @return array
150 */
151 protected function extractInformationForConfigFieldsOfTypeUser(array $configurationOption) {
152 preg_match('/user\\[(.*)\\]/is', $configurationOption['type'], $matches);
153 $configurationOption['generic'] = $matches[1];
154 $configurationOption['type'] = 'user';
155 return $configurationOption;
156 }
157
158 /**
159 * Gets meta information from configuration array and
160 * returns only the meta information
161 *
162 * @param array $configuration
163 * @return array
164 */
165 protected function addMetaInformation(&$configuration) {
166 $metaInformation = $configuration['__meta__'] ? $configuration['__meta__'] : array();
167 unset($configuration['__meta__']);
168 return $metaInformation;
169 }
170
171 /**
172 * Generate an array from the typoscript style constants
173 * Add meta data like TSConstantEditor comments
174 *
175 * @param string $configRaw
176 * @param array $extension
177 * @return array
178 */
179 public function createArrayFromConstants($configRaw, array $extension) {
180 $tsStyleConfig = $this->getT3libTsStyleConfig();
181 $tsStyleConfig->doNotSortCategoriesBeforeMakingForm = TRUE;
182 $theConstants = $tsStyleConfig->ext_initTSstyleConfig($configRaw, $extension['siteRelPath'], PATH_site . $extension['siteRelPath'], $GLOBALS['BACK_PATH']);
183 if (isset($tsStyleConfig->setup['constants']['TSConstantEditor.'])) {
184 foreach ($tsStyleConfig->setup['constants']['TSConstantEditor.'] as $category => $highlights) {
185 $theConstants['__meta__'][rtrim($category, '.')]['highlightText'] = $highlights['description'];
186 foreach ($highlights as $highlightNumber => $value) {
187 if (rtrim($category, '.') == $theConstants[$value]['cat']) {
188 $theConstants[$value]['highlight'] = $highlightNumber;
189 }
190 }
191 }
192 }
193 return $theConstants;
194 }
195
196 /**
197 * Wrapper for makeInstance to make it possible to mock
198 * the class
199 *
200 * @return \TYPO3\CMS\Core\TypoScript\ConfigurationForm
201 */
202 protected function getT3libTsStyleConfig() {
203 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\ConfigurationForm');
204 }
205
206 /**
207 * Merge new configuration with existing configuration
208 *
209 * @param array $configuration the new configuration array
210 * @param array $extension the extension information
211 * @return array
212 */
213 protected function mergeWithExistingConfiguration(array $configuration, array $extension) {
214 try {
215 $currentExtensionConfig = unserialize(
216 $this->configurationManager->getConfigurationValueByPath(
217 'EXT/extConf/' . $extension['key']
218 )
219 );
220 } catch (\RuntimeException $e) {
221 $currentExtensionConfig = array();
222 }
223 $flatExtensionConfig = \TYPO3\CMS\Core\Utility\ArrayUtility::flatten($currentExtensionConfig);
224 $valuedCurrentExtensionConfig = array();
225 foreach ($flatExtensionConfig as $key => $value) {
226 $valuedCurrentExtensionConfig[$key]['value'] = $value;
227 }
228 $configuration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($configuration, $valuedCurrentExtensionConfig);
229 return $configuration;
230 }
231
232 /**
233 * Converts a hierarchic configuration array to an
234 * hierarchic object storage structure
235 *
236 * @param array $configuration
237 * @return \SplObjectStorage
238 */
239 protected function convertHierarchicArrayToObject(array $configuration) {
240 $configurationObjectStorage = new \SplObjectStorage();
241 foreach ($configuration as $category => $subcategory) {
242 /** @var $configurationCategoryObject \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationCategory */
243 $configurationCategoryObject = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\ConfigurationCategory');
244 $configurationCategoryObject->setName($category);
245 if ($subcategory['highlightText']) {
246 $configurationCategoryObject->setHighlightText($subcategory['highlightText']);
247 unset($subcategory['highlightText']);
248 }
249 foreach ($subcategory as $subcatName => $configurationItems) {
250 /** @var $configurationSubcategoryObject \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationSubcategory */
251 $configurationSubcategoryObject = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\ConfigurationSubcategory');
252 $configurationSubcategoryObject->setName($subcatName);
253 foreach ($configurationItems as $configurationItem) {
254 /** @var $configurationObject \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem */
255 $configurationObject = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\ConfigurationItem');
256 if (isset($configurationItem['generic'])) {
257 $configurationObject->setGeneric($configurationItem['generic']);
258 }
259 if (isset($configurationItem['cat'])) {
260 $configurationObject->setCategory($configurationItem['cat']);
261 }
262 if (isset($configurationItem['subcat_name'])) {
263 $configurationObject->setSubCategory($configurationItem['subcat_name']);
264 }
265 if (isset($configurationItem['labels']) && isset($configurationItem['labels'][0])) {
266 $configurationObject->setLabelHeadline($configurationItem['labels'][0]);
267 }
268 if (isset($configurationItem['labels']) && isset($configurationItem['labels'][1])) {
269 $configurationObject->setLabelText($configurationItem['labels'][1]);
270 }
271 if (isset($configurationItem['type'])) {
272 $configurationObject->setType($configurationItem['type']);
273 }
274 if (isset($configurationItem['name'])) {
275 $configurationObject->setName($configurationItem['name']);
276 }
277 if (isset($configurationItem['value'])) {
278 $configurationObject->setValue($configurationItem['value']);
279 }
280 if (isset($configurationItem['highlight'])) {
281 $configurationObject->setHighlight($configurationItem['highlight']);
282 }
283 $configurationSubcategoryObject->addItem($configurationObject);
284 }
285 $configurationCategoryObject->addSubcategory($configurationSubcategoryObject);
286 }
287 $configurationObjectStorage->attach($configurationCategoryObject);
288 }
289 return $configurationObjectStorage;
290 }
291
292 }
293
294
295 ?>