Skip to content

Commit 0da7fa5

Browse files
Merge branch '6.2' into 6.3
* 6.2: [Cache] Fix tests [Mailer] [MailPace] Fix undefined array key in errors response [DependencyInjection] Allow casting env var processors to cast null [Cache] Fix DBAL deprecations Fix broken symlink tests Windows PHP 7.4+ [Serializer] Fix discriminator map not working with `AbstractNormalizer::OBJECT_TO_POPULATE` [Serializer] Fix discriminator map not working with `AbstractNormalizer::OBJECT_TO_POPULATE`
2 parents ebf5f9c + 8399919 commit 0da7fa5

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

EnvVarProcessor.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
196196
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
197197
}
198198

199-
return null;
199+
if (!\in_array($prefix, ['string', 'bool', 'not', 'int', 'float'], true)) {
200+
return null;
201+
}
200202
}
201203

202204
if ('shuffle' === $prefix) {
@@ -205,7 +207,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
205207
return $env;
206208
}
207209

208-
if (!\is_scalar($env)) {
210+
if (null !== $env && !\is_scalar($env)) {
209211
throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to "%s".', $name, $prefix));
210212
}
211213

@@ -220,15 +222,15 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
220222
}
221223

222224
if ('int' === $prefix) {
223-
if (false === $env = filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT)) {
225+
if (null !== $env && false === $env = filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT)) {
224226
throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to int.', $name));
225227
}
226228

227229
return (int) $env;
228230
}
229231

230232
if ('float' === $prefix) {
231-
if (false === $env = filter_var($env, \FILTER_VALIDATE_FLOAT)) {
233+
if (null !== $env && false === $env = filter_var($env, \FILTER_VALIDATE_FLOAT)) {
232234
throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to float.', $name));
233235
}
234236

Tests/ContainerBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,8 @@ public function testEnvAreNullable()
988988
$container->register('foo', 'stdClass')
989989
->setPublic(true)
990990
->setProperties([
991-
'fake' => '%env(int:FAKE)%',
992-
]);
991+
'fake' => '%env(resolve:FAKE)%',
992+
]);
993993

994994
$container->compile(true);
995995

Tests/EnvVarProcessorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,4 +845,22 @@ public static function provideGetEnvUrlPath()
845845
['blog//', 'https://symfony.com/blog//'],
846846
];
847847
}
848+
849+
/**
850+
* @testWith ["", "string"]
851+
* [false, "bool"]
852+
* [true, "not"]
853+
* [0, "int"]
854+
* [0.0, "float"]
855+
*/
856+
public function testGetEnvCastsNull($expected, string $prefix)
857+
{
858+
$processor = new EnvVarProcessor(new Container());
859+
860+
$this->assertSame($expected, $processor->getEnv($prefix, 'default::FOO', static function () use ($processor) {
861+
return $processor->getEnv('default', ':FOO', static function () {
862+
return null;
863+
});
864+
}));
865+
}
848866
}

0 commit comments

Comments
 (0)