TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
ConfigurationUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility;
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 
46  public function saveDefaultConfiguration($extensionKey)
47  {
48  $currentConfiguration = $this->getCurrentConfiguration($extensionKey);
49  $nestedConfiguration = $this->convertValuedToNestedConfiguration($currentConfiguration);
50  $this->writeConfiguration($nestedConfiguration, $extensionKey);
51  }
52 
66  public function writeConfiguration(array $configuration = [], $extensionKey)
67  {
69  $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
70  $configurationManager->setLocalConfigurationValueByPath('EXT/extConf/' . $extensionKey, serialize($configuration));
71  }
72 
79  public function getCurrentConfiguration($extensionKey)
80  {
81  $mergedConfiguration = $this->getDefaultConfigurationFromExtConfTemplateAsValuedArray($extensionKey);
82  // No objects allowed in extConf at all - it is safe to deny that during unserialize()
83  $currentExtensionConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey], ['allowed_classes' => false]);
84  $currentExtensionConfig = is_array($currentExtensionConfig) ? $currentExtensionConfig : [];
85  $currentExtensionConfig = $this->convertNestedToValuedConfiguration($currentExtensionConfig);
87  $mergedConfiguration,
88  $currentExtensionConfig
89  );
90  return $mergedConfiguration;
91  }
92 
120  public function getDefaultConfigurationFromExtConfTemplateAsValuedArray($extensionKey)
121  {
122  $rawConfigurationString = $this->getDefaultConfigurationRawString($extensionKey);
123 
124  $theConstants = [];
125 
126  if ((string)$rawConfigurationString !== '') {
128  $tsStyleConfig = $this->objectManager->get(ConfigurationForm::class);
129  $tsStyleConfig->doNotSortCategoriesBeforeMakingForm = true;
130 
131  $theConstants = $tsStyleConfig->ext_initTSstyleConfig($rawConfigurationString);
132 
133  // Loop through configuration items, see if it is assigned to a sub category
134  // and add the sub category label to the item property if so.
135  foreach ($theConstants as $configurationOptionName => $configurationOption) {
136  if (
137  array_key_exists('subcat_name', $configurationOption)
138  && isset($tsStyleConfig->subCategories[$configurationOption['subcat_name']])
139  && isset($tsStyleConfig->subCategories[$configurationOption['subcat_name']][0])
140  ) {
141  $theConstants[$configurationOptionName]['subcat_label'] = $tsStyleConfig->subCategories[$configurationOption['subcat_name']][0];
142  }
143  }
144 
145  // Set up the additional descriptions
146  if (isset($tsStyleConfig->setup['constants']['TSConstantEditor.'])) {
147  foreach ($tsStyleConfig->setup['constants']['TSConstantEditor.'] as $category => $highlights) {
148  $theConstants['__meta__'][rtrim($category, '.')]['highlightText'] = $highlights['description'];
149  foreach ($highlights as $highlightNumber => $value) {
150  if (rtrim($category, '.') == $theConstants[$value]['cat']) {
151  $theConstants[$value]['highlight'] = $highlightNumber;
152  }
153  }
154  }
155  }
156  }
157 
158  return $theConstants;
159  }
160 
168  protected function getDefaultConfigurationRawString($extensionKey)
169  {
170  $rawString = '';
171  $extConfTemplateFileLocation = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(
172  'EXT:' . $extensionKey . '/ext_conf_template.txt'
173  );
174  if (file_exists($extConfTemplateFileLocation)) {
175  $rawString = file_get_contents($extConfTemplateFileLocation);
176  }
177  return $rawString;
178  }
179 
190  public function convertValuedToNestedConfiguration(array $valuedConfiguration)
191  {
192  $nestedConfiguration = [];
193  foreach ($valuedConfiguration as $name => $section) {
194  $path = str_replace('.', './', $name);
195  $nestedConfiguration = \TYPO3\CMS\Core\Utility\ArrayUtility::setValueByPath($nestedConfiguration, $path, $section['value'], '/');
196  }
197  return $nestedConfiguration;
198  }
199 
209  public function convertNestedToValuedConfiguration(array $nestedConfiguration)
210  {
211  $flatExtensionConfig = \TYPO3\CMS\Core\Utility\ArrayUtility::flatten($nestedConfiguration);
212  $valuedCurrentExtensionConfig = [];
213  foreach ($flatExtensionConfig as $key => $value) {
214  $valuedCurrentExtensionConfig[$key]['value'] = $value;
215  }
216  return $valuedCurrentExtensionConfig;
217  }
218 }
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
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']
static getFileAbsFileName($filename, $_=null, $_2=null)
static setValueByPath(array $array, $path, $value, $delimiter= '/')