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

Skip to content

Commit c4b16e5

Browse files
committed
[DependencyInjection] Allow casting env var processors to cast null
1 parent fb32b92 commit c4b16e5

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)