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

@@ -16,11 +16,13 @@ use Symfony\Component\DependencyInjection\Argument\ServiceLocator as ArgumentSer
use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Contracts\Service\ResetInterface;
@@ -36,11 +38,12 @@ class_exists(ArgumentServiceLocator::class);
* The container can have four possible behaviors when a service
* does not exist (or is not initialized for the last case):
*
* * EXCEPTION_ON_INVALID_REFERENCE: Throws an exception (the default)
* * EXCEPTION_ON_INVALID_REFERENCE: Throws an exception at compilation time (the default)
* * NULL_ON_INVALID_REFERENCE: Returns null
* * IGNORE_ON_INVALID_REFERENCE: Ignores the wrapping command asking for the reference
* (for instance, ignore a setter if the service does not exist)
* * IGNORE_ON_UNINITIALIZED_REFERENCE: Ignores/returns null for uninitialized services or invalid references
* * RUNTIME_EXCEPTION_ON_INVALID_REFERENCE: Throws an exception at runtime
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
@@ -58,9 +61,11 @@ class Container implements ContainerInterface, ResetInterface
protected $resolving = [];
protected $syntheticIds = [];
private $envCache = [];
private $compiled = false;
private $getEnv;
private array $envCache = [];
private bool $compiled = false;
private \Closure $getEnv;
private static \Closure $make;
public function __construct(ParameterBagInterface $parameterBag = null)
{
@@ -74,32 +79,33 @@ class Container implements ContainerInterface, ResetInterface
*
* * Parameter values are resolved;
* * The parameter bag is frozen.
*
* @return void
*/
public function compile()
{
$this->parameterBag->resolve();
$this->parameterBag = new FrozenParameterBag($this->parameterBag->all());
$this->parameterBag = new FrozenParameterBag(
$this->parameterBag->all(),
$this->parameterBag instanceof ParameterBag ? $this->parameterBag->allDeprecated() : []
);
$this->compiled = true;
}
/**
* Returns true if the container is compiled.
*
* @return bool
*/
public function isCompiled()
public function isCompiled(): bool
{
return $this->compiled;
}
/**
* Gets the service container parameter bag.
*
* @return ParameterBagInterface
*/
public function getParameterBag()
public function getParameterBag(): ParameterBagInterface
{
return $this->parameterBag;
}
@@ -109,28 +115,22 @@ class Container implements ContainerInterface, ResetInterface
*
* @return array|bool|string|int|float|\UnitEnum|null
*
* @throws InvalidArgumentException if the parameter is not defined
* @throws ParameterNotFoundException if the parameter is not defined
*/
public function getParameter(string $name)
{
return $this->parameterBag->get($name);
}
/**
* @return bool
*/
public function hasParameter(string $name)
public function hasParameter(string $name): bool
{
return $this->parameterBag->has($name);
}
/**
* Sets a parameter.
*
* @param string $name The parameter name
* @param array|bool|string|int|float|\UnitEnum|null $value The parameter value
* @return void
*/
public function setParameter(string $name, $value)
public function setParameter(string $name, array|bool|string|int|float|\UnitEnum|null $value)
{
$this->parameterBag->set($name, $value);
}
@@ -140,6 +140,8 @@ class Container implements ContainerInterface, ResetInterface
*
* Setting a synthetic service to null resets it: has() returns false and get()
* behaves in the same way as if the service was never created.
*
* @return void
*/
public function set(string $id, ?object $service)
{
@@ -147,7 +149,7 @@ class Container implements ContainerInterface, ResetInterface
if (isset($this->privates['service_container']) && $this->privates['service_container'] instanceof \Closure) {
$initialize = $this->privates['service_container'];
unset($this->privates['service_container']);
$initialize();
$initialize($this);
}
if ('service_container' === $id) {
@@ -179,14 +181,7 @@ class Container implements ContainerInterface, ResetInterface
$this->services[$id] = $service;
}
/**
* Returns true if the given service is defined.
*
* @param string $id The service identifier
*
* @return bool
*/
public function has(string $id)
public function has(string $id): bool
{
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
@@ -204,19 +199,16 @@ class Container implements ContainerInterface, ResetInterface
/**
* Gets a service.
*
* @return object|null
*
* @throws ServiceCircularReferenceException When a circular reference is detected
* @throws ServiceNotFoundException When the service is not defined
* @throws \Exception if an exception has been thrown when the service has been resolved
*
* @see Reference
*/
public function get(string $id, int $invalidBehavior = /* self::EXCEPTION_ON_INVALID_REFERENCE */ 1)
public function get(string $id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE): ?object
{
return $this->services[$id]
?? $this->services[$id = $this->aliases[$id] ?? $id]
?? ('service_container' === $id ? $this : ($this->factories[$id] ?? [$this, 'make'])($id, $invalidBehavior));
?? ('service_container' === $id ? $this : ($this->factories[$id] ?? self::$make ??= self::make(...))($this, $id, $invalidBehavior));
}
/**
@@ -224,41 +216,41 @@ class Container implements ContainerInterface, ResetInterface
*
* As a separate method to allow "get()" to use the really fast `??` operator.
*/
private function make(string $id, int $invalidBehavior)
private static function make(self $container, string $id, int $invalidBehavior): ?object
{
if (isset($this->loading[$id])) {
throw new ServiceCircularReferenceException($id, array_merge(array_keys($this->loading), [$id]));
if (isset($container->loading[$id])) {
throw new ServiceCircularReferenceException($id, array_merge(array_keys($container->loading), [$id]));
}
$this->loading[$id] = true;
$container->loading[$id] = true;
try {
if (isset($this->fileMap[$id])) {
return /* self::IGNORE_ON_UNINITIALIZED_REFERENCE */ 4 === $invalidBehavior ? null : $this->load($this->fileMap[$id]);
} elseif (isset($this->methodMap[$id])) {
return /* self::IGNORE_ON_UNINITIALIZED_REFERENCE */ 4 === $invalidBehavior ? null : $this->{$this->methodMap[$id]}();
if (isset($container->fileMap[$id])) {
return /* self::IGNORE_ON_UNINITIALIZED_REFERENCE */ 4 === $invalidBehavior ? null : $container->load($container->fileMap[$id]);
} elseif (isset($container->methodMap[$id])) {
return /* self::IGNORE_ON_UNINITIALIZED_REFERENCE */ 4 === $invalidBehavior ? null : $container->{$container->methodMap[$id]}($container);
}
} catch (\Exception $e) {
unset($this->services[$id]);
unset($container->services[$id]);
throw $e;
} finally {
unset($this->loading[$id]);
unset($container->loading[$id]);
}
if (/* self::EXCEPTION_ON_INVALID_REFERENCE */ 1 === $invalidBehavior) {
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
if (!$id) {
throw new ServiceNotFoundException($id);
}
if (isset($this->syntheticIds[$id])) {
if (isset($container->syntheticIds[$id])) {
throw new ServiceNotFoundException($id, null, null, [], sprintf('The "%s" service is synthetic, it needs to be set at boot time before it can be used.', $id));
}
if (isset($this->getRemovedIds()[$id])) {
if (isset($container->getRemovedIds()[$id])) {
throw new ServiceNotFoundException($id, null, null, [], sprintf('The "%s" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.', $id));
}
$alternatives = [];
foreach ($this->getServiceIds() as $knownId) {
foreach ($container->getServiceIds() as $knownId) {
if ('' === $knownId || '.' === $knownId[0]) {
continue;
}
@@ -276,10 +268,8 @@ class Container implements ContainerInterface, ResetInterface
/**
* Returns true if the given service has actually been initialized.
*
* @return bool
*/
public function initialized(string $id)
public function initialized(string $id): bool
{
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
@@ -293,7 +283,7 @@ class Container implements ContainerInterface, ResetInterface
}
/**
* {@inheritdoc}
* @return void
*/
public function reset()
{
@@ -305,7 +295,7 @@ class Container implements ContainerInterface, ResetInterface
if ($service instanceof ResetInterface) {
$service->reset();
}
} catch (\Throwable $e) {
} catch (\Throwable) {
continue;
}
}
@@ -316,43 +306,39 @@ class Container implements ContainerInterface, ResetInterface
*
* @return string[]
*/
public function getServiceIds()
public function getServiceIds(): array
{
return array_map('strval', array_unique(array_merge(['service_container'], array_keys($this->fileMap), array_keys($this->methodMap), array_keys($this->aliases), array_keys($this->services))));
}
/**
* Gets service ids that existed at compile time.
*
* @return array
*/
public function getRemovedIds()
public function getRemovedIds(): array
{
return [];
}
/**
* Camelizes a string.
*
* @return string
*/
public static function camelize(string $id)
public static function camelize(string $id): string
{
return strtr(ucwords(strtr($id, ['_' => ' ', '.' => '_ ', '\\' => '_ '])), [' ' => '']);
}
/**
* A string to underscore.
*
* @return string
*/
public static function underscore(string $id)
public static function underscore(string $id): string
{
return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], ['\\1_\\2', '\\1_\\2'], str_replace('_', '.', $id)));
}
/**
* Creates a service by requiring its factory file.
*
* @return mixed
*/
protected function load(string $file)
{
@@ -362,11 +348,9 @@ class Container implements ContainerInterface, ResetInterface
/**
* Fetches a variable from the environment.
*
* @return mixed
*
* @throws EnvNotFoundException When the environment variable is not found and has no default value
*/
protected function getEnv(string $name)
protected function getEnv(string $name): mixed
{
if (isset($this->resolving[$envName = "env($name)"])) {
throw new ParameterCircularReferenceException(array_keys($this->resolving));
@@ -377,9 +361,7 @@ class Container implements ContainerInterface, ResetInterface
if (!$this->has($id = 'container.env_var_processors_locator')) {
$this->set($id, new ServiceLocator([]));
}
if (!$this->getEnv) {
$this->getEnv = \Closure::fromCallable([$this, 'getEnv']);
}
$this->getEnv ??= $this->getEnv(...);
$processors = $this->get($id);
if (false !== $i = strpos($name, ':')) {
@@ -389,7 +371,11 @@ class Container implements ContainerInterface, ResetInterface
$prefix = 'string';
$localName = $name;
}
$processor = $processors->has($prefix) ? $processors->get($prefix) : new EnvVarProcessor($this);
if (false === $i) {
$prefix = '';
}
$this->resolving[$envName] = true;
try {
@@ -400,14 +386,9 @@ class Container implements ContainerInterface, ResetInterface
}
/**
* @param string|false $registry
* @param string|bool $load
*
* @return mixed
*
* @internal
*/
final protected function getService($registry, string $id, ?string $method, $load)
final protected function getService(string|false $registry, string $id, ?string $method, string|bool $load): mixed
{
if ('service_container' === $id) {
return $this;
@@ -419,13 +400,13 @@ class Container implements ContainerInterface, ResetInterface
return false !== $registry ? $this->{$registry}[$id] ?? null : null;
}
if (false !== $registry) {
return $this->{$registry}[$id] ?? $this->{$registry}[$id] = $load ? $this->load($method) : $this->{$method}();
return $this->{$registry}[$id] ??= $load ? $this->load($method) : $this->{$method}($this);
}
if (!$load) {
return $this->{$method}();
return $this->{$method}($this);
}
return ($factory = $this->factories[$id] ?? $this->factories['service_container'][$id] ?? null) ? $factory() : $this->load($method);
return ($factory = $this->factories[$id] ?? $this->factories['service_container'][$id] ?? null) ? $factory($this) : $this->load($method);
}
private function __clone()