Skip to content

[DependencyInjection] Stop considering empty env vars as populated #48705

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

Merged
merged 1 commit into from
Dec 28, 2022
Merged
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
18 changes: 8 additions & 10 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,16 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed

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 = $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] ?? false)) && '' !== $env) {
break;
}
}

if (false === $env || null === $env) {
if (false === $env || '' === $env) {
$loaders = $this->loaders;
$this->loaders = new \ArrayIterator();

Expand All @@ -169,7 +167,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
continue;
}
$this->loadedVars[] = $vars = $loader->loadEnvVars();
if (false !== $env = $vars[$name] ?? false) {
if (false !== ($env = ($vars[$name] ?? false)) && '' !== $env) {
$ended = false;
break;
}
Expand All @@ -184,7 +182,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
}
}

if (false === $env || null === $env) {
if (false === $env) {
if (!$this->container->hasParameter("env($name)")) {
throw new EnvNotFoundException(sprintf('Environment variable not found: "%s".', $name));
}
Expand Down Expand Up @@ -218,7 +216,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
if (\in_array($prefix, ['bool', 'not'], true)) {
$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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,12 +744,15 @@ public function validCsv()

public function testEnvLoader()
{
$_ENV['BAZ_ENV_LOADER'] = '';

$loaders = function () {
yield new class() implements EnvVarLoaderInterface {
public function loadEnvVars(): array
{
return [
'FOO_ENV_LOADER' => '123',
'BAZ_ENV_LOADER' => '',
];
}
};
Expand All @@ -760,6 +763,7 @@ public function loadEnvVars(): array
return [
'FOO_ENV_LOADER' => '234',
'BAR_ENV_LOADER' => '456',
'BAZ_ENV_LOADER' => '567',
];
}
};
Expand All @@ -773,8 +777,13 @@ public function loadEnvVars(): array
$result = $processor->getEnv('string', 'BAR_ENV_LOADER', function () {});
$this->assertSame('456', $result);

$result = $processor->getEnv('string', 'BAZ_ENV_LOADER', function () {});
$this->assertSame('567', $result);

$result = $processor->getEnv('string', 'FOO_ENV_LOADER', function () {});
$this->assertSame('123', $result); // check twice

unset($_ENV['BAZ_ENV_LOADER']);
}

public function testCircularEnvLoader()
Expand Down