TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Site\Set;
|
||||
|
||||
use TYPO3\CMS\Core\Settings\Category;
|
||||
use TYPO3\CMS\Core\Settings\CategoryAccumulator;
|
||||
|
||||
class CategoryRegistry
|
||||
{
|
||||
public function __construct(
|
||||
protected SetRegistry $setRegistry,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve list of instantiated categories for the list of
|
||||
* provided $setNames, including their dependencies (recursive)
|
||||
*
|
||||
* @return list<Category>
|
||||
*/
|
||||
public function getCategories(string ...$setNames): array
|
||||
{
|
||||
$sets = $this->setRegistry->getSets(...$setNames);
|
||||
$categories = [];
|
||||
|
||||
$categoryDefinitions = [];
|
||||
foreach ($sets as $set) {
|
||||
foreach ($set->categoryDefinitions as $definition) {
|
||||
$categoryDefinitions[$definition->key] = $definition;
|
||||
}
|
||||
}
|
||||
$settingsDefinitions = [];
|
||||
foreach ($sets as $set) {
|
||||
foreach ($set->settingsDefinitions as $definition) {
|
||||
$settingsDefinitions[$definition->key] = $definition;
|
||||
}
|
||||
}
|
||||
|
||||
$cateryAccumulator = new CategoryAccumulator();
|
||||
return $cateryAccumulator->getCategories(
|
||||
$categoryDefinitions,
|
||||
$settingsDefinitions,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Site\Set;
|
||||
|
||||
/**
|
||||
* @internal Only to be used by internal site settings functionality
|
||||
*/
|
||||
final class InvalidCategoryDefinitionsException extends \RuntimeException
|
||||
{
|
||||
private readonly string $setName;
|
||||
|
||||
public function __construct(
|
||||
string $message = '',
|
||||
int $code = 0,
|
||||
?\Throwable $previous = null,
|
||||
string $setName = '',
|
||||
) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->setName = $setName;
|
||||
}
|
||||
|
||||
public function getSetName(): string
|
||||
{
|
||||
return $this->setName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Site\Set;
|
||||
|
||||
/**
|
||||
* @internal Only to be used by internal site settings functionality
|
||||
*/
|
||||
class InvalidSetException extends \RuntimeException
|
||||
{
|
||||
private readonly string $setName;
|
||||
|
||||
public function __construct(
|
||||
string $message = '',
|
||||
int $code = 0,
|
||||
?\Throwable $previous = null,
|
||||
string $setName = '',
|
||||
) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->setName = $setName;
|
||||
}
|
||||
|
||||
public function getSetName(): string
|
||||
{
|
||||
return $this->setName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Site\Set;
|
||||
|
||||
/**
|
||||
* @internal Only to be used by internal site set functionality
|
||||
*/
|
||||
final class InvalidSetRouteEnhancersException extends \RuntimeException
|
||||
{
|
||||
private readonly string $setName;
|
||||
|
||||
public function __construct(
|
||||
string $message = '',
|
||||
int $code = 0,
|
||||
?\Throwable $previous = null,
|
||||
string $setName = '',
|
||||
) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->setName = $setName;
|
||||
}
|
||||
|
||||
public function getSetName(): string
|
||||
{
|
||||
return $this->setName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Site\Set;
|
||||
|
||||
/**
|
||||
* @internal Only to be used by internal site settings functionality
|
||||
*/
|
||||
final class InvalidSettingsDefinitionsException extends \RuntimeException
|
||||
{
|
||||
private readonly string $setName;
|
||||
|
||||
public function __construct(
|
||||
string $message = '',
|
||||
int $code = 0,
|
||||
?\Throwable $previous = null,
|
||||
string $setName = '',
|
||||
) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->setName = $setName;
|
||||
}
|
||||
|
||||
public function getSetName(): string
|
||||
{
|
||||
return $this->setName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Site\Set;
|
||||
|
||||
/**
|
||||
* @internal Only to be used by internal site settings functionality
|
||||
*/
|
||||
final class InvalidSettingsException extends \RuntimeException
|
||||
{
|
||||
private readonly string $setName;
|
||||
|
||||
public function __construct(
|
||||
string $message = '',
|
||||
int $code = 0,
|
||||
?\Throwable $previous = null,
|
||||
string $setName = '',
|
||||
) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->setName = $setName;
|
||||
}
|
||||
|
||||
public function getSetName(): string
|
||||
{
|
||||
return $this->setName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Site\Set;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class SetCollector
|
||||
{
|
||||
/** @var array<string, SetDefinition> */
|
||||
protected array $sets = [];
|
||||
|
||||
/** @var array<string, array{ error: SetError, name: string, context: string }> */
|
||||
protected array $invalidSets = [];
|
||||
|
||||
/**
|
||||
* @return array<string, SetDefinition>
|
||||
*/
|
||||
public function getSetDefinitions(): array
|
||||
{
|
||||
return $this->sets;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array{ error: SetError, name: string, context: string }>
|
||||
*/
|
||||
public function getInvalidSets(): array
|
||||
{
|
||||
return $this->invalidSets;
|
||||
}
|
||||
|
||||
public function add(SetDefinition $set): void
|
||||
{
|
||||
$this->sets[$set->name] = $set;
|
||||
}
|
||||
|
||||
public function addError(SetError $error, string $name, string $context): void
|
||||
{
|
||||
$this->invalidSets[$name] = [
|
||||
'error' => $error,
|
||||
'name' => $name,
|
||||
'context' => $context,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Site\Set;
|
||||
|
||||
use TYPO3\CMS\Core\Settings\CategoryDefinition;
|
||||
use TYPO3\CMS\Core\Settings\SettingDefinition;
|
||||
|
||||
readonly class SetDefinition
|
||||
{
|
||||
/**
|
||||
* @param list<string> $dependencies
|
||||
* @param SettingDefinition[] $settingsDefinitions
|
||||
* @param CategoryDefinition[] $categoryDefinitions
|
||||
* @param array<string, array<string, mixed>> $routeEnhancers Route enhancers keyed by identifier
|
||||
*/
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public string $label,
|
||||
public array $dependencies = [],
|
||||
public array $optionalDependencies = [],
|
||||
public array $settingsDefinitions = [],
|
||||
public array $categoryDefinitions = [],
|
||||
public ?string $typoscript = null,
|
||||
public ?string $pagets = null,
|
||||
public array $settings = [],
|
||||
public bool $hidden = false,
|
||||
public array $routeEnhancers = [],
|
||||
) {}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return array_filter(get_object_vars($this), fn(mixed $value) => $value !== null && $value !== []);
|
||||
}
|
||||
|
||||
public static function __set_state(array $state): self
|
||||
{
|
||||
return new self(...$state);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Site\Set;
|
||||
|
||||
enum SetError: string
|
||||
{
|
||||
case notFound = 'not-found';
|
||||
case missingDependency = 'missing-dependency';
|
||||
case invalidSettingsDefinitions = 'invalid-settings-definitions';
|
||||
case invalidCategoryDefinitions = 'invalid-category-definitions';
|
||||
case invalidSettings = 'invalid-settings';
|
||||
case invalidRouteEnhancers = 'invalid-route-enhancers';
|
||||
case invalidSet = 'invalid-set';
|
||||
|
||||
public function getLabel(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::notFound => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.siteSet.notFound',
|
||||
self::missingDependency => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.siteSet.missingDependency',
|
||||
self::invalidSettingsDefinitions => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.siteSet.invalidSettingsDefinitions',
|
||||
self::invalidCategoryDefinitions => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.siteSet.invalidCategoryDefinitions',
|
||||
self::invalidSettings => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.siteSet.invalidSettings',
|
||||
self::invalidRouteEnhancers => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.siteSet.invalidRouteEnhancers',
|
||||
self::invalidSet => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.siteSet.invalidSet',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Site\Set;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use TYPO3\CMS\Core\Attribute\AsEventListener;
|
||||
use TYPO3\CMS\Core\Cache\Event\CacheWarmupEvent;
|
||||
use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend;
|
||||
use TYPO3\CMS\Core\Service\DependencyOrderingService;
|
||||
|
||||
#[Autoconfigure(public: true)]
|
||||
class SetRegistry
|
||||
{
|
||||
/** @var list<SetDefinition>|null */
|
||||
protected ?array $orderedSets = null;
|
||||
|
||||
/** @var array<string, array{ error: SetError, name: string, context: string }> */
|
||||
protected ?array $invalidSets = null;
|
||||
|
||||
public function __construct(
|
||||
protected DependencyOrderingService $dependencyOrderingService,
|
||||
#[Autowire(expression: 'service("package-dependent-cache-identifier").withPrefix("Sets").toString()')]
|
||||
protected readonly string $cacheIdentifier,
|
||||
#[Autowire(service: 'cache.core')]
|
||||
protected readonly PhpFrontend $cache,
|
||||
#[Autowire(lazy: true)]
|
||||
protected SetCollector $setCollector,
|
||||
protected LoggerInterface $logger,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve list of ordered sets, matched by
|
||||
* $setNames, including their dependencies (recursive)
|
||||
*
|
||||
* @return list<SetDefinition>
|
||||
*/
|
||||
public function getSets(string ...$setNames): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$this->getOrderedSets(),
|
||||
fn(SetDefinition $set): bool
|
||||
=> in_array($set->name, $setNames, true)
|
||||
|| $this->hasDependency($setNames, $set->name)
|
||||
));
|
||||
}
|
||||
|
||||
public function hasSet(string $setName): bool
|
||||
{
|
||||
return isset($this->getOrderedSets()[$setName]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, SetDefinition>
|
||||
* @internal
|
||||
*/
|
||||
public function getAllSets(): array
|
||||
{
|
||||
return $this->getOrderedSets();
|
||||
}
|
||||
|
||||
public function getSet(string $setName): ?SetDefinition
|
||||
{
|
||||
return $this->getOrderedSets()[$setName] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array{ error: SetError, name: string, context: string }>
|
||||
*/
|
||||
public function getInvalidSets(): array
|
||||
{
|
||||
// create ordered sets which logs invalidSets as out-of-band data
|
||||
if ($this->orderedSets === null) {
|
||||
$this->getOrderedSets();
|
||||
}
|
||||
return $this->invalidSets;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, SetDefinition>
|
||||
*/
|
||||
protected function getOrderedSets(): array
|
||||
{
|
||||
return $this->orderedSets ?? $this->getFromCache() ?? $this->computeOrderedSets();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, SetDefinition>
|
||||
*/
|
||||
protected function getFromCache(): ?array
|
||||
{
|
||||
if (!$this->cache->has($this->cacheIdentifier)) {
|
||||
return null;
|
||||
}
|
||||
$setData = null;
|
||||
try {
|
||||
$setData = $this->cache->require($this->cacheIdentifier);
|
||||
} catch (\Error) {
|
||||
}
|
||||
if ($setData === false) {
|
||||
// Cache entry has been removed in the meantime
|
||||
return null;
|
||||
}
|
||||
if (!is_array($setData) || !isset($setData['orderedSets']) || !isset($setData['invalidSets'])) {
|
||||
throw new \RuntimeException('Invalid "Site Sets" cache entry', 1727809282);
|
||||
}
|
||||
$this->orderedSets = $setData['orderedSets'];
|
||||
$this->invalidSets = $setData['invalidSets'];
|
||||
return $this->orderedSets;
|
||||
}
|
||||
|
||||
protected function checkMissingDependencies(array $sets, SetDefinition $set): ?string
|
||||
{
|
||||
foreach ($set->dependencies as $dependencyName) {
|
||||
$dependency = $sets[$dependencyName] ?? null;
|
||||
if ($dependency === null) {
|
||||
return $dependencyName;
|
||||
}
|
||||
$missingSubDependency = $this->checkMissingDependencies($sets, $dependency);
|
||||
if ($missingSubDependency !== null) {
|
||||
return $dependencyName . '[' . $missingSubDependency . ']';
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, SetDefinition>
|
||||
*/
|
||||
protected function computeOrderedSets(): array
|
||||
{
|
||||
$tmp = [];
|
||||
$this->invalidSets = $this->setCollector->getInvalidSets();
|
||||
$sets = $this->setCollector->getSetDefinitions();
|
||||
foreach ($sets as $set) {
|
||||
$missingDependency = $this->checkMissingDependencies($sets, $set);
|
||||
if ($missingDependency !== null) {
|
||||
$this->logger->error('Invalid set "{name}": Missing dependency "{dependency}"', [
|
||||
'name' => $set->name,
|
||||
'dependency' => $missingDependency,
|
||||
]);
|
||||
$this->invalidSets[$set->name] = [
|
||||
'error' => SetError::missingDependency,
|
||||
'name' => $set->name,
|
||||
'context' => $missingDependency,
|
||||
];
|
||||
continue;
|
||||
}
|
||||
$tmp[$set->name] = [
|
||||
'set' => $set,
|
||||
'after' => $set->dependencies,
|
||||
'after-resilient' => array_filter($set->optionalDependencies, static fn($dependency) => isset($sets[$dependency])),
|
||||
];
|
||||
}
|
||||
|
||||
$this->orderedSets = array_map(
|
||||
static fn(array $data): SetDefinition => $data['set'],
|
||||
$this->dependencyOrderingService->orderByDependencies($tmp)
|
||||
);
|
||||
|
||||
$setData = [
|
||||
'orderedSets' => $this->orderedSets,
|
||||
'invalidSets' => $this->invalidSets,
|
||||
];
|
||||
$this->cache->set($this->cacheIdentifier, 'return ' . var_export($setData, true) . ';');
|
||||
return $this->orderedSets;
|
||||
}
|
||||
|
||||
protected function hasDependency(array $setNames, string $dependency): bool
|
||||
{
|
||||
foreach ($setNames as $setName) {
|
||||
$set = $this->getSet($setName);
|
||||
if ($set === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in_array($dependency, $set->dependencies, true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (in_array($dependency, $set->optionalDependencies, true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->hasDependency($set->dependencies, $dependency)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->hasDependency($set->optionalDependencies, $dependency)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#[AsEventListener('typo3-core/set-registry')]
|
||||
public function warmupCaches(CacheWarmupEvent $event): void
|
||||
{
|
||||
if ($event->hasGroup('system')) {
|
||||
$this->computeOrderedSets();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,371 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\Site\Set;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use TYPO3\CMS\Core\Configuration\Loader\Exception\YamlParseException;
|
||||
use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader;
|
||||
use TYPO3\CMS\Core\Settings\CategoryDefinition;
|
||||
use TYPO3\CMS\Core\Settings\InvalidSettingDefinitionException;
|
||||
use TYPO3\CMS\Core\Settings\SettingDefinition;
|
||||
use TYPO3\CMS\Core\Settings\SettingDefinitionValidation;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
#[Autoconfigure(public: true)]
|
||||
class YamlSetDefinitionProvider
|
||||
{
|
||||
/** @var array<string, SetDefinition> */
|
||||
protected array $sets = [];
|
||||
public function __construct(
|
||||
protected readonly SettingDefinitionValidation $settingDefinitionValidation,
|
||||
protected readonly YamlFileLoader $yamlFileLoader,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array<string, SetDefinition>
|
||||
*/
|
||||
public function getSetDefinitions(): array
|
||||
{
|
||||
return $this->sets;
|
||||
}
|
||||
|
||||
public function addSet(SetDefinition $set): void
|
||||
{
|
||||
$this->sets[$set->name] = $set;
|
||||
}
|
||||
|
||||
public function get(\SplFileInfo $fileInfo, ?string $virtualSetPath = null): SetDefinition
|
||||
{
|
||||
$filename = GeneralUtility::fixWindowsFilePath($fileInfo->getPathname());
|
||||
$path = dirname($filename);
|
||||
$virtualSetPath ??= $path . '/';
|
||||
// No placeholders or imports processed on purpose
|
||||
// Use dependencies for shared sets
|
||||
try {
|
||||
$set = Yaml::parseFile($filename);
|
||||
} catch (ParseException $e) {
|
||||
$source = $virtualSetPath . basename($filename);
|
||||
throw new InvalidSetException('Failed to parse set definition from "' . $source . '": ' . $e->getMessage(), 1711024370, $e);
|
||||
}
|
||||
$setName = $set['name'] ?? '';
|
||||
|
||||
$settingsDefinitionsFile = $path . '/settings.definitions.yaml';
|
||||
if (is_file($settingsDefinitionsFile)) {
|
||||
try {
|
||||
$settingsDefinitions = Yaml::parseFile($settingsDefinitionsFile, Yaml::PARSE_OBJECT | Yaml::PARSE_OBJECT_FOR_MAP);
|
||||
} catch (ParseException $e) {
|
||||
$source = $virtualSetPath . basename($settingsDefinitionsFile);
|
||||
throw new InvalidSettingsDefinitionsException(
|
||||
'Invalid settings definition. Source: ' . $source,
|
||||
1711024374,
|
||||
$e,
|
||||
$setName
|
||||
);
|
||||
}
|
||||
if (!is_object($settingsDefinitions->settings ?? null)) {
|
||||
$source = $virtualSetPath . basename($settingsDefinitionsFile);
|
||||
throw new InvalidSettingsDefinitionsException(
|
||||
'Missing "settings" key in settings definitions. Source: ' . $source,
|
||||
1711024378,
|
||||
null,
|
||||
$setName
|
||||
);
|
||||
}
|
||||
// YAML maps are decoded as objects. Normalize them to arrays so
|
||||
// settings/category definitions can be handled uniformly below.
|
||||
$set['settingsDefinitions'] = array_map(
|
||||
static fn(?object $value): ?array => $value === null ? null : (array)$value,
|
||||
get_object_vars($settingsDefinitions->settings)
|
||||
);
|
||||
$set['categoryDefinitions'] = [];
|
||||
if (isset($settingsDefinitions->categories)) {
|
||||
$set['categoryDefinitions'] = array_map(
|
||||
static fn(?object $value): ?array => $value === null ? null : (array)$value,
|
||||
get_object_vars($settingsDefinitions->categories)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$settingsFile = $path . '/settings.yaml';
|
||||
if (is_file($settingsFile)) {
|
||||
try {
|
||||
// HEADS UP: YamlFileLoader::PROCESS_PLACEHOLDERS is omitted on purpose and MUST NOT be added.
|
||||
// Site sets are intended to be self-contained and must not rely on implicit
|
||||
// dependencies to global (environment) variables.
|
||||
$settings = $this->yamlFileLoader->load($settingsFile, YamlFileLoader::PROCESS_IMPORTS | YamlFileLoader::ALLOW_EMPTY_FILE);
|
||||
} catch (YamlParseException $e) {
|
||||
$source = $virtualSetPath . basename($settingsFile);
|
||||
throw new InvalidSettingsException('Invalid settings format. Source: ' . $source, 1711024380, $e, $setName);
|
||||
}
|
||||
$set['settings'] = $settings;
|
||||
}
|
||||
|
||||
$routeEnhancersFile = $path . '/route-enhancers.yaml';
|
||||
if (is_file($routeEnhancersFile)) {
|
||||
try {
|
||||
$routeEnhancers = $this->yamlFileLoader->load($routeEnhancersFile, YamlFileLoader::PROCESS_IMPORTS | YamlFileLoader::ALLOW_EMPTY_FILE);
|
||||
} catch (YamlParseException $e) {
|
||||
$source = $virtualSetPath . basename($routeEnhancersFile);
|
||||
throw new InvalidSetRouteEnhancersException(
|
||||
'Invalid route enhancers format. Source: ' . $source,
|
||||
1764749081,
|
||||
$e,
|
||||
$setName
|
||||
);
|
||||
}
|
||||
if ($routeEnhancers !== [] && !is_array($routeEnhancers['routeEnhancers'] ?? null)) {
|
||||
$source = $virtualSetPath . basename($routeEnhancersFile);
|
||||
throw new InvalidSetRouteEnhancersException(
|
||||
'Missing "routeEnhancers" key in route enhancers file. Source: ' . $source,
|
||||
1764749082,
|
||||
null,
|
||||
$setName
|
||||
);
|
||||
}
|
||||
if ($routeEnhancers !== [] && array_keys($routeEnhancers) !== ['routeEnhancers']) {
|
||||
$source = $virtualSetPath . basename($routeEnhancersFile);
|
||||
throw new InvalidSetRouteEnhancersException(
|
||||
'Superfluous keys in route enhancers file. Use "routeEnhancers" as root level key. Source: ' . $source,
|
||||
1764749083,
|
||||
null,
|
||||
$setName
|
||||
);
|
||||
}
|
||||
$set['routeEnhancers'] = $routeEnhancers['routeEnhancers'] ?? [];
|
||||
}
|
||||
|
||||
if (($set['labels'] ?? '') === '') {
|
||||
if (is_file($path . '/labels.xlf')) {
|
||||
$set['labels'] = $virtualSetPath . 'labels.xlf';
|
||||
}
|
||||
}
|
||||
|
||||
return $this->createDefinition($set, $virtualSetPath);
|
||||
}
|
||||
|
||||
protected function createDefinition(array $set, string $basePath): SetDefinition
|
||||
{
|
||||
$settingsDefinitions = [];
|
||||
$labels = $set['labels'] ?? null;
|
||||
unset($set['labels']);
|
||||
|
||||
if ($labels) {
|
||||
$set['label'] ??= 'LLL:' . $labels . ':label';
|
||||
}
|
||||
|
||||
foreach (($set['settingsDefinitions'] ?? []) as $setting => $options) {
|
||||
// Cast objects to arrays
|
||||
if (is_object($options['options'] ?? null)) {
|
||||
$options['options'] = (array)$options['options'];
|
||||
}
|
||||
|
||||
if (is_array($options['enum'] ?? null)) {
|
||||
$options['enum'] = array_combine(
|
||||
$options['enum'],
|
||||
array_map(
|
||||
static fn(string|int|float|bool $value): string => sprintf(
|
||||
'{label}:settings.%s.enum.%s',
|
||||
$setting,
|
||||
is_bool($value) ? ($value ? 'true' : 'false') : (string)$value
|
||||
),
|
||||
$options['enum']
|
||||
)
|
||||
);
|
||||
} elseif (is_object($options['enum'] ?? null)) {
|
||||
$options['enum'] = (array)$options['enum'];
|
||||
}
|
||||
if (is_array($options['enum'] ?? null)) {
|
||||
foreach ($options['enum'] as $enumValue => $enumLabel) {
|
||||
if ($enumLabel === null) {
|
||||
$options['enum'][$enumValue] = (string)$enumValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_object($options['tags'] ?? null)) {
|
||||
$options['tags'] = array_values((array)$options['tags']);
|
||||
}
|
||||
if ($labels) {
|
||||
$domain = 'LLL:' . $labels . ':';
|
||||
$options['label'] ??= $domain . 'settings.' . $setting;
|
||||
$options['description'] ??= $domain . 'settings.description.' . $setting;
|
||||
if (is_array($options['enum'] ?? null)) {
|
||||
foreach ($options['enum'] as $enumValue => $enumLabel) {
|
||||
if (is_string($enumLabel) && str_starts_with($enumLabel, '{label}:')) {
|
||||
$options['enum'][$enumValue] = $domain . substr($enumLabel, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$settingDefinitionData = [...['key' => $setting], ...$options];
|
||||
try {
|
||||
$definition = new SettingDefinition(...$settingDefinitionData);
|
||||
} catch (\Error $e) {
|
||||
throw new InvalidSettingsDefinitionsException(
|
||||
'Invalid setting definition "' . $setting . '": ' . json_encode($options) . ' – ' . $this->getObjectConstructionErrors($e, SettingDefinition::class, $settingDefinitionData),
|
||||
1702623312,
|
||||
$e,
|
||||
$set['name'] ?? ''
|
||||
);
|
||||
}
|
||||
try {
|
||||
$this->settingDefinitionValidation->validate($definition);
|
||||
} catch (InvalidSettingDefinitionException $e) {
|
||||
throw new InvalidSettingsDefinitionsException(
|
||||
$e->getMessage(),
|
||||
1752483401,
|
||||
$e,
|
||||
$set['name'] ?? ''
|
||||
);
|
||||
}
|
||||
$settingsDefinitions[] = $definition;
|
||||
}
|
||||
|
||||
$categoryDefinitions = [];
|
||||
foreach (($set['categoryDefinitions'] ?? []) as $category => $options) {
|
||||
if ($labels) {
|
||||
$options['label'] ??= 'LLL:' . $labels . ':categories.' . $category;
|
||||
$options['description'] ??= 'LLL:' . $labels . ':categories.description.' . $category;
|
||||
}
|
||||
try {
|
||||
$definition = new CategoryDefinition(...[...['key' => $category], ...$options]);
|
||||
} catch (\Error $e) {
|
||||
throw new InvalidCategoryDefinitionsException(
|
||||
'Invalid category definition "' . $category . '": ' . json_encode($options),
|
||||
1702623313,
|
||||
$e,
|
||||
$set['name'] ?? ''
|
||||
);
|
||||
}
|
||||
$categoryDefinitions[] = $definition;
|
||||
}
|
||||
|
||||
foreach (($set['routeEnhancers'] ?? []) as $identifier => $config) {
|
||||
if (!is_array($config)) {
|
||||
throw new InvalidSetRouteEnhancersException(
|
||||
sprintf('Invalid route enhancer definition "%s": expected array, got %s', $identifier, gettype($config)),
|
||||
1732800002,
|
||||
null,
|
||||
$set['name'] ?? ''
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$setData = [
|
||||
...$set,
|
||||
'settingsDefinitions' => $settingsDefinitions,
|
||||
'categoryDefinitions' => $categoryDefinitions,
|
||||
];
|
||||
$setData['typoscript'] ??= $basePath;
|
||||
$setData['pagets'] ??= $basePath . 'page.tsconfig';
|
||||
try {
|
||||
return new SetDefinition(...$setData);
|
||||
} catch (\Error $e) {
|
||||
throw new InvalidSetException(
|
||||
'Invalid set definition: ' . json_encode($set) . ' – ' . $this->getObjectConstructionErrors($e, SetDefinition::class, $setData),
|
||||
1170859526,
|
||||
$e,
|
||||
$set['name'] ?? ''
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getObjectConstructionErrors(
|
||||
\Error $error,
|
||||
string $className,
|
||||
array $arguments,
|
||||
): string {
|
||||
$reflection = new \ReflectionClass($className);
|
||||
$constructor = $reflection->getConstructor();
|
||||
$parameters = $constructor->getParameters();
|
||||
$missingParameters = [];
|
||||
$typeErrors = [];
|
||||
foreach ($parameters as $parameter) {
|
||||
if (isset($arguments[$parameter->name])) {
|
||||
$value = $arguments[$parameter->name];
|
||||
unset($arguments[$parameter->name]);
|
||||
$type = $parameter->getType();
|
||||
if (!$this->typeMatches($type, $value)) {
|
||||
$typeErrors[$parameter->name] = (string)$type;
|
||||
}
|
||||
} elseif (!$parameter->isDefaultValueAvailable()) {
|
||||
$missingParameters[] = $parameter->name;
|
||||
}
|
||||
}
|
||||
|
||||
$errors = [];
|
||||
if ($missingParameters !== []) {
|
||||
$errors[] = 'Missing properties: ' . implode(', ', $missingParameters);
|
||||
}
|
||||
if ($arguments !== []) {
|
||||
$errors[] = 'Invalid properties: ' . implode(', ', array_keys($arguments));
|
||||
}
|
||||
if ($typeErrors !== []) {
|
||||
$errors[] = 'Invalid type: ' . implode(', ', array_keys($typeErrors));
|
||||
}
|
||||
|
||||
if ($errors === []) {
|
||||
return $error->getMessage();
|
||||
}
|
||||
|
||||
return implode('; ', $errors);
|
||||
}
|
||||
|
||||
protected function typeMatches(
|
||||
\ReflectionType $type,
|
||||
mixed $value
|
||||
): bool {
|
||||
if ($type->allowsNull() && $value === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($type instanceof \ReflectionUnionType) {
|
||||
foreach ($type->getTypes() as $t) {
|
||||
if ($this->typeMatches($t, $value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($type instanceof \ReflectionIntersectionType) {
|
||||
foreach ($type->getTypes() as $t) {
|
||||
if (!$this->typeMatches($t, $value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($type instanceof \ReflectionNamedType) {
|
||||
$typeName = $type->getName();
|
||||
$valueType = gettype($value);
|
||||
if ($valueType === 'object') {
|
||||
return is_subclass_of($value, $typeName);
|
||||
}
|
||||
return $valueType === $typeName;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user