Skip to content

[DI] Fix unresolved env parameters values #20695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,7 @@ protected function getEnv($name)
if (isset($this->envCache[$name]) || array_key_exists($name, $this->envCache)) {
return $this->envCache[$name];
}
if (isset($_ENV[$name])) {
return $this->envCache[$name] = $_ENV[$name];
}
if (false !== $env = getenv($name)) {
return $this->envCache[$name] = $env;
}

if (!$this->hasParameter("env($name)")) {
throw new EnvNotFoundException($name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ public function getExpressionLanguageProviders()
}

/**
* Resolves env parameter placeholders in a string.
* Resolves env parameter placeholders in a string or an array..
*
* @param string $string The string to resolve
* @param string|null $format A sprintf() format to use as replacement for env placeholders or null to use the default parameter format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\ParameterBag;

use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

/**
Expand All @@ -29,13 +30,12 @@ public function get($name)
if (0 === strpos($name, 'env(') && ')' === substr($name, -1) && 'env()' !== $name) {
$env = substr($name, 4, -1);

if (isset($this->envPlaceholders[$env])) {
foreach ($this->envPlaceholders[$env] as $placeholder) {
return $placeholder; // return first result
}
if (isset($_ENV[$env])) {
return $_ENV[$env];
}
if (preg_match('/\W/', $env)) {
throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));

if (false !== $value = getenv($env)) {
return $value;
}

if ($this->has($name)) {
Expand All @@ -44,13 +44,11 @@ public function 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));
}
}

$uniqueName = md5($name.uniqid(mt_rand(), true));
$placeholder = sprintf('env_%s_%s', $env, $uniqueName);
$this->envPlaceholders[$env][$placeholder] = $placeholder;
return $defaultValue;
}

return $placeholder;
throw new EnvNotFoundException($name);
}

return parent::get($name);
Expand All @@ -66,6 +64,43 @@ public function getEnvPlaceholders()
return $this->envPlaceholders;
}

/**
* Returns the map of env vars used in the resolved parameter values to their placeholders.
*
* @return string[][] A map of env var names to their placeholders
*/
public function getEnvPlaceholder($name)
{
if (0 !== strpos($name, 'env(') || ')' !== substr($name, -1) || 'env()' !== $name) {
return $name;
}

$env = substr($name, 4, -1);

if (isset($this->envPlaceholders[$env])) {
foreach ($this->envPlaceholders[$env] as $placeholder) {
return $placeholder; // return first result
}
}
if (preg_match('/\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));
}
}

$uniqueName = md5($name.uniqid(mt_rand(), true));
$placeholder = sprintf('env_%s_%s', $env, $uniqueName);
$this->envPlaceholders[$env][$placeholder] = $placeholder;

return $placeholder;
}

/**
* Merges the env placeholders of another EnvPlaceholderParameterBag.
*/
Expand Down