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

Skip to content

Commit a8f8903

Browse files
[DependencyInjection] Stop considering empty env vars as populated
1 parent e9870a4 commit a8f8903

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,16 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
145145

146146
if (false !== $i || 'string' !== $prefix) {
147147
$env = $getEnv($name);
148-
} elseif (isset($_ENV[$name])) {
149-
$env = $_ENV[$name];
150-
} elseif (isset($_SERVER[$name]) && !str_starts_with($name, 'HTTP_')) {
151-
$env = $_SERVER[$name];
152-
} elseif (false === ($env = getenv($name)) || null === $env) { // null is a possible value because of thread safety issues
148+
} elseif ('' === ($env = $_ENV[$name] ?? (str_starts_with($name, 'HTTP_') ? null : ($_SERVER[$name] ?? null)))
149+
|| false === ($env = $env ?? getenv($name) ?? false) // null is a possible value because of thread safety issues
150+
) {
153151
foreach ($this->loadedVars as $vars) {
154-
if (false !== $env = ($vars[$name] ?? false)) {
152+
if (false !== ($env = ($vars[$name] ?? false)) && '' !== $env) {
155153
break;
156154
}
157155
}
158156

159-
if (false === $env || null === $env) {
157+
if (false === $env || '' === $env) {
160158
$loaders = $this->loaders;
161159
$this->loaders = new \ArrayIterator();
162160

@@ -169,7 +167,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
169167
continue;
170168
}
171169
$this->loadedVars[] = $vars = $loader->loadEnvVars();
172-
if (false !== $env = $vars[$name] ?? false) {
170+
if (false !== ($env = ($vars[$name] ?? false)) && '' !== $env) {
173171
$ended = false;
174172
break;
175173
}
@@ -184,7 +182,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
184182
}
185183
}
186184

187-
if (false === $env || null === $env) {
185+
if (false === $env) {
188186
if (!$this->container->hasParameter("env($name)")) {
189187
throw new EnvNotFoundException(sprintf('Environment variable not found: "%s".', $name));
190188
}
@@ -218,7 +216,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
218216
if (\in_array($prefix, ['bool', 'not'], true)) {
219217
$env = (bool) (filter_var($env, \FILTER_VALIDATE_BOOL) ?: filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT));
220218

221-
return 'not' === $prefix ? !$env : $env;
219+
return 'not' === $prefix xor $env;
222220
}
223221

224222
if ('int' === $prefix) {

src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,12 +744,15 @@ public function validCsv()
744744

745745
public function testEnvLoader()
746746
{
747+
$_ENV['BAZ_ENV_LOADER'] = '';
748+
747749
$loaders = function () {
748750
yield new class() implements EnvVarLoaderInterface {
749751
public function loadEnvVars(): array
750752
{
751753
return [
752754
'FOO_ENV_LOADER' => '123',
755+
'BAZ_ENV_LOADER' => '',
753756
];
754757
}
755758
};
@@ -760,6 +763,7 @@ public function loadEnvVars(): array
760763
return [
761764
'FOO_ENV_LOADER' => '234',
762765
'BAR_ENV_LOADER' => '456',
766+
'BAZ_ENV_LOADER' => '567',
763767
];
764768
}
765769
};
@@ -773,8 +777,13 @@ public function loadEnvVars(): array
773777
$result = $processor->getEnv('string', 'BAR_ENV_LOADER', function () {});
774778
$this->assertSame('456', $result);
775779

780+
$result = $processor->getEnv('string', 'BAZ_ENV_LOADER', function () {});
781+
$this->assertSame('567', $result);
782+
776783
$result = $processor->getEnv('string', 'FOO_ENV_LOADER', function () {});
777784
$this->assertSame('123', $result); // check twice
785+
786+
unset($_ENV['BAZ_ENV_LOADER']);
778787
}
779788

780789
public function testCircularEnvLoader()

0 commit comments

Comments
 (0)