Skip to content

Commit 87a65d8

Browse files
bug #50530 [DependencyInjection] Fix support for false boolean env vars (Okhoshi)
This PR was merged into the 6.3 branch. Discussion ---------- [DependencyInjection] Fix support for `false` boolean env vars | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - After upgrading `symfony/dependency-injection` package to version 6.3, some of our env vars couldn't be found anymore. For some scripts, we are providing an adhoc default value to env vars by setting `$_SERVER` directly. However, since version 6.3 of the DependencyInjection component, setting an env var to `false` (the boolean value, like in the snippet below) doesn't work anymore, and Symfony reports that the variable cannot be found. ```php $_SERVER['FOO'] = false; ``` It seems to be a side effect of the changes made in #48705. Commits ------- 5101d18 [DependencyInjection] Fix support for `false` boolean env vars
2 parents d9a8902 + 5101d18 commit 87a65d8

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
146146
if (false !== $i || 'string' !== $prefix) {
147147
$env = $getEnv($name);
148148
} 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
149+
|| (false !== $env && false === ($env = $env ?? getenv($name) ?? false)) // null is a possible value because of thread safety issues
150150
) {
151151
foreach ($this->loadedVars as $vars) {
152152
if (false !== ($env = ($vars[$name] ?? $env)) && '' !== $env) {

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

+62
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,67 @@ public static function validStrings()
5959
];
6060
}
6161

62+
/**
63+
* @dataProvider validRealEnvValues
64+
*/
65+
public function testGetEnvRealEnv($value, $processed)
66+
{
67+
$_ENV['FOO'] = $value;
68+
69+
$processor = new EnvVarProcessor(new Container());
70+
71+
$result = $processor->getEnv('string', 'FOO', function () {
72+
$this->fail('Should not be called');
73+
});
74+
75+
$this->assertSame($processed, $result);
76+
77+
unset($_ENV['FOO']);
78+
}
79+
80+
public static function validRealEnvValues()
81+
{
82+
return [
83+
['hello', 'hello'],
84+
[true, '1'],
85+
[false, ''],
86+
[1, '1'],
87+
[0, '0'],
88+
[1.1, '1.1'],
89+
[10, '10'],
90+
];
91+
}
92+
93+
public function testGetEnvRealEnvInvalid()
94+
{
95+
$_ENV['FOO'] = null;
96+
$this->expectException(EnvNotFoundException::class);
97+
$this->expectExceptionMessage('Environment variable not found: "FOO".');
98+
99+
$processor = new EnvVarProcessor(new Container());
100+
101+
$processor->getEnv('string', 'FOO', function () {
102+
$this->fail('Should not be called');
103+
});
104+
105+
unset($_ENV['FOO']);
106+
}
107+
108+
public function testGetEnvRealEnvNonScalar()
109+
{
110+
$_ENV['FOO'] = [];
111+
$this->expectException(RuntimeException::class);
112+
$this->expectExceptionMessage('Non-scalar env var "FOO" cannot be cast to "string".');
113+
114+
$processor = new EnvVarProcessor(new Container());
115+
116+
$processor->getEnv('string', 'FOO', function () {
117+
$this->fail('Should not be called');
118+
});
119+
120+
unset($_ENV['FOO']);
121+
}
122+
62123
/**
63124
* @dataProvider validBools
64125
*/
@@ -97,6 +158,7 @@ public static function validBools()
97158
['true', true],
98159
['false', false],
99160
['null', false],
161+
['', false],
100162
['1', true],
101163
['0', false],
102164
['1.1', true],

0 commit comments

Comments
 (0)