‪TYPO3CMS  ‪main
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 
23 use TYPO3\CMS\Core\Package\PackageManager;
28 
47 {
84  public function get(string $extension, string $path = ''): mixed
85  {
86  $hasBeenSynchronized = false;
87  if (!$this->‪hasConfiguration($extension)) {
88  // This if() should not be hit at "casual" runtime, but only in early setup phases
90  $hasBeenSynchronized = true;
91  if (!$this->‪hasConfiguration($extension)) {
92  // If there is still no such entry, even after sync -> throw
94  'No extension configuration for extension ' . $extension . ' found. Either this extension'
95  . ' has no extension configuration or the configuration is not up to date. Execute the'
96  . ' install tool to update configuration.',
97  1509654728
98  );
99  }
100  }
101  if (empty($path)) {
102  return ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extension];
103  }
104  if (!‪ArrayUtility::isValidPath(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'], $extension . '/' . $path)) {
105  // This if() should not be hit at "casual" runtime, but only in early setup phases
106  if (!$hasBeenSynchronized) {
108  }
109  // If there is still no such entry, even after sync -> throw
110  if (!‪ArrayUtility::isValidPath(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'], $extension . '/' . $path)) {
112  'Path ' . $path . ' does not exist in extension configuration',
113  1509977699
114  );
115  }
116  }
117  return ‪ArrayUtility::getValueByPath(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'], $extension . '/' . $path);
118  }
119 
150  public function set(string $extension, mixed $value = null): void
151  {
152  if (empty($extension)) {
153  throw new \RuntimeException('extension name must not be empty', 1509715852);
154  }
155  $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
156  if ($value === null) {
157  // Remove whole extension config
158  $configurationManager->removeLocalConfigurationKeysByPath(['EXTENSIONS/' . $extension]);
159  if (isset(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extension])) {
160  unset(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extension]);
161  }
162  } else {
163  // Set full extension config
164  $configurationManager->setLocalConfigurationValueByPath('EXTENSIONS/' . $extension, $value);
165  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extension] = $value;
166  }
167  }
168 
177  public function ‪setAll(array $configuration, bool $skipWriteIfLocalConfigurationDoesNotExist = false): void
178  {
179  $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
180  if ($skipWriteIfLocalConfigurationDoesNotExist === false || @file_exists($configurationManager->getSystemConfigurationFileLocation())) {
181  $configurationManager->setLocalConfigurationValueByPath('EXTENSIONS', $configuration);
182  }
183  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'] = $configuration;
184  }
185 
195  public function ‪synchronizeExtConfTemplateWithLocalConfigurationOfAllExtensions(bool $skipWriteIfLocalConfigurationDoesNotExist = 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, $skipWriteIfLocalConfigurationDoesNotExist);
215  }
216  }
217 
226  public function ‪synchronizeExtConfTemplateWithLocalConfiguration(string $extensionKey): void
227  {
228  $package = GeneralUtility::makeInstance(PackageManager::class)->getPackage($extensionKey);
229  if (!@is_file($package->getPackagePath() . 'ext_conf_template.txt')) {
230  return;
231  }
232  $currentLocalConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extensionKey] ?? [];
233  $extConfTemplateConfiguration = $this->‪getExtConfTablesWithoutCommentsAsNestedArrayWithoutDots($extensionKey);
234  ArrayUtility::mergeRecursiveWithOverrule($extConfTemplateConfiguration, $currentLocalConfiguration);
235  // Write new config if changed. Loose array comparison to not write if only array key order is different
236  if ($extConfTemplateConfiguration != $currentLocalConfiguration) {
237  $this->set($extensionKey, $extConfTemplateConfiguration);
238  }
239  }
240 
247  protected function ‪getExtConfTablesWithoutCommentsAsNestedArrayWithoutDots(string $extensionKey): array
248  {
249  $rawConfigurationString = $this->‪getDefaultConfigurationRawString($extensionKey);
250  $typoScriptStringFactory = GeneralUtility::makeInstance(TypoScriptStringFactory::class);
251  $typoScriptTree = $typoScriptStringFactory->parseFromString($rawConfigurationString, new ‪AstBuilder(new ‪NoopEventDispatcher()));
252  return GeneralUtility::removeDotsFromTS($typoScriptTree->toArray());
253  }
254 
261  protected function ‪getDefaultConfigurationRawString(string $extensionKey): string
262  {
263  $rawString = '';
264  $extConfTemplateFileLocation = GeneralUtility::getFileAbsFileName(
265  'EXT:' . $extensionKey . '/ext_conf_template.txt'
266  );
267  if (file_exists($extConfTemplateFileLocation)) {
268  $rawString = (string)file_get_contents($extConfTemplateFileLocation);
269  }
270  return $rawString;
271  }
272 
273  protected function ‪hasConfiguration(string $extension): bool
274  {
275  return isset(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extension]) && is_array(‪$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'][$extension]);
276  }
277 }
‪TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException
Definition: ExtensionConfigurationPathDoesNotExistException.php:24
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration
Definition: ExtensionConfiguration.php:47
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\getDefaultConfigurationRawString
‪getDefaultConfigurationRawString(string $extensionKey)
Definition: ExtensionConfiguration.php:261
‪TYPO3\CMS\Core\Utility\ArrayUtility\isValidPath
‪static bool isValidPath(array $array, array|string $path, string $delimiter='/')
Definition: ArrayUtility.php:141
‪TYPO3\CMS\Core\Utility\ArrayUtility\getValueByPath
‪static getValueByPath(array $array, array|string $path, string $delimiter='/')
Definition: ArrayUtility.php:176
‪TYPO3\CMS\Core\TypoScript\AST\AstBuilder
Definition: AstBuilder.php:45
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\synchronizeExtConfTemplateWithLocalConfiguration
‪synchronizeExtConfTemplateWithLocalConfiguration(string $extensionKey)
Definition: ExtensionConfiguration.php:226
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Configuration
Definition: CKEditor5Migrator.php:18
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\getExtConfTablesWithoutCommentsAsNestedArrayWithoutDots
‪getExtConfTablesWithoutCommentsAsNestedArrayWithoutDots(string $extensionKey)
Definition: ExtensionConfiguration.php:247
‪TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException
Definition: ExtensionConfigurationExtensionNotConfiguredException.php:24
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\setAll
‪setAll(array $configuration, bool $skipWriteIfLocalConfigurationDoesNotExist=false)
Definition: ExtensionConfiguration.php:177
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\hasConfiguration
‪hasConfiguration(string $extension)
Definition: ExtensionConfiguration.php:273
‪TYPO3\CMS\Core\TypoScript\TypoScriptStringFactory
Definition: TypoScriptStringFactory.php:37
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration\synchronizeExtConfTemplateWithLocalConfigurationOfAllExtensions
‪synchronizeExtConfTemplateWithLocalConfigurationOfAllExtensions(bool $skipWriteIfLocalConfigurationDoesNotExist=false)
Definition: ExtensionConfiguration.php:195