Skip to content

Commit 8399919

Browse files
Merge branch '5.4' into 6.2
* 5.4: [Cache] Fix tests [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`
2 parents 98deb65 + 6c3c271 commit 8399919

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
@@ -198,7 +198,9 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
198198
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
199199
}
200200

201-
return null;
201+
if (!\in_array($prefix, ['string', 'bool', 'not', 'int', 'float'], true)) {
202+
return null;
203+
}
202204
}
203205

204206
if ('shuffle' === $prefix) {
@@ -207,7 +209,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
207209
return $env;
208210
}
209211

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

@@ -222,15 +224,15 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
222224
}
223225

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

229231
return (int) $env;
230232
}
231233

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

Tests/ContainerBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,8 @@ public function testEnvAreNullable()
863863
$container->register('foo', 'stdClass')
864864
->setPublic(true)
865865
->setProperties([
866-
'fake' => '%env(int:FAKE)%',
867-
]);
866+
'fake' => '%env(resolve:FAKE)%',
867+
]);
868868

869869
$container->compile(true);
870870

Tests/EnvVarProcessorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,4 +851,22 @@ public static function provideGetEnvUrlPath()
851851
['blog//', 'https://symfony.com/blog//'],
852852
];
853853
}
854+
855+
/**
856+
* @testWith ["", "string"]
857+
* [false, "bool"]
858+
* [true, "not"]
859+
* [0, "int"]
860+
* [0.0, "float"]
861+
*/
862+
public function testGetEnvCastsNull($expected, string $prefix)
863+
{
864+
$processor = new EnvVarProcessor(new Container());
865+
866+
$this->assertSame($expected, $processor->getEnv($prefix, 'default::FOO', static function () use ($processor) {
867+
return $processor->getEnv('default', ':FOO', static function () {
868+
return null;
869+
});
870+
}));
871+
}
854872
}

0 commit comments

Comments
 (0)