TYPO3 CMS  TYPO3_6-2
ConfigurationItemRepository.php
Go to the documentation of this file.
1 <?php
3 
19 
26 
31  protected $objectManager;
32 
39  public function findByExtensionKey($extensionKey) {
40  $configurationArray = $this->getConfigurationArrayFromExtensionKey($extensionKey);
41  return $this->convertHierarchicArrayToObject($configurationArray);
42  }
43 
50  protected function getConfigurationArrayFromExtensionKey($extensionKey) {
52  $configurationUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
53  $defaultConfiguration = $configurationUtility->getDefaultConfigurationFromExtConfTemplateAsValuedArray($extensionKey);
54 
55  $resultArray = array();
56  if (count($defaultConfiguration) > 0) {
57  $metaInformation = $this->addMetaInformation($defaultConfiguration);
58  $configuration = $this->mergeWithExistingConfiguration($defaultConfiguration, $extensionKey);
59  $hierarchicConfiguration = array();
60  foreach ($configuration as $configurationOption) {
61  $originalConfiguration = $this->buildConfigurationArray($configurationOption, $extensionKey);
62  ArrayUtility::mergeRecursiveWithOverrule($originalConfiguration, $hierarchicConfiguration);
63  $hierarchicConfiguration = $originalConfiguration;
64  }
65 
66  // Flip category array as it was merged the other way around
67  $hierarchicConfiguration = array_reverse($hierarchicConfiguration);
68 
69  // Sort configurations of each subcategory
70  foreach ($hierarchicConfiguration as &$catConfigurationArray) {
71  foreach ($catConfigurationArray as &$subcatConfigurationArray) {
72  uasort($subcatConfigurationArray, function ($a, $b) {
73  return strnatcmp($a['subcat'], $b['subcat']);
74  });
75  }
76  unset($subcatConfigurationArray);
77  }
78  unset($tempConfiguration);
79 
80  ArrayUtility::mergeRecursiveWithOverrule($hierarchicConfiguration, $metaInformation);
81  $resultArray = $hierarchicConfiguration;
82  }
83 
84  return $resultArray;
85  }
86 
94  protected function buildConfigurationArray($configurationOption, $extensionKey) {
95  $hierarchicConfiguration = array();
96  if (GeneralUtility::isFirstPartOfStr($configurationOption['type'], 'user')) {
97  $configurationOption = $this->extractInformationForConfigFieldsOfTypeUser($configurationOption);
98  } elseif (GeneralUtility::isFirstPartOfStr($configurationOption['type'], 'options')) {
99  $configurationOption = $this->extractInformationForConfigFieldsOfTypeOptions($configurationOption);
100  }
101  if ($this->translate($configurationOption['label'], $extensionKey)) {
102  $configurationOption['label'] = $this->translate($configurationOption['label'], $extensionKey);
103  }
104  $configurationOption['labels'] = GeneralUtility::trimExplode(':', $configurationOption['label'], FALSE, 2);
105  $configurationOption['subcat_name'] = $configurationOption['subcat_name'] ?: '__default';
106  $hierarchicConfiguration[$configurationOption['cat']][$configurationOption['subcat_name']][$configurationOption['name']] = $configurationOption;
107  return $hierarchicConfiguration;
108  }
109 
117  protected function extractInformationForConfigFieldsOfTypeOptions(array $configurationOption) {
118  preg_match('/options\[(.*)\]/is', $configurationOption['type'], $typeMatches);
119  $optionItems = GeneralUtility::trimExplode(',', $typeMatches[1]);
120  foreach ($optionItems as $optionItem) {
121  $optionPair = GeneralUtility::trimExplode('=', $optionItem);
122  if (count($optionPair) === 2) {
123  $configurationOption['generic'][$optionPair[0]] = $optionPair[1];
124  } else {
125  $configurationOption['generic'][$optionPair[0]] = $optionPair[0];
126  }
127  }
128  $configurationOption['type'] = 'options';
129  return $configurationOption;
130  }
131 
139  protected function extractInformationForConfigFieldsOfTypeUser(array $configurationOption) {
140  preg_match('/user\\[(.*)\\]/is', $configurationOption['type'], $matches);
141  $configurationOption['generic'] = $matches[1];
142  $configurationOption['type'] = 'user';
143  return $configurationOption;
144  }
145 
153  protected function addMetaInformation(&$configuration) {
154  $metaInformation = $configuration['__meta__'] ?: array();
155  unset($configuration['__meta__']);
156  return $metaInformation;
157  }
158 
166  protected function mergeWithExistingConfiguration(array $defaultConfiguration, $extensionKey) {
167  try {
168  $currentExtensionConfig = unserialize(
169  $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager')
170  ->getConfigurationValueByPath('EXT/extConf/' . $extensionKey)
171  );
172  if (!is_array($currentExtensionConfig)) {
173  $currentExtensionConfig = array();
174  }
175  } catch (\RuntimeException $e) {
176  $currentExtensionConfig = array();
177  }
178  $flatExtensionConfig = ArrayUtility::flatten($currentExtensionConfig);
179  $valuedCurrentExtensionConfig = array();
180  foreach ($flatExtensionConfig as $key => $value) {
181  $valuedCurrentExtensionConfig[$key]['value'] = $value;
182  }
183  ArrayUtility::mergeRecursiveWithOverrule($defaultConfiguration, $valuedCurrentExtensionConfig);
184  return $defaultConfiguration;
185  }
186 
194  protected function convertHierarchicArrayToObject(array $configuration) {
195  $configurationObjectStorage = new \SplObjectStorage();
196  foreach ($configuration as $category => $subcategory) {
198  $configurationCategoryObject = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\ConfigurationCategory');
199  $configurationCategoryObject->setName($category);
200  if ($subcategory['highlightText']) {
201  $configurationCategoryObject->setHighlightText($subcategory['highlightText']);
202  unset($subcategory['highlightText']);
203  }
204  foreach ($subcategory as $subcatName => $configurationItems) {
206  $configurationSubcategoryObject = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\ConfigurationSubcategory');
207  $configurationSubcategoryObject->setName($subcatName);
208  foreach ($configurationItems as $configurationItem) {
209  // Set sub category label if configuration item contains a subcat label.
210  // The sub category label is set multiple times if there is more than one item
211  // in a sub category, but that is ok since all items of one sub category
212  // share the same label.
213  if (array_key_exists('subcat_label', $configurationItem)) {
214  $configurationSubcategoryObject->setLabel($configurationItem['subcat_label']);
215  }
216 
218  $configurationObject = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\ConfigurationItem');
219  if (isset($configurationItem['generic'])) {
220  $configurationObject->setGeneric($configurationItem['generic']);
221  }
222  if (isset($configurationItem['cat'])) {
223  $configurationObject->setCategory($configurationItem['cat']);
224  }
225  if (isset($configurationItem['subcat_name'])) {
226  $configurationObject->setSubCategory($configurationItem['subcat_name']);
227  }
228  if (isset($configurationItem['labels']) && isset($configurationItem['labels'][0])) {
229  $configurationObject->setLabelHeadline($configurationItem['labels'][0]);
230  }
231  if (isset($configurationItem['labels']) && isset($configurationItem['labels'][1])) {
232  $configurationObject->setLabelText($configurationItem['labels'][1]);
233  }
234  if (isset($configurationItem['type'])) {
235  $configurationObject->setType($configurationItem['type']);
236  }
237  if (isset($configurationItem['name'])) {
238  $configurationObject->setName($configurationItem['name']);
239  }
240  if (isset($configurationItem['value'])) {
241  $configurationObject->setValue($configurationItem['value']);
242  }
243  if (isset($configurationItem['highlight'])) {
244  $configurationObject->setHighlight($configurationItem['highlight']);
245  }
246  $configurationSubcategoryObject->addItem($configurationObject);
247  }
248  $configurationCategoryObject->addSubcategory($configurationSubcategoryObject);
249  }
250  $configurationObjectStorage->attach($configurationCategoryObject);
251  }
252  return $configurationObjectStorage;
253  }
254 
263  protected function translate($key, $extensionName) {
264  $translation = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, $extensionName);
265  if ($translation) {
266  return $translation;
267  }
268  return NULL;
269  }
270 
271 }
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=TRUE, $includeEmptyValues=TRUE, $enableUnsetFeature=TRUE)
static isFirstPartOfStr($str, $partStr)
static flatten(array $array, $prefix='')
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
static translate($key, $extensionName, $arguments=NULL)