c983d84b005136020c648068f4c82c3a6433c502
[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 $hierarchicConfiguration = [];
65 foreach ($configuration as $configurationOption) {
66 $originalConfiguration = $this->buildConfigurationArray($configurationOption, $extensionKey);
67 ArrayUtility::mergeRecursiveWithOverrule($originalConfiguration, $hierarchicConfiguration);
68 $hierarchicConfiguration = $originalConfiguration;
69 }
70
71 // Flip category array as it was merged the other way around
72 $hierarchicConfiguration = array_reverse($hierarchicConfiguration);
73
74 // Sort configurations of each subcategory
75 foreach ($hierarchicConfiguration as &$catConfigurationArray) {
76 foreach ($catConfigurationArray as &$subcatConfigurationArray) {
77 uasort($subcatConfigurationArray, function ($a, $b) {
78 return strnatcmp($a['subcat'], $b['subcat']);
79 });
80 }
81 unset($subcatConfigurationArray);
82 }
83 unset($tempConfiguration);
84 $resultArray = $hierarchicConfiguration;
85 }
86
87 return $resultArray;
88 }
89
90 /**
91 * Builds a configuration array from each line (option) of the config file
92 *
93 * @param string $configurationOption config file line representing one setting
94 * @param string $extensionKey Extension key
95 * @return array
96 */
97 protected function buildConfigurationArray($configurationOption, $extensionKey)
98 {
99 $hierarchicConfiguration = [];
100 if (GeneralUtility::isFirstPartOfStr($configurationOption['type'], 'user')) {
101 $configurationOption = $this->extractInformationForConfigFieldsOfTypeUser($configurationOption);
102 } elseif (GeneralUtility::isFirstPartOfStr($configurationOption['type'], 'options')) {
103 $configurationOption = $this->extractInformationForConfigFieldsOfTypeOptions($configurationOption);
104 }
105 if ($this->translate($configurationOption['label'], $extensionKey)) {
106 $configurationOption['label'] = $this->translate($configurationOption['label'], $extensionKey);
107 }
108 $configurationOption['labels'] = GeneralUtility::trimExplode(':', $configurationOption['label'], false, 2);
109 $configurationOption['subcat_name'] = $configurationOption['subcat_name'] ?: '__default';
110 $hierarchicConfiguration[$configurationOption['cat']][$configurationOption['subcat_name']][$configurationOption['name']] = $configurationOption;
111 return $hierarchicConfiguration;
112 }
113
114 /**
115 * Extracts additional information for fields of type "options"
116 * Extracts "type", "label" and values information
117 *
118 * @param array $configurationOption
119 * @return array
120 */
121 protected function extractInformationForConfigFieldsOfTypeOptions(array $configurationOption)
122 {
123 preg_match('/options\[(.*)\]/is', $configurationOption['type'], $typeMatches);
124 $optionItems = GeneralUtility::trimExplode(',', $typeMatches[1]);
125 foreach ($optionItems as $optionItem) {
126 $optionPair = GeneralUtility::trimExplode('=', $optionItem);
127 if (count($optionPair) === 2) {
128 $configurationOption['generic'][$optionPair[0]] = $optionPair[1];
129 } else {
130 $configurationOption['generic'][$optionPair[0]] = $optionPair[0];
131 }
132 }
133 $configurationOption['type'] = 'options';
134 return $configurationOption;
135 }
136
137 /**
138 * Extract additional information for fields of type "user"
139 * Extracts "type" and the function to be called
140 *
141 * @param array $configurationOption
142 * @return array
143 */
144 protected function extractInformationForConfigFieldsOfTypeUser(array $configurationOption)
145 {
146 preg_match('/user\\[(.*)\\]/is', $configurationOption['type'], $matches);
147 $configurationOption['generic'] = $matches[1];
148 $configurationOption['type'] = 'user';
149 return $configurationOption;
150 }
151
152 /**
153 * Converts a hierarchic configuration array to an
154 * hierarchic object storage structure
155 *
156 * @param array $configuration
157 * @return \SplObjectStorage
158 */
159 protected function convertHierarchicArrayToObject(array $configuration)
160 {
161 $configurationObjectStorage = new \SplObjectStorage();
162 foreach ($configuration as $category => $subcategory) {
163 /** @var $configurationCategoryObject \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationCategory */
164 $configurationCategoryObject = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationCategory::class);
165 $configurationCategoryObject->setName($category);
166 foreach ($subcategory as $subcatName => $configurationItems) {
167 /** @var $configurationSubcategoryObject \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationSubcategory */
168 $configurationSubcategoryObject = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationSubcategory::class);
169 $configurationSubcategoryObject->setName($subcatName);
170 foreach ($configurationItems as $configurationItem) {
171 // Set sub category label if configuration item contains a subcat label.
172 // The sub category label is set multiple times if there is more than one item
173 // in a sub category, but that is ok since all items of one sub category
174 // share the same label.
175 if (array_key_exists('subcat_label', $configurationItem)) {
176 $configurationSubcategoryObject->setLabel($configurationItem['subcat_label']);
177 }
178
179 /** @var $configurationObject \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem */
180 $configurationObject = $this->objectManager->get(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem::class);
181 if (isset($configurationItem['generic'])) {
182 $configurationObject->setGeneric($configurationItem['generic']);
183 }
184 if (isset($configurationItem['cat'])) {
185 $configurationObject->setCategory($configurationItem['cat']);
186 }
187 if (isset($configurationItem['subcat_name'])) {
188 $configurationObject->setSubCategory($configurationItem['subcat_name']);
189 }
190 if (isset($configurationItem['labels']) && isset($configurationItem['labels'][0])) {
191 $configurationObject->setLabelHeadline($configurationItem['labels'][0]);
192 }
193 if (isset($configurationItem['labels']) && isset($configurationItem['labels'][1])) {
194 $configurationObject->setLabelText($configurationItem['labels'][1]);
195 }
196 if (isset($configurationItem['type'])) {
197 $configurationObject->setType($configurationItem['type']);
198 }
199 if (isset($configurationItem['name'])) {
200 $configurationObject->setName($configurationItem['name']);
201 }
202 if (isset($configurationItem['value'])) {
203 $configurationObject->setValue($configurationItem['value']);
204 }
205 $configurationSubcategoryObject->addItem($configurationObject);
206 }
207 $configurationCategoryObject->addSubcategory($configurationSubcategoryObject);
208 }
209 $configurationObjectStorage->attach($configurationCategoryObject);
210 }
211 return $configurationObjectStorage;
212 }
213
214 /**
215 * Returns the localized label of the LOCAL_LANG key, $key.
216 * Wrapper for the static call.
217 *
218 * @param string $key The key from the LOCAL_LANG array for which to return the value.
219 * @param string $extensionName The name of the extension
220 * @return string|NULL The value from LOCAL_LANG or NULL if no translation was found.
221 */
222 protected function translate($key, $extensionName)
223 {
224 $translation = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, $extensionName);
225 if ($translation) {
226 return $translation;
227 }
228 return null;
229 }
230 }