Skip to content

Commit 091ac2b

Browse files
bug #50517 [DependencyInjection] Fix casting scalar env vars from null (fancyweb)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Fix casting scalar env vars from null | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | - | Tickets | #50415 | License | MIT | Doc PR | - Using a casting env var processor on `null` returns `null` (it doesn't cast) which is not what one could expect and not what is documented. We could allow it. Commits ------- c4b16e5 [DependencyInjection] Allow casting env var processors to cast null
2 parents fb32b92 + c4b16e5 commit 091ac2b

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,12 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
181181
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
182182
}
183183

184-
return null;
184+
if (!\in_array($prefix, ['string', 'bool', 'not', 'int', 'float'], true)) {
185+
return null;
186+
}
185187
}
186188

187-
if (!\is_scalar($env)) {
189+
if (null !== $env && !\is_scalar($env)) {
188190
throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to "%s".', $name, $prefix));
189191
}
190192

@@ -199,15 +201,15 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
199201
}
200202

201203
if ('int' === $prefix) {
202-
if (false === $env = filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT)) {
204+
if (null !== $env && false === $env = filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT)) {
203205
throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to int.', $name));
204206
}
205207

206208
return (int) $env;
207209
}
208210

209211
if ('float' === $prefix) {
210-
if (false === $env = filter_var($env, \FILTER_VALIDATE_FLOAT)) {
212+
if (null !== $env && false === $env = filter_var($env, \FILTER_VALIDATE_FLOAT)) {
211213
throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to float.', $name));
212214
}
213215

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,8 @@ public function testEnvAreNullable()
839839
$container->register('foo', 'stdClass')
840840
->setPublic(true)
841841
->setProperties([
842-
'fake' => '%env(int:FAKE)%',
843-
]);
842+
'fake' => '%env(resolve:FAKE)%',
843+
]);
844844

845845
$container->compile(true);
846846

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,4 +760,22 @@ public static function provideGetEnvUrlPath()
760760
['blog//', 'https://symfony.com/blog//'],
761761
];
762762
}
763+
764+
/**
765+
* @testWith ["", "string"]
766+
* [false, "bool"]
767+
* [true, "not"]
768+
* [0, "int"]
769+
* [0.0, "float"]
770+
*/
771+
public function testGetEnvCastsNull($expected, string $prefix)
772+
{
773+
$processor = new EnvVarProcessor(new Container());
774+
775+
$this->assertSame($expected, $processor->getEnv($prefix, 'default::FOO', static function () use ($processor) {
776+
return $processor->getEnv('default', ':FOO', static function () {
777+
return null;
778+
});
779+
}));
780+
}
763781
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy