[FEATURE] Store extension configuration as plain array
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Domain / Repository / ConfigurationItemRepository.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Domain\Repository;
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\Utility\ArrayUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * A repository for extension configuration items
22 */
23 class ConfigurationItemRepository
24 {
25 /**
26 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
27 */
28 protected $objectManager;
29
30 /**
31 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
32 */
33 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
34 {
35 $this->objectManager = $objectManager;
36 }
37
38 /**
39 * Find configuration options by extension
40 *
41 * @param string $extensionKey Extension key
42 * @return \SplObjectStorage
43 */
44 public function findByExtensionKey($extensionKey)
45 {
46 $configurationArray = $this->getConfigurationArrayFromExtensionKey($extensionKey);
47 return $this->convertHierarchicArrayToObject($configurationArray);
48 }
49
50 /**
51 * Converts the raw configuration file content to an configuration object storage
52 *
53 * @param string $extensionKey Extension key
54 * @return array
55 */
56 protected function getConfigurationArrayFromExtensionKey($extensionKey)
57 {
58 /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
59 $configurationUtility = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility::class);
60 $configuration = $configurationUtility->getCurrentConfiguration($extensionKey);
61
62 $resultArray = [];
63 if (!empty($configuration)) {
64 $metaInformation = $this->addMetaInformation($configuration);
65 $hierarchicConfiguration = [];
66 foreach ($configuration as $configurationOption) {
67 $originalConfiguration = $this->buildConfigurationArray($configurationOption, $extensionKey);
68 ArrayUtility::mergeRecursiveWithOverrule($originalConfiguration, $hierarchicConfiguration);
69 $hierarchicConfiguration = $originalConfiguration;
70 }
71
72 // Flip category array as it was merged the other way around
73 $hierarchicConfiguration = array_reverse($hierarchicConfiguration);
74
75 // Sort configurations of each subcategory
76 foreach ($hierarchicConfiguration as &$catConfigurationArray) {
77 foreach ($catConfigurationArray as &$subcatConfigurationArray) {
78 uasort($subcatConfigurationArray, function ($a, $b) {
79 return strnatcmp($a['subcat'], $b['subcat']);
80 });
81 }
82 unset($subcatConfigurationArray);
83 }
84 unset($tempConfiguration);
85
86 ArrayUtility::mergeRecursiveWithOverrule($hierarchicConfiguration, $metaInformation);
87 $resultArray = $hierarchicConfiguration;
88 }
89
90 return $resultArray;
91 }
92
93 /**
94 * Builds a configuration array from each line (option) of the config file
95 *
96 * @param string $configurationOption config file line representing one setting
97 * @param string $extensionKey Extension key
98 * @return array
99 */
100 protected function buildConfigurationArray($configurationOption, $extensionKey)
101 {
102 $hierarchicConfiguration = [];
103 if (GeneralUtility::isFirstPartOfStr($configurationOption['type'], 'user')) {
104 $configurationOption = $this->extractInformationForConfigFieldsOfTypeUser($configurationOption);
105 } elseif (GeneralUtility::isFirstPartOfStr($configurationOption['type'], 'options')) {
106 $configurationOption = $this->extractInformationForConfigFieldsOfTypeOptions($configurationOption);
107 }
108 if ($this->translate($configurationOption['label'], $extensionKey)) {
109 $configurationOption['label'] = $this->translate($configurationOption['label'], $extensionKey);
110 }
111 $configurationOption['labels'] = GeneralUtility::trimExplode(':', $configurationOption['label'], false, 2);
112 $configurationOption['subcat_name'] = $configurationOption['subcat_name'] ?: '__default';
113 $hierarchicConfiguration[$configurationOption['cat']][$configurationOption['subcat_name']][$configurationOption['name']] = $configurationOption;
114 return $hierarchicConfiguration;
115 }
116
117 /**
118 * Extracts additional information for fields of type "options"
119 * Extracts "type", "label" and values information
120 *
121 * @param array $configurationOption
122 * @return array
123 */
124 protected function extractInformationForConfigFieldsOfTypeOptions(array $configurationOption)
125 {
126 preg_match('/options\[(.*)\]/is', $configurationOption['type'], $typeMatches);
127 $optionItems = GeneralUtility::trimExplode(',', $typeMatches[1]);
128 foreach ($optionItems as $optionItem) {
129 $optionPair = GeneralUtility::trimExplode('=', $optionItem);
130 if (count($optionPair) === 2) {
131 $configurationOption['generic'][$optionPair[0]] = $optionPair[1];
132 } else {
133 $configurationOption['generic'][$optionPair[0]] = $optionPair[0];
134 }
135 }
136 $configurationOption['type'] = 'options';
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 {
149 preg_match('/user\\[(.*)\\]/is', $configurationOption['type'], $matches);
150 $configurationOption['generic'] = $matches[1];
151 $configurationOption['type'] = 'user';
152 return $configurationOption;
153 }
154
155 /**
156 * Gets meta information from configuration array and
157 * returns only the meta information
158 *
159 * @param array $configuration
160 * @return array
161 */
162 protected function addMetaInformation(&$configuration)
163 {
164 $metaInformation = $configuration['__meta__'] ?: [];
165 unset($configuration['__meta__']);
166 return $metaInformation;
167 }
168
169 /**
170 * Converts a hierarchic configuration array to an
171 * hierarchic object storage structure
172 *
173 * @param array $configuration
174 * @return \SplObjectStorage
175 */
176 protected function convertHierarchicArrayToObject(array $configuration)
177 {
178 $configurationObjectStorage = new \SplObjectStorage();
179 foreach ($configuration as $category => $subcategory) {
180 /** @var $configurationCategoryObject \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationCategory */
181 $configurationCategoryObject = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationCategory::class);
182 $configurationCategoryObject->setName($category);
183 if ($subcategory['highlightText']) {
184 $configurationCategoryObject->setHighlightText($subcategory['highlightText']);
185 unset($subcategory['highlightText']);
186 }
187 foreach ($subcategory as $subcatName => $configurationItems) {
188 /** @var $configurationSubcategoryObject \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationSubcategory */
189 $configurationSubcategoryObject = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationSubcategory::class);
190 $configurationSubcategoryObject->setName($subcatName);
191 foreach ($configurationItems as $configurationItem) {
192 // Set sub category label if configuration item contains a subcat label.
193 // The sub category label is set multiple times if there is more than one item
194 // in a sub category, but that is ok since all items of one sub category
195 // share the same label.
196 if (array_key_exists('subcat_label', $configurationItem)) {
197 $configurationSubcategoryObject->setLabel($configurationItem['subcat_label']);
198 }
199
200 /** @var $configurationObject \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem */
201 $configurationObject = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem::class);
202 if (isset($configurationItem['generic'])) {
203 $configurationObject->setGeneric($configurationItem['generic']);
204 }
205 if (isset($configurationItem['cat'])) {
206 $configurationObject->setCategory($configurationItem['cat']);
207 }
208 if (isset($configurationItem['subcat_name'])) {
209 $configurationObject->setSubCategory($configurationItem['subcat_name']);
210 }
211 if (isset($configurationItem['labels']) && isset($configurationItem['labels'][0])) {
212 $configurationObject->setLabelHeadline($configurationItem['labels'][0]);
213 }
214 if (isset($configurationItem['labels']) && isset($configurationItem['labels'][1])) {
215 $configurationObject->setLabelText($configurationItem['labels'][1]);
216 }
217 if (isset($configurationItem['type'])) {
218 $configurationObject->setType($configurationItem['type']);
219 }
220 if (isset($configurationItem['name'])) {
221 $configurationObject->setName($configurationItem['name']);
222 }
223 if (isset($configurationItem['value'])) {
224 $configurationObject->setValue($configurationItem['value']);
225 }
226 if (isset($configurationItem['highlight'])) {
227 $configurationObject->setHighlight($configurationItem['highlight']);
228 }
229 $configurationSubcategoryObject->addItem($configurationObject);
230 }
231 $configurationCategoryObject->addSubcategory($configurationSubcategoryObject);
232 }
233 $configurationObjectStorage->attach($configurationCategoryObject);
234 }
235 return $configurationObjectStorage;
236 }
237
238 /**
239 * Returns the localized label of the LOCAL_LANG key, $key.
240 * Wrapper for the static call.
241 *
242 * @param string $key The key from the LOCAL_LANG array for which to return the value.
243 * @param string $extensionName The name of the extension
244 * @return string|NULL The value from LOCAL_LANG or NULL if no translation was found.
245 */
246 protected function translate($key, $extensionName)
247 {
248 $translation = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, $extensionName);
249 if ($translation) {
250 return $translation;
251 }
252 return null;
253 }
254 }