2 declare(strict_types = 1);
19 use Symfony\Component\Finder\Finder;
20 use Symfony\Component\Yaml\Yaml;
84 if ($useCache && $this->firstLevelCache !==
null) {
99 foreach ($siteConfiguration as $identifier => $configuration) {
100 $rootPageId = (int)($configuration[
'rootPageId'] ?? 0);
101 if ($rootPageId > 0) {
102 $sites[$identifier] = GeneralUtility::makeInstance(Site::class, $identifier, $rootPageId, $configuration);
105 $this->firstLevelCache = $sites;
118 if ($siteConfiguration = $this->
getCache()->
get($this->cacheIdentifier)) {
120 $siteConfiguration = preg_replace(
'/^<\?php\s*|\s*#$/',
'', $siteConfiguration);
121 $siteConfiguration = json_decode($siteConfiguration,
true);
125 if (empty($siteConfiguration)) {
128 $finder->files()->depth(0)->name($this->configFileName)->in($this->configPath .
'/*');
129 }
catch (\InvalidArgumentException $e) {
133 $loader = GeneralUtility::makeInstance(YamlFileLoader::class);
134 $siteConfiguration = [];
136 $configuration = $loader->load(GeneralUtility::fixWindowsFilePath((
string)$fileInfo));
137 $identifier = basename($fileInfo->getPath());
138 if (isset($configuration[
'site'])) {
140 'Site configuration with key \'site\' has been deprecated, remove indentation level and site key.',
143 $configuration = $configuration[
'site'];
145 $siteConfiguration[$identifier] = $configuration;
147 $this->
getCache()->
set($this->cacheIdentifier, json_encode($siteConfiguration));
149 return $siteConfiguration ?? [];
162 public function load(
string $siteIdentifier): array
165 $loader = GeneralUtility::makeInstance(YamlFileLoader::class);
176 public function write(
string $siteIdentifier, array $configuration): void
178 $folder = $this->configPath .
'/' . $siteIdentifier;
180 $newConfiguration = $configuration;
181 if (!file_exists($folder)) {
182 GeneralUtility::mkdir_deep($folder);
183 } elseif (file_exists($fileName)) {
184 $loader = GeneralUtility::makeInstance(YamlFileLoader::class);
186 $newConfiguration = $loader->load(GeneralUtility::fixWindowsFilePath($fileName), 0);
188 $processed = $loader->load(GeneralUtility::fixWindowsFilePath($fileName));
190 $newModified = array_replace_recursive(
191 self::findRemoved($processed, $configuration),
192 self::findModified($processed, $configuration)
198 $yamlFileContents = Yaml::dump($newConfiguration, 99, 2);
199 GeneralUtility::writeFile($fileName, $yamlFileContents);
200 $this->firstLevelCache =
null;
212 public function rename(
string $currentIdentifier,
string $newIdentifier): void
214 $result =
rename($this->configPath .
'/' . $currentIdentifier, $this->configPath .
'/' . $newIdentifier);
216 throw new \RuntimeException(
'Unable to rename folder sites/' . $currentIdentifier, 1522491300);
219 $this->firstLevelCache =
null;
229 public function delete(
string $siteIdentifier):
void
232 if (!isset($sites[$siteIdentifier])) {
233 throw new SiteNotFoundException(
'Site configuration named ' . $siteIdentifier .
' not found.', 1522866183);
236 if (!file_exists($fileName)) {
237 throw new SiteNotFoundException(
'Site configuration file ' . $this->configFileName .
' within the site ' . $siteIdentifier .
' not found.', 1522866184);
242 $this->firstLevelCache =
null;
251 protected function getCache(): FrontendInterface
253 return GeneralUtility::makeInstance(CacheManager::class)->getCache(
'cache_core');
262 ksort($newConfiguration);
263 if (isset($newConfiguration[
'imports'])) {
264 $imports = $newConfiguration[
'imports'];
265 unset($newConfiguration[
'imports']);
266 $newConfiguration[
'imports'] = $imports;
268 return $newConfiguration;
271 protected static function findModified(array $currentConfiguration, array $newConfiguration): array
274 foreach ($newConfiguration as $key => $value) {
275 if (!isset($currentConfiguration[$key]) || $currentConfiguration[$key] !== $newConfiguration[$key]) {
276 if (!isset($newConfiguration[$key]) && isset($currentConfiguration[$key])) {
277 $differences[$key] =
'__UNSET';
278 } elseif (isset($currentConfiguration[$key])
279 && is_array($newConfiguration[$key])
280 && is_array($currentConfiguration[$key])
282 $differences[$key] =
self::findModified($currentConfiguration[$key], $newConfiguration[$key]);
284 $differences[$key] = $value;
291 protected static function findRemoved(array $currentConfiguration, array $newConfiguration): array
294 foreach ($currentConfiguration as $key => $value) {
295 if (!isset($newConfiguration[$key])) {
296 $removed[$key] =
'__UNSET';
297 } elseif (isset($currentConfiguration[$key]) && is_array($currentConfiguration[$key]) && is_array($newConfiguration[$key])) {
298 $removedInRecursion =
self::findRemoved($currentConfiguration[$key], $newConfiguration[$key]);
299 if (!empty($removedInRecursion)) {
300 $removed[$key] = $removedInRecursion;