N°6934 - Symfony 6.4 - upgrade Symfony bundles to 6.4 (#580)

* Update Symfony lib to version ~6.4.0
* Update code missing return type
* Add an iTop general configuration entry to store application secret (Symfony mandatory parameter)
* Use dependency injection in ExceptionListener & UserProvider classes
This commit is contained in:
bdalsass
2023-12-05 13:56:56 +01:00
committed by GitHub
parent 863ab4560c
commit 27ce51ab07
1392 changed files with 44869 additions and 27799 deletions

View File

@@ -17,12 +17,16 @@ use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Config\Resource\GlobResource;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\DependencyInjection\Attribute\Exclude;
use Symfony\Component\DependencyInjection\Attribute\When;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\RegisterAutoconfigureAttributesPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
/**
* FileLoader is the abstract class used by all built-in loaders that are file based.
@@ -38,6 +42,8 @@ abstract class FileLoader extends BaseFileLoader
protected $instanceof = [];
protected $interfaces = [];
protected $singlyImplemented = [];
/** @var array<string, Alias> */
protected $aliases = [];
protected $autoRegisterAliasesForSinglyImplementedInterfaces = true;
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator, string $env = null)
@@ -48,11 +54,9 @@ abstract class FileLoader extends BaseFileLoader
}
/**
* {@inheritdoc}
*
* @param bool|string $ignoreErrors Whether errors should be ignored; pass "not_found" to ignore only when the loaded resource is not found
*/
public function import($resource, string $type = null, $ignoreErrors = false, string $sourceResource = null, $exclude = null)
public function import(mixed $resource, string $type = null, bool|string $ignoreErrors = false, string $sourceResource = null, $exclude = null): mixed
{
$args = \func_get_args();
@@ -90,8 +94,11 @@ abstract class FileLoader extends BaseFileLoader
* @param string $namespace The namespace prefix of classes in the scanned directory
* @param string $resource The directory to look for classes, glob-patterns allowed
* @param string|string[]|null $exclude A globbed path of files to exclude or an array of globbed paths of files to exclude
* @param string|null $source The path to the file that defines the auto-discovery rule
*
* @return void
*/
public function registerClasses(Definition $prototype, string $namespace, string $resource, $exclude = null)
public function registerClasses(Definition $prototype, string $namespace, string $resource, string|array $exclude = null/* , string $source = null */)
{
if (!str_ends_with($namespace, '\\')) {
throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": "%s".', $namespace));
@@ -99,66 +106,138 @@ abstract class FileLoader extends BaseFileLoader
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) {
throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: "%s".', $namespace));
}
// This can happen with YAML files
if (\is_array($exclude) && \in_array(null, $exclude, true)) {
throw new InvalidArgumentException('The exclude list must not contain a "null" value.');
}
// This can happen with XML files
if (\is_array($exclude) && \in_array('', $exclude, true)) {
throw new InvalidArgumentException('The exclude list must not contain an empty value.');
}
$source = \func_num_args() > 4 ? func_get_arg(4) : null;
$autoconfigureAttributes = new RegisterAutoconfigureAttributesPass();
$autoconfigureAttributes = $autoconfigureAttributes->accept($prototype) ? $autoconfigureAttributes : null;
$classes = $this->findClasses($namespace, $resource, (array) $exclude, $autoconfigureAttributes);
// prepare for deep cloning
$serializedPrototype = serialize($prototype);
$classes = $this->findClasses($namespace, $resource, (array) $exclude, $autoconfigureAttributes, $source);
$getPrototype = static fn () => clone $prototype;
$serialized = serialize($prototype);
// avoid deep cloning if no definitions are nested
if (strpos($serialized, 'O:48:"Symfony\Component\DependencyInjection\Definition"', 55)
|| strpos($serialized, 'O:53:"Symfony\Component\DependencyInjection\ChildDefinition"', 55)
) {
// prepare for deep cloning
foreach (['Arguments', 'Properties', 'MethodCalls', 'Configurator', 'Factory', 'Bindings'] as $key) {
$serialized = serialize($prototype->{'get'.$key}());
if (strpos($serialized, 'O:48:"Symfony\Component\DependencyInjection\Definition"')
|| strpos($serialized, 'O:53:"Symfony\Component\DependencyInjection\ChildDefinition"')
) {
$getPrototype = static fn () => $getPrototype()->{'set'.$key}(unserialize($serialized));
}
}
}
unset($serialized);
foreach ($classes as $class => $errorMessage) {
if (null === $errorMessage && $autoconfigureAttributes && $this->env) {
if (null === $errorMessage && $autoconfigureAttributes) {
$r = $this->container->getReflectionClass($class);
$attribute = null;
foreach ($r->getAttributes(When::class) as $attribute) {
if ($this->env === $attribute->newInstance()->env) {
$attribute = null;
break;
}
}
if (null !== $attribute) {
if ($r->getAttributes(Exclude::class)[0] ?? null) {
$this->addContainerExcludedTag($class, $source);
continue;
}
if ($this->env) {
$attribute = null;
foreach ($r->getAttributes(When::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
if ($this->env === $attribute->newInstance()->env) {
$attribute = null;
break;
}
}
if (null !== $attribute) {
$this->addContainerExcludedTag($class, $source);
continue;
}
}
}
if (interface_exists($class, false)) {
$this->interfaces[] = $class;
} else {
$this->setDefinition($class, $definition = unserialize($serializedPrototype));
$this->setDefinition($class, $definition = $getPrototype());
if (null !== $errorMessage) {
$definition->addError($errorMessage);
continue;
}
$definition->setClass($class);
$interfaces = [];
foreach (class_implements($class, false) as $interface) {
$this->singlyImplemented[$interface] = ($this->singlyImplemented[$interface] ?? $class) !== $class ? false : $class;
$interfaces[] = $interface;
}
if (!$autoconfigureAttributes) {
continue;
}
$r = $this->container->getReflectionClass($class);
$defaultAlias = 1 === \count($interfaces) ? $interfaces[0] : null;
foreach ($r->getAttributes(AsAlias::class) as $attr) {
/** @var AsAlias $attribute */
$attribute = $attr->newInstance();
$alias = $attribute->id ?? $defaultAlias;
$public = $attribute->public;
if (null === $alias) {
throw new LogicException(sprintf('Alias cannot be automatically determined for class "%s". If you have used the #[AsAlias] attribute with a class implementing multiple interfaces, add the interface you want to alias to the first parameter of #[AsAlias].', $class));
}
if (isset($this->aliases[$alias])) {
throw new LogicException(sprintf('The "%s" alias has already been defined with the #[AsAlias] attribute in "%s".', $alias, $this->aliases[$alias]));
}
$this->aliases[$alias] = new Alias($class, $public);
}
}
}
foreach ($this->aliases as $alias => $aliasDefinition) {
$this->container->setAlias($alias, $aliasDefinition);
}
if ($this->autoRegisterAliasesForSinglyImplementedInterfaces) {
$this->registerAliasesForSinglyImplementedInterfaces();
}
}
/**
* @return void
*/
public function registerAliasesForSinglyImplementedInterfaces()
{
foreach ($this->interfaces as $interface) {
if (!empty($this->singlyImplemented[$interface]) && !$this->container->has($interface)) {
if (!empty($this->singlyImplemented[$interface]) && !isset($this->aliases[$interface]) && !$this->container->has($interface)) {
$this->container->setAlias($interface, $this->singlyImplemented[$interface]);
}
}
$this->interfaces = $this->singlyImplemented = [];
$this->interfaces = $this->singlyImplemented = $this->aliases = [];
}
/**
* Registers a definition in the container with its instanceof-conditionals.
*
* @return void
*/
protected function setDefinition(string $id, Definition $definition)
{
$this->container->removeBindings($id);
foreach ($definition->getTag('container.error') as $error) {
if (isset($error['message'])) {
$definition->addError($error['message']);
}
}
if ($this->isLoadingInstanceof) {
if (!$definition instanceof ChildDefinition) {
throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, get_debug_type($definition)));
@@ -169,7 +248,7 @@ abstract class FileLoader extends BaseFileLoader
}
}
private function findClasses(string $namespace, string $pattern, array $excludePatterns, ?RegisterAutoconfigureAttributesPass $autoconfigureAttributes): array
private function findClasses(string $namespace, string $pattern, array $excludePatterns, ?RegisterAutoconfigureAttributesPass $autoconfigureAttributes, ?string $source): array
{
$parameterBag = $this->container->getParameterBag();
@@ -178,9 +257,7 @@ abstract class FileLoader extends BaseFileLoader
$excludePatterns = $parameterBag->unescapeValue($parameterBag->resolveValue($excludePatterns));
foreach ($excludePatterns as $excludePattern) {
foreach ($this->glob($excludePattern, true, $resource, true, true) as $path => $info) {
if (null === $excludePrefix) {
$excludePrefix = $resource->getPrefix();
}
$excludePrefix ??= $resource->getPrefix();
// normalize Windows slashes and remove trailing slashes
$excludePaths[rtrim(str_replace('\\', '/', $path), '/')] = true;
@@ -189,7 +266,6 @@ abstract class FileLoader extends BaseFileLoader
$pattern = $parameterBag->unescapeValue($parameterBag->resolveValue($pattern));
$classes = [];
$extRegexp = '/\\.php$/';
$prefixLen = null;
foreach ($this->glob($pattern, true, $resource, false, false, $excludePaths) as $path => $info) {
if (null === $prefixLen) {
@@ -204,10 +280,10 @@ abstract class FileLoader extends BaseFileLoader
continue;
}
if (!preg_match($extRegexp, $path, $m) || !$info->isReadable()) {
if (!str_ends_with($path, '.php')) {
continue;
}
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -\strlen($m[0]))), '\\');
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -4)), '\\');
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $class)) {
continue;
@@ -242,6 +318,30 @@ abstract class FileLoader extends BaseFileLoader
}
}
if (null !== $prefixLen) {
foreach ($excludePaths as $path => $_) {
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, str_ends_with($path, '.php') ? -4 : null)), '\\');
$this->addContainerExcludedTag($class, $source);
}
}
return $classes;
}
private function addContainerExcludedTag(string $class, ?string $source): void
{
if ($this->container->has($class)) {
return;
}
static $attributes = [];
if (null !== $source && !isset($attributes[$source])) {
$attributes[$source] = ['source' => sprintf('in "%s/%s"', basename(\dirname($source)), basename($source))];
}
$this->container->register($class, $class)
->setAbstract(true)
->addTag('container.excluded', null !== $source ? $attributes[$source] : []);
}
}