TYPO3 CMS  TYPO3_7-6
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 
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  $currentExtensionConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$extensionKey]);
83  $currentExtensionConfig = is_array($currentExtensionConfig) ? $currentExtensionConfig : [];
84  $currentExtensionConfig = $this->convertNestedToValuedConfiguration($currentExtensionConfig);
86  $mergedConfiguration,
87  $currentExtensionConfig
88  );
89  return $mergedConfiguration;
90  }
91 
119  public function getDefaultConfigurationFromExtConfTemplateAsValuedArray($extensionKey)
120  {
121  $rawConfigurationString = $this->getDefaultConfigurationRawString($extensionKey);
122 
123  $theConstants = [];
124 
125  if ((string)$rawConfigurationString !== '') {
126  $extensionPathInformation = $this->getExtensionPathInformation($extensionKey);
127 
129  $tsStyleConfig = $this->objectManager->get(ConfigurationForm::class);
130  $tsStyleConfig->doNotSortCategoriesBeforeMakingForm = true;
131 
132  $theConstants = $tsStyleConfig->ext_initTSstyleConfig(
133  $rawConfigurationString,
134  $extensionPathInformation['siteRelPath'],
135  PATH_site . $extensionPathInformation['siteRelPath']
136  );
137 
138  // Loop through configuration items, see if it is assigned to a sub category
139  // and add the sub category label to the item property if so.
140  foreach ($theConstants as $configurationOptionName => $configurationOption) {
141  if (
142  array_key_exists('subcat_name', $configurationOption)
143  && isset($tsStyleConfig->subCategories[$configurationOption['subcat_name']])
144  && isset($tsStyleConfig->subCategories[$configurationOption['subcat_name']][0])
145  ) {
146  $theConstants[$configurationOptionName]['subcat_label'] = $tsStyleConfig->subCategories[$configurationOption['subcat_name']][0];
147  }
148  }
149 
150  // Set up the additional descriptions
151  if (isset($tsStyleConfig->setup['constants']['TSConstantEditor.'])) {
152  foreach ($tsStyleConfig->setup['constants']['TSConstantEditor.'] as $category => $highlights) {
153  $theConstants['__meta__'][rtrim($category, '.')]['highlightText'] = $highlights['description'];
154  foreach ($highlights as $highlightNumber => $value) {
155  if (rtrim($category, '.') == $theConstants[$value]['cat']) {
156  $theConstants[$value]['highlight'] = $highlightNumber;
157  }
158  }
159  }
160  }
161  }
162 
163  return $theConstants;
164  }
165 
170  protected function getExtensionPathInformation($extensionKey)
171  {
172  return $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey];
173  }
174 
182  protected function getDefaultConfigurationRawString($extensionKey)
183  {
184  $rawString = '';
185  $extConfTemplateFileLocation = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(
186  'EXT:' . $extensionKey . '/ext_conf_template.txt',
187  false
188  );
189  if (file_exists($extConfTemplateFileLocation)) {
190  $rawString = file_get_contents($extConfTemplateFileLocation);
191  }
192  return $rawString;
193  }
194 
205  public function convertValuedToNestedConfiguration(array $valuedConfiguration)
206  {
207  $nestedConfiguration = [];
208  foreach ($valuedConfiguration as $name => $section) {
209  $path = str_replace('.', './', $name);
210  $nestedConfiguration = \TYPO3\CMS\Core\Utility\ArrayUtility::setValueByPath($nestedConfiguration, $path, $section['value'], '/');
211  }
212  return $nestedConfiguration;
213  }
214 
224  public function convertNestedToValuedConfiguration(array $nestedConfiguration)
225  {
226  $flatExtensionConfig = \TYPO3\CMS\Core\Utility\ArrayUtility::flatten($nestedConfiguration);
227  $valuedCurrentExtensionConfig = [];
228  foreach ($flatExtensionConfig as $key => $value) {
229  $valuedCurrentExtensionConfig[$key]['value'] = $value;
230  }
231  return $valuedCurrentExtensionConfig;
232  }
233 }
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
static setValueByPath(array $array, $path, $value, $delimiter='/')
static flatten(array $array, $prefix='')
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
static getFileAbsFileName($filename, $onlyRelative=true, $relToTYPO3_mainDir=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']