Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Fix support for empty env vars #50101

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
Apr 21, 2023
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
4 changes: 2 additions & 2 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
|| 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)) && '' !== $env) {
if (false !== ($env = ($vars[$name] ?? $env)) && '' !== $env) {
break;
}
}
Expand All @@ -167,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)) && '' !== $env) {
if (false !== ($env = ($vars[$name] ?? $env)) && '' !== $env) {
$ended = false;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ public static function validCsv()
public function testEnvLoader()
{
$_ENV['BAZ_ENV_LOADER'] = '';
$_ENV['BUZ_ENV_LOADER'] = '';

$loaders = function () {
yield new class() implements EnvVarLoaderInterface {
Expand All @@ -751,7 +752,7 @@ public function loadEnvVars(): array
};
};

$processor = new EnvVarProcessor(new Container(), $loaders());
$processor = new EnvVarProcessor(new Container(), new RewindableGenerator($loaders, 2));

$result = $processor->getEnv('string', 'FOO_ENV_LOADER', function () {});
$this->assertSame('123', $result);
Expand All @@ -762,10 +763,14 @@ public function loadEnvVars(): array
$result = $processor->getEnv('string', 'BAZ_ENV_LOADER', function () {});
$this->assertSame('567', $result);

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

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

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

public function testCircularEnvLoader()
Expand Down