From 3a604ac39243990983554fa397c4a58e300c7e91 Mon Sep 17 00:00:00 2001 From: Beno!t POLASZEK Date: Tue, 8 Jan 2019 11:24:53 +0100 Subject: [PATCH] Nullable environment variable processor --- .../DependencyInjection/CHANGELOG.md | 1 + .../DependencyInjection/EnvVarProcessor.php | 5 ++++ .../RegisterEnvVarProcessorsPassTest.php | 1 + .../Tests/EnvVarProcessorTest.php | 25 +++++++++++++++++++ 4 files changed, 32 insertions(+) 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'], + ]; + } }