Skip to content

Commit

Permalink
bug #50530 [DependencyInjection] Fix support for false boolean env …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
nicolas-grekas committed Jun 9, 2023
2 parents d9a8902 + 5101d18 commit 87a65d8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Expand Up @@ -146,7 +146,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
if (false !== $i || 'string' !== $prefix) {
$env = $getEnv($name);
} 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
|| (false !== $env && 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] ?? $env)) && '' !== $env) {
Expand Down
Expand Up @@ -59,6 +59,67 @@ public static function validStrings()
];
}

/**
* @dataProvider validRealEnvValues
*/
public function testGetEnvRealEnv($value, $processed)
{
$_ENV['FOO'] = $value;

$processor = new EnvVarProcessor(new Container());

$result = $processor->getEnv('string', 'FOO', function () {
$this->fail('Should not be called');
});

$this->assertSame($processed, $result);

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

public static function validRealEnvValues()
{
return [
['hello', 'hello'],
[true, '1'],
[false, ''],
[1, '1'],
[0, '0'],
[1.1, '1.1'],
[10, '10'],
];
}

public function testGetEnvRealEnvInvalid()
{
$_ENV['FOO'] = null;
$this->expectException(EnvNotFoundException::class);
$this->expectExceptionMessage('Environment variable not found: "FOO".');

$processor = new EnvVarProcessor(new Container());

$processor->getEnv('string', 'FOO', function () {
$this->fail('Should not be called');
});

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

public function testGetEnvRealEnvNonScalar()
{
$_ENV['FOO'] = [];
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Non-scalar env var "FOO" cannot be cast to "string".');

$processor = new EnvVarProcessor(new Container());

$processor->getEnv('string', 'FOO', function () {
$this->fail('Should not be called');
});

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

/**
* @dataProvider validBools
*/
Expand Down Expand Up @@ -97,6 +158,7 @@ public static function validBools()
['true', true],
['false', false],
['null', false],
['', false],
['1', true],
['0', false],
['1.1', true],
Expand Down

0 comments on commit 87a65d8

Please sign in to comment.