diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 7afa4a21b89a0..c3f17d360c8c6 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * added `%env(trim:...)%` processor to trim a string value * added `%env(default:...)%` processor to fallback to a default value + * added `%env(nullable:...)%` processor to allow empty variables to be processed as null values 4.2.0 ----- diff --git a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php index 1b71924d8a719..da4e2d9f7ac62 100644 --- a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php +++ b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php @@ -41,6 +41,7 @@ public static function getProvidedTypes() 'int' => 'int', 'json' => 'array', 'key' => 'bool|int|float|string|array', + 'nullable' => 'bool|int|float|string|array', 'resolve' => 'string', 'default' => 'bool|int|float|string|array', 'string' => 'string', @@ -195,6 +196,10 @@ public function getEnv($prefix, $name, \Closure $getEnv) return str_getcsv($env); } + if ('nullable' === $prefix) { + return '' === $env ? null : $env; + } + if ('trim' === $prefix) { return trim($env); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php index f376165dfc0a1..7d73f6cfb37e9 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php @@ -39,6 +39,7 @@ public function testSimpleProcessor() 'int' => ['int'], 'json' => ['array'], 'key' => ['bool', 'int', 'float', 'string', 'array'], + 'nullable' => ['bool', 'int', 'float', 'string', 'array'], 'resolve' => ['string'], 'default' => ['bool', 'int', 'float', 'string', 'array'], 'string' => ['string'], diff --git a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php index 0ae6f4b0ef2e7..31614ce7d064f 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php @@ -433,4 +433,29 @@ public function testGetEnvKeyChained() ]; })); } + + /** + * @dataProvider validNullables + */ + public function testGetEnvNullable($value, $processed) + { + $processor = new EnvVarProcessor(new Container()); + $result = $processor->getEnv('nullable', 'foo', function ($name) use ($value) { + $this->assertSame('foo', $name); + + return $value; + }); + $this->assertSame($processed, $result); + } + + public function validNullables() + { + return [ + ['hello', 'hello'], + ['', null], + ['null', 'null'], + ['Null', 'Null'], + ['NULL', 'NULL'], + ]; + } } 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