mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-28 13:08:45 +02:00
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:
@@ -20,12 +20,13 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
||||
*/
|
||||
class EnvVarProcessor implements EnvVarProcessorInterface
|
||||
{
|
||||
private $container;
|
||||
private $loaders;
|
||||
private $loadedVars = [];
|
||||
private ContainerInterface $container;
|
||||
/** @var \Traversable<EnvVarLoaderInterface> */
|
||||
private \Traversable $loaders;
|
||||
private array $loadedVars = [];
|
||||
|
||||
/**
|
||||
* @param EnvVarLoaderInterface[] $loaders
|
||||
* @param \Traversable<EnvVarLoaderInterface>|null $loaders
|
||||
*/
|
||||
public function __construct(ContainerInterface $container, \Traversable $loaders = null)
|
||||
{
|
||||
@@ -33,10 +34,7 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
||||
$this->loaders = $loaders ?? new \ArrayIterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getProvidedTypes()
|
||||
public static function getProvidedTypes(): array
|
||||
{
|
||||
return [
|
||||
'base64' => 'string',
|
||||
@@ -56,13 +54,13 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
||||
'string' => 'string',
|
||||
'trim' => 'string',
|
||||
'require' => 'bool|int|float|string|array',
|
||||
'enum' => \BackedEnum::class,
|
||||
'shuffle' => 'array',
|
||||
'defined' => 'bool',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getEnv(string $prefix, string $name, \Closure $getEnv)
|
||||
public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
|
||||
{
|
||||
$i = strpos($name, ':');
|
||||
|
||||
@@ -86,6 +84,34 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
||||
return $array[$key];
|
||||
}
|
||||
|
||||
if ('enum' === $prefix) {
|
||||
if (false === $i) {
|
||||
throw new RuntimeException(sprintf('Invalid env "enum:%s": a "%s" class-string should be provided.', $name, \BackedEnum::class));
|
||||
}
|
||||
|
||||
$next = substr($name, $i + 1);
|
||||
$backedEnumClassName = substr($name, 0, $i);
|
||||
$backedEnumValue = $getEnv($next);
|
||||
|
||||
if (!\is_string($backedEnumValue) && !\is_int($backedEnumValue)) {
|
||||
throw new RuntimeException(sprintf('Resolved value of "%s" did not result in a string or int value.', $next));
|
||||
}
|
||||
|
||||
if (!is_subclass_of($backedEnumClassName, \BackedEnum::class)) {
|
||||
throw new RuntimeException(sprintf('"%s" is not a "%s".', $backedEnumClassName, \BackedEnum::class));
|
||||
}
|
||||
|
||||
return $backedEnumClassName::tryFrom($backedEnumValue) ?? throw new RuntimeException(sprintf('Enum value "%s" is not backed by "%s".', $backedEnumValue, $backedEnumClassName));
|
||||
}
|
||||
|
||||
if ('defined' === $prefix) {
|
||||
try {
|
||||
return '' !== ($getEnv($name) ?? '');
|
||||
} catch (EnvNotFoundException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ('default' === $prefix) {
|
||||
if (false === $i) {
|
||||
throw new RuntimeException(sprintf('Invalid env "default:%s": a fallback parameter should be provided.', $name));
|
||||
@@ -104,7 +130,7 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
||||
if ('' !== $env && null !== $env) {
|
||||
return $env;
|
||||
}
|
||||
} catch (EnvNotFoundException $e) {
|
||||
} catch (EnvNotFoundException) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
@@ -126,20 +152,27 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
||||
}
|
||||
}
|
||||
|
||||
$returnNull = false;
|
||||
if ('' === $prefix) {
|
||||
if ('' === $name) {
|
||||
return null;
|
||||
}
|
||||
$returnNull = true;
|
||||
$prefix = 'string';
|
||||
}
|
||||
|
||||
if (false !== $i || 'string' !== $prefix) {
|
||||
$env = $getEnv($name);
|
||||
} elseif (isset($_ENV[$name])) {
|
||||
$env = $_ENV[$name];
|
||||
} elseif (isset($_SERVER[$name]) && !str_starts_with($name, 'HTTP_')) {
|
||||
$env = $_SERVER[$name];
|
||||
} elseif (false === ($env = getenv($name)) || null === $env) { // null is a possible value because of thread safety issues
|
||||
} elseif ('' === ($env = $_ENV[$name] ?? (str_starts_with($name, 'HTTP_') ? null : ($_SERVER[$name] ?? null)))
|
||||
|| (false !== $env && false === ($env = $env ?? getenv($name) ?? false)) // null is a possible value because of thread safety issues
|
||||
) {
|
||||
foreach ($this->loadedVars as $vars) {
|
||||
if (false !== $env = ($vars[$name] ?? false)) {
|
||||
if (false !== ($env = ($vars[$name] ?? $env)) && '' !== $env) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (false === $env || null === $env) {
|
||||
if (false === $env || '' === $env) {
|
||||
$loaders = $this->loaders;
|
||||
$this->loaders = new \ArrayIterator();
|
||||
|
||||
@@ -152,7 +185,7 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
||||
continue;
|
||||
}
|
||||
$this->loadedVars[] = $vars = $loader->loadEnvVars();
|
||||
if (false !== $env = $vars[$name] ?? false) {
|
||||
if (false !== ($env = ($vars[$name] ?? $env)) && '' !== $env) {
|
||||
$ended = false;
|
||||
break;
|
||||
}
|
||||
@@ -160,14 +193,14 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
||||
if ($ended || $count === $i) {
|
||||
$loaders = $this->loaders;
|
||||
}
|
||||
} catch (ParameterCircularReferenceException $e) {
|
||||
} catch (ParameterCircularReferenceException) {
|
||||
// skip loaders that need an env var that is not defined
|
||||
} finally {
|
||||
$this->loaders = $loaders;
|
||||
}
|
||||
}
|
||||
|
||||
if (false === $env || null === $env) {
|
||||
if (false === $env) {
|
||||
if (!$this->container->hasParameter("env($name)")) {
|
||||
throw new EnvNotFoundException(sprintf('Environment variable not found: "%s".', $name));
|
||||
}
|
||||
@@ -177,14 +210,26 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
||||
}
|
||||
|
||||
if (null === $env) {
|
||||
if ($returnNull) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!isset($this->getProvidedTypes()[$prefix])) {
|
||||
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
|
||||
}
|
||||
|
||||
return null;
|
||||
if (!\in_array($prefix, ['string', 'bool', 'not', 'int', 'float'], true)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (!\is_scalar($env)) {
|
||||
if ('shuffle' === $prefix) {
|
||||
\is_array($env) ? shuffle($env) : throw new RuntimeException(sprintf('Env var "%s" cannot be shuffled, expected array, got "%s".', $name, get_debug_type($env)));
|
||||
|
||||
return $env;
|
||||
}
|
||||
|
||||
if (null !== $env && !\is_scalar($env)) {
|
||||
throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to "%s".', $name, $prefix));
|
||||
}
|
||||
|
||||
@@ -193,13 +238,13 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
||||
}
|
||||
|
||||
if (\in_array($prefix, ['bool', 'not'], true)) {
|
||||
$env = (bool) (filter_var($env, \FILTER_VALIDATE_BOOLEAN) ?: filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT));
|
||||
$env = (bool) (filter_var($env, \FILTER_VALIDATE_BOOL) ?: filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT));
|
||||
|
||||
return 'not' === $prefix ? !$env : $env;
|
||||
return 'not' === $prefix xor $env;
|
||||
}
|
||||
|
||||
if ('int' === $prefix) {
|
||||
if (false === $env = filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT)) {
|
||||
if (null !== $env && false === $env = filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT)) {
|
||||
throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to int.', $name));
|
||||
}
|
||||
|
||||
@@ -207,7 +252,7 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
||||
}
|
||||
|
||||
if ('float' === $prefix) {
|
||||
if (false === $env = filter_var($env, \FILTER_VALIDATE_FLOAT)) {
|
||||
if (null !== $env && false === $env = filter_var($env, \FILTER_VALIDATE_FLOAT)) {
|
||||
throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to float.', $name));
|
||||
}
|
||||
|
||||
@@ -292,7 +337,7 @@ class EnvVarProcessor implements EnvVarProcessorInterface
|
||||
}
|
||||
|
||||
if ('csv' === $prefix) {
|
||||
return str_getcsv($env, ',', '"', \PHP_VERSION_ID >= 70400 ? '' : '\\');
|
||||
return '' === $env ? [] : str_getcsv($env, ',', '"', '');
|
||||
}
|
||||
|
||||
if ('trim' === $prefix) {
|
||||
|
||||
Reference in New Issue
Block a user