e1e28349f851099e783355296b403fee8fbbb8e8
[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 preg_match('/options\\[(.*)\\]/is', $configurationOption['label'], $labelMatches);
132 $optionValues = explode(',', $typeMatches[1]);
133 $optionLabels = explode(',', $labelMatches[1]);
134 $configurationOption['generic'] = $labelMatches ? array_combine($optionLabels, $optionValues) : array_combine($optionValues, $optionValues);
135 $configurationOption['type'] = 'options';
136 $configurationOption['label'] = str_replace($labelMatches[0], '', $configurationOption['label']);
137 return $configurationOption;
138 }
139
140 /**
141 * Extract additional information for fields of type "user"
142 * Extracts "type" and the function to be called
143 *
144 * @param array $configurationOption
145 * @return array
146 */
147 protected function extractInformationForConfigFieldsOfTypeUser(array $configurationOption) {
148 preg_match('/user\\[(.*)\\]/is', $configurationOption['type'], $matches);
149 $configurationOption['generic'] = $matches[1];
150 $configurationOption['type'] = 'user';
151 return $configurationOption;
152 }
153
154 /**
155 * Gets meta information from configuration array and
156 * returns only the meta information
157 *
158 * @param array $configuration
159 * @return array
160 */
161 protected function addMetaInformation(&$configuration) {
162 $metaInformation = $configuration['__meta__'] ? $configuration['__meta__'] : array();
163 unset($configuration['__meta__']);
164 return $metaInformation;
165 }
166
167 /**
168 * Generate an array from the typoscript style constants
169 * Add meta data like TSConstantEditor comments
170 *
171 * @param string $configRaw
172 * @param array $extension
173 * @return array
174 */
175 public function createArrayFromConstants($configRaw, array $extension) {
176 $tsStyleConfig = $this->getT3libTsStyleConfig();
177 $tsStyleConfig->doNotSortCategoriesBeforeMakingForm = TRUE;
178 $theConstants = $tsStyleConfig->ext_initTSstyleConfig($configRaw, $extension['siteRelPath'], PATH_site . $extension['siteRelPath'], $GLOBALS['BACK_PATH']);
179 if (isset($tsStyleConfig->setup['constants']['TSConstantEditor.'])) {
180 foreach ($tsStyleConfig->setup['constants']['TSConstantEditor.'] as $category => $highlights) {
181 $theConstants['__meta__'][rtrim($category, '.')]['highlightText'] = $highlights['description'];
182 foreach ($highlights as $highlightNumber => $value) {
183 if (rtrim($category, '.') == $theConstants[$value]['cat']) {
184 $theConstants[$value]['highlight'] = $highlightNumber;
185 }
186 }
187 }
188 }
189 return $theConstants;
190 }
191
192 /**
193 * Wrapper for makeInstance to make it possible to mock
194 * the class
195 *
196 * @return \TYPO3\CMS\Core\TypoScript\ConfigurationForm
197 */
198 protected function getT3libTsStyleConfig() {
199 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\ConfigurationForm');
200 }
201
202 /**
203 * Merge new configuration with existing configuration
204 *
205 * @param array $configuration the new configuration array
206 * @param array $extension the extension information
207 * @return array
208 */
209 protected function mergeWithExistingConfiguration(array $configuration, array $extension) {
210 try {
211 $currentExtensionConfig = unserialize(
212 $this->configurationManager->getConfigurationValueByPath(
213 'EXT/extConf/' . $extension['key']
214 )
215 );
216 } catch (\RuntimeException $e) {
217 $currentExtensionConfig = array();
218 }
219 $flatExtensionConfig = \TYPO3\CMS\Core\Utility\ArrayUtility::flatten($currentExtensionConfig);
220 $valuedCurrentExtensionConfig = array();
221 foreach ($flatExtensionConfig as $key => $value) {
222 $valuedCurrentExtensionConfig[$key]['value'] = $value;
223 }
224 $configuration = \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule($configuration, $valuedCurrentExtensionConfig);
225 return $configuration;
226 }
227
228 /**
229 * Converts a hierarchic configuration array to an
230 * hierarchic object storage structure
231 *
232 * @param array $configuration
233 * @return \SplObjectStorage
234 */
235 protected function convertHierarchicArrayToObject(array $configuration) {
236 $configurationObjectStorage = new \SplObjectStorage();
237 foreach ($configuration as $category => $subcategory) {
238 /** @var $configurationCategoryObject \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationCategory */
239 $configurationCategoryObject = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\ConfigurationCategory');
240 $configurationCategoryObject->setName($category);
241 if ($subcategory['highlightText']) {
242 $configurationCategoryObject->setHighlightText($subcategory['highlightText']);
243 unset($subcategory['highlightText']);
244 }
245 foreach ($subcategory as $subcatName => $configurationItems) {
246 /** @var $configurationSubcategoryObject \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationSubcategory */
247 $configurationSubcategoryObject = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\ConfigurationSubcategory');
248 $configurationSubcategoryObject->setName($subcatName);
249 foreach ($configurationItems as $configurationItem) {
250 /** @var $configurationObject \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem */
251 $configurationObject = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\ConfigurationItem');
252 if (isset($configurationItem['generic'])) {
253 $configurationObject->setGeneric($configurationItem['generic']);
254 }
255 if (isset($configurationItem['cat'])) {
256 $configurationObject->setCategory($configurationItem['cat']);
257 }
258 if (isset($configurationItem['subcat_name'])) {
259 $configurationObject->setSubCategory($configurationItem['subcat_name']);
260 }
261 if (isset($configurationItem['labels']) && isset($configurationItem['labels'][0])) {
262 $configurationObject->setLabelHeadline($configurationItem['labels'][0]);
263 }
264 if (isset($configurationItem['labels']) && isset($configurationItem['labels'][1])) {
265 $configurationObject->setLabelText($configurationItem['labels'][1]);
266 }
267 if (isset($configurationItem['type'])) {
268 $configurationObject->setType($configurationItem['type']);
269 }
270 if (isset($configurationItem['name'])) {
271 $configurationObject->setName($configurationItem['name']);
272 }
273 if (isset($configurationItem['value'])) {
274 $configurationObject->setValue($configurationItem['value']);
275 }
276 if (isset($configurationItem['highlight'])) {
277 $configurationObject->setHighlight($configurationItem['highlight']);
278 }
279 $configurationSubcategoryObject->addItem($configurationObject);
280 }
281 $configurationCategoryObject->addSubcategory($configurationSubcategoryObject);
282 }
283 $configurationObjectStorage->attach($configurationCategoryObject);
284 }
285 return $configurationObjectStorage;
286 }
287
288 }
289
290
291 ?>