mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-30 22:18:46 +02:00
migration symfony 5 4 (#300)
* symfony 5.4 (diff dev) * symfony 5.4 (working) * symfony 5.4 (update autoload) * symfony 5.4 (remove swiftmailer mailer implementation) * symfony 5.4 (php doc and split Global accessor class) ### Impacted packages: composer require php:">=7.2.5 <8.0.0" symfony/console:5.4.* symfony/dotenv:5.4.* symfony/framework-bundle:5.4.* symfony/twig-bundle:5.4.* symfony/yaml:5.4.* --update-with-dependencies composer require symfony/stopwatch:5.4.* symfony/web-profiler-bundle:5.4.* --dev --update-with-dependencies
This commit is contained in:
@@ -19,15 +19,19 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
||||
*/
|
||||
class EnvPlaceholderParameterBag extends ParameterBag
|
||||
{
|
||||
private $envPlaceholderUniquePrefix;
|
||||
private $envPlaceholders = [];
|
||||
private $unusedEnvPlaceholders = [];
|
||||
private $providedTypes = [];
|
||||
|
||||
private static $counter = 0;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($name)
|
||||
public function get(string $name)
|
||||
{
|
||||
if (0 === strpos($name, 'env(') && ')' === substr($name, -1) && 'env()' !== $name) {
|
||||
if (str_starts_with($name, 'env(') && str_ends_with($name, ')') && 'env()' !== $name) {
|
||||
$env = substr($name, 4, -1);
|
||||
|
||||
if (isset($this->envPlaceholders[$env])) {
|
||||
@@ -35,20 +39,20 @@ class EnvPlaceholderParameterBag extends ParameterBag
|
||||
return $placeholder; // return first result
|
||||
}
|
||||
}
|
||||
if (!preg_match('/^(?:\w++:)*+\w++$/', $env)) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid "%s" name: only "word" characters are allowed.', $name));
|
||||
}
|
||||
|
||||
if ($this->has($name)) {
|
||||
$defaultValue = parent::get($name);
|
||||
|
||||
if (null !== $defaultValue && !is_scalar($defaultValue)) {
|
||||
throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar or null, but "%s" given to "%s".', \gettype($defaultValue), $name));
|
||||
if (isset($this->unusedEnvPlaceholders[$env])) {
|
||||
foreach ($this->unusedEnvPlaceholders[$env] as $placeholder) {
|
||||
return $placeholder; // return first result
|
||||
}
|
||||
}
|
||||
if (!preg_match('/^(?:[-.\w]*+:)*+\w++$/', $env)) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
|
||||
}
|
||||
if ($this->has($name) && null !== ($defaultValue = parent::get($name)) && !\is_string($defaultValue)) {
|
||||
throw new RuntimeException(sprintf('The default value of an env() parameter must be a string or null, but "%s" given to "%s".', get_debug_type($defaultValue), $name));
|
||||
}
|
||||
|
||||
$uniqueName = md5($name.uniqid(mt_rand(), true));
|
||||
$placeholder = sprintf('env_%s_%s', str_replace(':', '_', $env), $uniqueName);
|
||||
$uniqueName = md5($name.'_'.self::$counter++);
|
||||
$placeholder = sprintf('%s_%s_%s', $this->getEnvPlaceholderUniquePrefix(), strtr($env, ':-.', '___'), $uniqueName);
|
||||
$this->envPlaceholders[$env][$placeholder] = $placeholder;
|
||||
|
||||
return $placeholder;
|
||||
@@ -57,6 +61,20 @@ class EnvPlaceholderParameterBag extends ParameterBag
|
||||
return parent::get($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the common env placeholder prefix for env vars created by this bag.
|
||||
*/
|
||||
public function getEnvPlaceholderUniquePrefix(): string
|
||||
{
|
||||
if (null === $this->envPlaceholderUniquePrefix) {
|
||||
$reproducibleEntropy = unserialize(serialize($this->parameters));
|
||||
array_walk_recursive($reproducibleEntropy, function (&$v) { $v = null; });
|
||||
$this->envPlaceholderUniquePrefix = 'env_'.substr(md5(serialize($reproducibleEntropy)), -16);
|
||||
}
|
||||
|
||||
return $this->envPlaceholderUniquePrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the map of env vars used in the resolved parameter values to their placeholders.
|
||||
*
|
||||
@@ -67,6 +85,16 @@ class EnvPlaceholderParameterBag extends ParameterBag
|
||||
return $this->envPlaceholders;
|
||||
}
|
||||
|
||||
public function getUnusedEnvPlaceholders(): array
|
||||
{
|
||||
return $this->unusedEnvPlaceholders;
|
||||
}
|
||||
|
||||
public function clearUnusedEnvPlaceholders()
|
||||
{
|
||||
$this->unusedEnvPlaceholders = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges the env placeholders of another EnvPlaceholderParameterBag.
|
||||
*/
|
||||
@@ -79,6 +107,14 @@ class EnvPlaceholderParameterBag extends ParameterBag
|
||||
$this->envPlaceholders[$env] += $placeholders;
|
||||
}
|
||||
}
|
||||
|
||||
if ($newUnusedPlaceholders = $bag->getUnusedEnvPlaceholders()) {
|
||||
$this->unusedEnvPlaceholders += $newUnusedPlaceholders;
|
||||
|
||||
foreach ($newUnusedPlaceholders as $env => $placeholders) {
|
||||
$this->unusedEnvPlaceholders[$env] += $placeholders;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,13 +146,8 @@ class EnvPlaceholderParameterBag extends ParameterBag
|
||||
parent::resolve();
|
||||
|
||||
foreach ($this->envPlaceholders as $env => $placeholders) {
|
||||
if (!$this->has($name = "env($env)")) {
|
||||
continue;
|
||||
}
|
||||
if (is_numeric($default = $this->parameters[$name])) {
|
||||
$this->parameters[$name] = (string) $default;
|
||||
} elseif (null !== $default && !is_scalar($default)) {
|
||||
throw new RuntimeException(sprintf('The default value of env parameter "%s" must be scalar or null, "%s" given.', $env, \gettype($default)));
|
||||
if ($this->has($name = "env($env)") && null !== ($default = $this->parameters[$name]) && !\is_string($default)) {
|
||||
throw new RuntimeException(sprintf('The default value of env parameter "%s" must be a string or null, "%s" given.', $env, get_debug_type($default)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user