‪TYPO3CMS  11.5
ExtensionConfiguration.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3\CMS\Core\Package\PackageManager;
26 
45 {
82  public function get(string $extension, string $path = '')
83  {
84  $hasBeenSynchronized = false;
85  if (!$this->‪hasConfiguration($extension)) {
86  // This if() should not be hit at "casual" runtime, but only in early setup phases
88  $hasBeenSynchronized = true;
89  if (!$this->‪hasConfiguration($extension)) {
90  // If there is still no such entry, even after sync -> throw
92  'No extension configuration for extension ' . $extension . ' found. Either this extension'
93  . ' has no extension configuration or the configuration is not up to date. Execute the'
94  . ' install tool to update configuration.',
95  1509654728
96  );
97  }
98  }
99  if (empty($path)) {
100  return ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extension];
101  }
102  if (!‪ArrayUtility::isValidPath(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'], $extension . '/' . $path)) {
103  // This if() should not be hit at "casual" runtime, but only in early setup phases
104  if (!$hasBeenSynchronized) {
106  }
107  // If there is still no such entry, even after sync -> throw
108  if (!‪ArrayUtility::isValidPath(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'], $extension . '/' . $path)) {
110  'Path ' . $path . ' does not exist in extension configuration',
111  1509977699
112  );
113  }
114  }
115  return ‪ArrayUtility::getValueByPath(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'], $extension . '/' . $path);
116  }
117 
148  public function set(string $extension, $value = null): void
149  {
150  if (empty($extension)) {
151  throw new \RuntimeException('extension name must not be empty', 1509715852);
152  }
153  $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
154  if ($value === null) {
155  // Remove whole extension config
156  $configurationManager->removeLocalConfigurationKeysByPath(['EXTENSIONS/' . $extension]);
157  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extension])) {
158  unset(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extension]);
159  }
160  } else {
161  // Set full extension config
162  $configurationManager->setLocalConfigurationValueByPath('EXTENSIONS/' . $extension, $value);
163  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extension] = $value;
164  }
165  }
166 
176  public function ‪setAll(array $configuration, bool $skipWriteIfLocalConfiguationDoesNotExist = false): void
177  {
178  $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
179  if ($skipWriteIfLocalConfiguationDoesNotExist === false || @file_exists($configurationManager->getLocalConfigurationFileLocation())) {
180  $configurationManager->setLocalConfigurationValueByPath('EXTENSIONS', $configuration);
181  }
182  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'] = $configuration;
183  }
184 
195  public function ‪synchronizeExtConfTemplateWithLocalConfigurationOfAllExtensions(bool $skipWriteIfLocalConfiguationDoesNotExist = false): void
196  {
197  $activePackages = GeneralUtility::makeInstance(PackageManager::class)->getActivePackages();
198  $fullConfiguration = [];
199  $currentLocalConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'] ?? [];
200  foreach ($activePackages as $package) {
201  if (!@is_file($package->getPackagePath() . 'ext_conf_template.txt')) {
202  continue;
203  }
204  $extensionKey = $package->getPackageKey();
205  $currentExtensionConfig = $currentLocalConfiguration[$extensionKey] ?? [];
206  $extConfTemplateConfiguration = $this->‪getExtConfTablesWithoutCommentsAsNestedArrayWithoutDots($extensionKey);
207  ‪ArrayUtility::mergeRecursiveWithOverrule($extConfTemplateConfiguration, $currentExtensionConfig);
208  if (!empty($extConfTemplateConfiguration)) {
209  $fullConfiguration[$extensionKey] = $extConfTemplateConfiguration;
210  }
211  }
212  // Write new config if changed. Loose array comparison to not write if only array key order is different
213  if ($fullConfiguration != $currentLocalConfiguration) {
214  $this->‪setAll($fullConfiguration, $skipWriteIfLocalConfiguationDoesNotExist);
215  }
216  }
217 
227  public function ‪synchronizeExtConfTemplateWithLocalConfiguration(string $extensionKey): void
228  {
229  $package = GeneralUtility::makeInstance(PackageManager::class)->getPackage($extensionKey);
230  if (!@is_file($package->getPackagePath() . 'ext_conf_template.txt')) {
231  return;
232  }
233  $currentLocalConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extensionKey] ?? [];
234  $extConfTemplateConfiguration = $this->‪getExtConfTablesWithoutCommentsAsNestedArrayWithoutDots($extensionKey);
235  ‪ArrayUtility::mergeRecursiveWithOverrule($extConfTemplateConfiguration, $currentLocalConfiguration);
236  // Write new config if changed. Loose array comparison to not write if only array key order is different
237  if ($extConfTemplateConfiguration != $currentLocalConfiguration) {
238  $this->set($extensionKey, $extConfTemplateConfiguration);
239  }
240  }
241 
251  protected function ‪getExtConfTablesWithoutCommentsAsNestedArrayWithoutDots(string $extensionKey): array
252  {
253  $rawConfigurationString = $this->‪getDefaultConfigurationRawString($extensionKey);
254  $typoScriptParser = GeneralUtility::makeInstance(TypoScriptParser::class);
255  // we are parsing constants, so we need the instructions from comments
256  $typoScriptParser->regComments = true;
257  $typoScriptParser->parse($rawConfigurationString);
258  // setup contains the parsed constants string
259  $parsedTemplate = $typoScriptParser->setup;
260  return $this->‪removeCommentsAndDotsRecursive($parsedTemplate);
261  }
262 
272  public function ‪getDefaultConfigurationRawString(string $extensionKey): string
273  {
274  $rawString = '';
275  $extConfTemplateFileLocation = GeneralUtility::getFileAbsFileName(
276  'EXT:' . $extensionKey . '/ext_conf_template.txt'
277  );
278  if (file_exists($extConfTemplateFileLocation)) {
279  $rawString = (string)file_get_contents($extConfTemplateFileLocation);
280  }
281  return $rawString;
282  }
283 
288  protected function ‪hasConfiguration(string $extension): bool
289  {
290  return isset(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extension]) && is_array(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extension]);
291  }
292 
318  protected function ‪removeCommentsAndDotsRecursive(array $config): array
319  {
320  $cleanedConfig = [];
321  foreach ($config as $key => $value) {
322  if (substr($key, -2) === '..') {
323  continue;
324  }
325  if (substr($key, -1) === '.') {
326  $cleanedConfig[rtrim($key, '.')] = $this->‪removeCommentsAndDotsRecursive($value);
327  } else {
328  $cleanedConfig[$key] = $value;
329  }
330  }
331  return $cleanedConfig;
332  }
333 }
‪TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException
Definition: ExtensionConfigurationPathDoesNotExistException.php:24
‪TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser
Definition: TypoScriptParser.php:36
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration
Definition: ExtensionConfiguration.php:45
‪TYPO3\CMS\Core\Utility\ArrayUtility\isValidPath
‪static bool isValidPath(array $array, $path, $delimiter='/')
Definition: ArrayUtility.php:144
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Core\Utility\ArrayUtility\getValueByPath
‪static mixed getValueByPath(array $array, $path, $delimiter='/')
Definition: ArrayUtility.php:180
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\getExtConfTablesWithoutCommentsAsNestedArrayWithoutDots
‪array getExtConfTablesWithoutCommentsAsNestedArrayWithoutDots(string $extensionKey)
Definition: ExtensionConfiguration.php:251
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\removeCommentsAndDotsRecursive
‪array removeCommentsAndDotsRecursive(array $config)
Definition: ExtensionConfiguration.php:318
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\synchronizeExtConfTemplateWithLocalConfiguration
‪synchronizeExtConfTemplateWithLocalConfiguration(string $extensionKey)
Definition: ExtensionConfiguration.php:227
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\hasConfiguration
‪bool hasConfiguration(string $extension)
Definition: ExtensionConfiguration.php:288
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Configuration
Definition: ConfigurationManager.php:16
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\setAll
‪setAll(array $configuration, bool $skipWriteIfLocalConfiguationDoesNotExist=false)
Definition: ExtensionConfiguration.php:176
‪TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException
Definition: ExtensionConfigurationExtensionNotConfiguredException.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\synchronizeExtConfTemplateWithLocalConfigurationOfAllExtensions
‪synchronizeExtConfTemplateWithLocalConfigurationOfAllExtensions(bool $skipWriteIfLocalConfiguationDoesNotExist=false)
Definition: ExtensionConfiguration.php:195
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\getDefaultConfigurationRawString
‪string getDefaultConfigurationRawString(string $extensionKey)
Definition: ExtensionConfiguration.php:272