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