TYPO3 CMS  TYPO3_8-7
ConfigurationUtility.php
Go to the documentation of this file.
1 <?php
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 
18 
23 {
27  protected $objectManager;
28 
32  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
33  {
34  $this->objectManager = $objectManager;
35  }
36 
45  public function saveDefaultConfiguration($extensionKey)
46  {
47  $currentConfiguration = $this->getCurrentConfiguration($extensionKey);
48  $nestedConfiguration = $this->convertValuedToNestedConfiguration($currentConfiguration);
49  $this->writeConfiguration($nestedConfiguration, $extensionKey);
50  }
51 
64  public function writeConfiguration(array $configuration = [], $extensionKey)
65  {
67  $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
68  $configurationManager->setLocalConfigurationValueByPath('EXT/extConf/' . $extensionKey, serialize($configuration));
69  }
70 
77  public function getCurrentConfiguration($extensionKey)
78  {
79  $mergedConfiguration = $this->getDefaultConfigurationFromExtConfTemplateAsValuedArray($extensionKey);
80  // No objects allowed in extConf at all - it is safe to deny that during unserialize()
81  $currentExtensionConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey], ['allowed_classes' => false]);
82  $currentExtensionConfig = is_array($currentExtensionConfig) ? $currentExtensionConfig : [];
83  $currentExtensionConfig = $this->convertNestedToValuedConfiguration($currentExtensionConfig);
85  $mergedConfiguration,
86  $currentExtensionConfig
87  );
88  return $mergedConfiguration;
89  }
90 
118  public function getDefaultConfigurationFromExtConfTemplateAsValuedArray($extensionKey)
119  {
120  $rawConfigurationString = $this->getDefaultConfigurationRawString($extensionKey);
121 
122  $theConstants = [];
123 
124  if ((string)$rawConfigurationString !== '') {
126  $tsStyleConfig = $this->objectManager->get(ConfigurationForm::class);
127  $tsStyleConfig->doNotSortCategoriesBeforeMakingForm = true;
128 
129  $theConstants = $tsStyleConfig->ext_initTSstyleConfig($rawConfigurationString);
130 
131  // Loop through configuration items, see if it is assigned to a sub category
132  // and add the sub category label to the item property if so.
133  foreach ($theConstants as $configurationOptionName => $configurationOption) {
134  if (
135  array_key_exists('subcat_name', $configurationOption)
136  && isset($tsStyleConfig->subCategories[$configurationOption['subcat_name']])
137  && isset($tsStyleConfig->subCategories[$configurationOption['subcat_name']][0])
138  ) {
139  $theConstants[$configurationOptionName]['subcat_label'] = $tsStyleConfig->subCategories[$configurationOption['subcat_name']][0];
140  }
141  }
142 
143  // Set up the additional descriptions
144  if (isset($tsStyleConfig->setup['constants']['TSConstantEditor.'])) {
145  foreach ($tsStyleConfig->setup['constants']['TSConstantEditor.'] as $category => $highlights) {
146  $theConstants['__meta__'][rtrim($category, '.')]['highlightText'] = $highlights['description'];
147  foreach ($highlights as $highlightNumber => $value) {
148  if (rtrim($category, '.') == $theConstants[$value]['cat']) {
149  $theConstants[$value]['highlight'] = $highlightNumber;
150  }
151  }
152  }
153  }
154  }
155 
156  return $theConstants;
157  }
158 
166  protected function getDefaultConfigurationRawString($extensionKey)
167  {
168  $rawString = '';
169  $extConfTemplateFileLocation = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(
170  'EXT:' . $extensionKey . '/ext_conf_template.txt'
171  );
172  if (file_exists($extConfTemplateFileLocation)) {
173  $rawString = file_get_contents($extConfTemplateFileLocation);
174  }
175  return $rawString;
176  }
177 
188  public function convertValuedToNestedConfiguration(array $valuedConfiguration)
189  {
190  $nestedConfiguration = [];
191  foreach ($valuedConfiguration as $name => $section) {
192  $path = str_replace('.', './', $name);
193  $nestedConfiguration = \TYPO3\CMS\Core\Utility\ArrayUtility::setValueByPath($nestedConfiguration, $path, $section['value'], '/');
194  }
195  return $nestedConfiguration;
196  }
197 
207  public function convertNestedToValuedConfiguration(array $nestedConfiguration)
208  {
209  $flatExtensionConfig = \TYPO3\CMS\Core\Utility\ArrayUtility::flatten($nestedConfiguration);
210  $valuedCurrentExtensionConfig = [];
211  foreach ($flatExtensionConfig as $key => $value) {
212  $valuedCurrentExtensionConfig[$key]['value'] = $value;
213  }
214  return $valuedCurrentExtensionConfig;
215  }
216 }
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
static setValueByPath(array $array, $path, $value, $delimiter='/')
static getFileAbsFileName($filename, $_=null, $_2=null)
static flatten(array $array, $prefix='')
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']