| Total Complexity | 40 |
| Total Lines | 287 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SiteConfiguration often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SiteConfiguration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class SiteConfiguration implements SingletonInterface |
||
| 38 | { |
||
| 39 | protected PhpFrontend $cache; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $configPath; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Config yaml file name. |
||
| 48 | * |
||
| 49 | * @internal |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $configFileName = 'config.yaml'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Identifier to store all configuration data in cache_core cache. |
||
| 56 | * |
||
| 57 | * @internal |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $cacheIdentifier = 'sites-configuration'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Cache stores all configuration as Site objects, as long as they haven't been changed. |
||
| 64 | * This drastically improves performance as SiteFinder utilizes SiteConfiguration heavily |
||
| 65 | * |
||
| 66 | * @var array|null |
||
| 67 | */ |
||
| 68 | protected $firstLevelCache; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $configPath |
||
| 72 | * @param PhpFrontend $coreCache |
||
| 73 | */ |
||
| 74 | public function __construct(string $configPath, PhpFrontend $coreCache = null) |
||
| 75 | { |
||
| 76 | $this->configPath = $configPath; |
||
| 77 | // The following fallback to GeneralUtility;:getContainer() is only used in acceptance tests |
||
| 78 | // @todo: Fix testing-framework/typo3/sysext/core/Classes/Configuration/SiteConfiguration.php |
||
| 79 | // to inject the cache instance |
||
| 80 | $this->cache = $coreCache ?? GeneralUtility::getContainer()->get('cache.core'); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Return all site objects which have been found in the filesystem. |
||
| 85 | * |
||
| 86 | * @param bool $useCache |
||
| 87 | * @return Site[] |
||
| 88 | */ |
||
| 89 | public function getAllExistingSites(bool $useCache = true): array |
||
| 90 | { |
||
| 91 | if ($useCache && $this->firstLevelCache !== null) { |
||
| 92 | return $this->firstLevelCache; |
||
| 93 | } |
||
| 94 | return $this->resolveAllExistingSites($useCache); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Creates a site configuration with one language "English" which is the de-facto default language for TYPO3 in general. |
||
| 99 | * |
||
| 100 | * @param string $identifier |
||
| 101 | * @param int $rootPageId |
||
| 102 | * @param string $base |
||
| 103 | */ |
||
| 104 | public function createNewBasicSite(string $identifier, int $rootPageId, string $base): void |
||
| 105 | { |
||
| 106 | // Create a default site configuration called "main" as best practice |
||
| 107 | $this->write($identifier, [ |
||
| 108 | 'rootPageId' => $rootPageId, |
||
| 109 | 'base' => $base, |
||
| 110 | 'languages' => [ |
||
| 111 | 0 => [ |
||
| 112 | 'title' => 'English', |
||
| 113 | 'enabled' => true, |
||
| 114 | 'languageId' => 0, |
||
| 115 | 'base' => '/', |
||
| 116 | 'typo3Language' => 'default', |
||
| 117 | 'locale' => 'en_US.UTF-8', |
||
| 118 | 'iso-639-1' => 'en', |
||
| 119 | 'navigationTitle' => 'English', |
||
| 120 | 'hreflang' => 'en-us', |
||
| 121 | 'direction' => 'ltr', |
||
| 122 | 'flag' => 'us', |
||
| 123 | ], |
||
| 124 | ], |
||
| 125 | 'errorHandling' => [], |
||
| 126 | 'routes' => [], |
||
| 127 | ]); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Resolve all site objects which have been found in the filesystem. |
||
| 132 | * |
||
| 133 | * @param bool $useCache |
||
| 134 | * @return Site[] |
||
| 135 | */ |
||
| 136 | public function resolveAllExistingSites(bool $useCache = true): array |
||
| 137 | { |
||
| 138 | $sites = []; |
||
| 139 | $siteConfiguration = $this->getAllSiteConfigurationFromFiles($useCache); |
||
| 140 | foreach ($siteConfiguration as $identifier => $configuration) { |
||
| 141 | $rootPageId = (int)($configuration['rootPageId'] ?? 0); |
||
| 142 | if ($rootPageId > 0) { |
||
| 143 | $sites[$identifier] = GeneralUtility::makeInstance(Site::class, $identifier, $rootPageId, $configuration); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | $this->firstLevelCache = $sites; |
||
| 147 | return $sites; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Read the site configuration from config files. |
||
| 152 | * |
||
| 153 | * @param bool $useCache |
||
| 154 | * @return array |
||
| 155 | * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException |
||
| 156 | */ |
||
| 157 | protected function getAllSiteConfigurationFromFiles(bool $useCache = true): array |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Load plain configuration |
||
| 185 | * This method should only be used in case the original configuration as it exists in the file should be loaded, |
||
| 186 | * for example for writing / editing configuration. |
||
| 187 | * |
||
| 188 | * All read related actions should be performed on the site entity. |
||
| 189 | * |
||
| 190 | * @param string $siteIdentifier |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function load(string $siteIdentifier): array |
||
| 194 | { |
||
| 195 | $fileName = $this->configPath . '/' . $siteIdentifier . '/' . $this->configFileName; |
||
| 196 | $loader = GeneralUtility::makeInstance(YamlFileLoader::class); |
||
| 197 | return $loader->load(GeneralUtility::fixWindowsFilePath($fileName), YamlFileLoader::PROCESS_IMPORTS); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Add or update a site configuration |
||
| 202 | * |
||
| 203 | * @param string $siteIdentifier |
||
| 204 | * @param array $configuration |
||
| 205 | * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException |
||
| 206 | */ |
||
| 207 | public function write(string $siteIdentifier, array $configuration): void |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Renames a site identifier (and moves the folder) |
||
| 237 | * |
||
| 238 | * @param string $currentIdentifier |
||
| 239 | * @param string $newIdentifier |
||
| 240 | * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException |
||
| 241 | */ |
||
| 242 | public function rename(string $currentIdentifier, string $newIdentifier): void |
||
| 243 | { |
||
| 244 | $result = rename($this->configPath . '/' . $currentIdentifier, $this->configPath . '/' . $newIdentifier); |
||
| 245 | if (!$result) { |
||
| 246 | throw new \RuntimeException('Unable to rename folder sites/' . $currentIdentifier, 1522491300); |
||
| 247 | } |
||
| 248 | $this->cache->remove($this->cacheIdentifier); |
||
| 249 | $this->firstLevelCache = null; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Removes the config.yaml file of a site configuration. |
||
| 254 | * Also clears the cache. |
||
| 255 | * |
||
| 256 | * @param string $siteIdentifier |
||
| 257 | * @throws SiteNotFoundException |
||
| 258 | */ |
||
| 259 | public function delete(string $siteIdentifier): void |
||
| 260 | { |
||
| 261 | $sites = $this->getAllExistingSites(); |
||
| 262 | if (!isset($sites[$siteIdentifier])) { |
||
| 263 | throw new SiteNotFoundException('Site configuration named ' . $siteIdentifier . ' not found.', 1522866183); |
||
| 264 | } |
||
| 265 | $fileName = $this->configPath . '/' . $siteIdentifier . '/' . $this->configFileName; |
||
| 266 | if (!file_exists($fileName)) { |
||
| 267 | throw new SiteNotFoundException('Site configuration file ' . $this->configFileName . ' within the site ' . $siteIdentifier . ' not found.', 1522866184); |
||
| 268 | } |
||
| 269 | @unlink($fileName); |
||
| 270 | $this->cache->remove($this->cacheIdentifier); |
||
| 271 | $this->firstLevelCache = null; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param array $newConfiguration |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | protected function sortConfiguration(array $newConfiguration): array |
||
| 287 | } |
||
| 288 | |||
| 289 | protected static function findModified(array $currentConfiguration, array $newConfiguration): array |
||
| 307 | } |
||
| 308 | |||
| 309 | protected static function findRemoved(array $currentConfiguration, array $newConfiguration): array |
||
| 310 | { |
||
| 324 | } |
||
| 325 | } |
||
| 326 |