‪TYPO3CMS  ‪main
SiteSettingsFactory.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 
18 namespace ‪TYPO3\CMS\Core\Site;
19 
20 use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
21 use Symfony\Component\DependencyInjection\Attribute\Autowire;
31 
32 #[Autoconfigure(public: true)]
33 readonly class ‪SiteSettingsFactory
34 {
35  public function ‪__construct(
36  #[Autowire('%env(TYPO3:configPath)%/sites')]
37  protected string $configPath,
38  protected ‪SetRegistry $setRegistry,
39  protected ‪SettingsTypeRegistry $settingsTypeRegistry,
40  protected ‪YamlFileLoader $yamlFileLoader,
41  #[Autowire(service: 'cache.core')]
42  protected readonly ‪PhpFrontend $cache,
43  #[Autowire(expression: 'service("package-dependent-cache-identifier").withPrefix("SiteSettings")')]
44  protected ‪PackageDependentCacheIdentifier $cacheIdentifier,
45  protected string $settingsFileName = 'settings.yaml',
46  ) {}
47 
48  public function ‪getSettings(string ‪$siteIdentifier, array $siteConfiguration): ‪SiteSettings
49  {
50  $cacheIdentifier = $this->cacheIdentifier->withAdditionalHashedIdentifier(
51  ‪$siteIdentifier . '_' . json_encode($siteConfiguration)
52  )->toString();
53 
54  try {
55  $settings = $this->cache->require($cacheIdentifier);
56  if ($settings instanceof ‪SiteSettings) {
57  return $settings;
58  }
59  } catch (\Error) {
60  }
61 
62  $settings = $this->‪createSettings($siteIdentifier, $siteConfiguration);
63  $this->cache->set($cacheIdentifier, 'return ' . var_export($settings, true) . ';');
64  return $settings;
65  }
66 
76  public function ‪createSettings(string ‪$siteIdentifier, array $siteConfiguration): ‪SiteSettings
77  {
78  $sets = $siteConfiguration['dependencies'] ?? [];
79  $settings = [];
80 
81  $definitions = [];
82  $activeSets = [];
83  if (is_array($sets) && $sets !== []) {
84  $activeSets = $this->setRegistry->getSets(...$sets);
85  }
86 
87  foreach ($activeSets as $set) {
88  foreach ($set->settingsDefinitions as $settingDefinition) {
89  $definitions[] = $settingDefinition;
90  }
91  }
92 
93  foreach ($definitions as $settingDefinition) {
94  $settings = ‪ArrayUtility::setValueByPath($settings, $settingDefinition->key, $settingDefinition->default, '.');
95  }
96 
97  foreach ($activeSets as $set) {
98  ArrayUtility::mergeRecursiveWithOverrule($settings, $this->‪validateSettings($set->settings, $definitions));
99  }
100 
101  $fileName = $this->configPath . '/' . ‪$siteIdentifier . '/' . $this->settingsFileName;
102  if (file_exists($fileName)) {
103  $siteSettings = $this->yamlFileLoader->load(GeneralUtility::fixWindowsFilePath($fileName));
104  } else {
105  $siteSettings = $siteConfiguration['settings'] ?? [];
106  }
107 
108  ArrayUtility::mergeRecursiveWithOverrule($settings, $this->‪validateSettings($siteSettings, $definitions));
109 
110  return new ‪SiteSettings($settings);
111  }
112 
113  protected function ‪validateSettings(array $settings, array $definitions): array
114  {
115  foreach ($definitions as $definition) {
116  try {
117  $value = ‪ArrayUtility::getValueByPath($settings, $definition->key, '.');
118  } catch (‪MissingArrayPathException) {
119  continue;
120  }
121  if (!$this->settingsTypeRegistry->has($definition->type)) {
122  throw new \RuntimeException('Setting type ' . $definition->type . ' is not defined.', 1712437727);
123  }
124  $type = $this->settingsTypeRegistry->get($definition->type);
125  if (!$type->validate($value, $definition)) {
126  $settings = ‪ArrayUtility::removeByPath($settings, $definition->key, '.');
127  }
128 
129  $newValue = $type->transformValue($value, $definition);
130  if ($newValue !== $value) {
131  ‪ArrayUtility::setValueByPath($settings, $definition->key, $newValue, '.');
132  }
133  }
134 
135  return $settings;
136  }
137 
138 }
‪TYPO3\CMS\Core\Utility\ArrayUtility\removeByPath
‪static array removeByPath(array $array, string $path, string $delimiter='/')
Definition: ArrayUtility.php:303
‪TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException
Definition: MissingArrayPathException.php:27
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:25
‪TYPO3\CMS\Core\Package\Cache\PackageDependentCacheIdentifier
Definition: PackageDependentCacheIdentifier.php:30
‪TYPO3\CMS\Core\Settings\SettingsTypeRegistry
Definition: SettingsTypeRegistry.php:27
‪TYPO3\CMS\Core\Site\SiteSettingsFactory\__construct
‪__construct(#[Autowire('%env(TYPO3:configPath)%/sites')] protected string $configPath, protected SetRegistry $setRegistry, protected SettingsTypeRegistry $settingsTypeRegistry, protected YamlFileLoader $yamlFileLoader, #[Autowire(service:'cache.core')] protected readonly PhpFrontend $cache, #[Autowire(expression:'service("package-dependent-cache-identifier").withPrefix("SiteSettings")')] protected PackageDependentCacheIdentifier $cacheIdentifier, protected string $settingsFileName='settings.yaml',)
Definition: SiteSettingsFactory.php:35
‪TYPO3\CMS\Core\Utility\ArrayUtility\getValueByPath
‪static getValueByPath(array $array, array|string $path, string $delimiter='/')
Definition: ArrayUtility.php:176
‪TYPO3\CMS\Core\Site\Set\SetRegistry
Definition: SetRegistry.php:30
‪TYPO3\CMS\Core\Site
‪TYPO3\CMS\Core\Site\SiteSettingsFactory\createSettings
‪createSettings(string $siteIdentifier, array $siteConfiguration)
Definition: SiteSettingsFactory.php:76
‪TYPO3\CMS\Core\Site\SiteSettingsFactory\validateSettings
‪validateSettings(array $settings, array $definitions)
Definition: SiteSettingsFactory.php:113
‪TYPO3\CMS\Core\Site\SiteSettingsFactory\getSettings
‪getSettings(string $siteIdentifier, array $siteConfiguration)
Definition: SiteSettingsFactory.php:48
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader
Definition: YamlFileLoader.php:47
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪TYPO3\CMS\Core\Utility\ArrayUtility\setValueByPath
‪static array setValueByPath(array $array, string|array|\ArrayAccess $path, mixed $value, string $delimiter='/')
Definition: ArrayUtility.php:261
‪TYPO3\CMS\Webhooks\Message\$siteIdentifier
‪identifier readonly int readonly array readonly string readonly string $siteIdentifier
Definition: PageModificationMessage.php:38
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Site\Entity\SiteSettings
Definition: SiteSettings.php:29
‪TYPO3\CMS\Core\Site\SiteSettingsFactory
Definition: SiteSettingsFactory.php:34