From c50aad2be16ac785f54ab53bcfee02193bc651c7 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sat, 9 Mar 2019 23:18:01 +0100 Subject: [PATCH] [DI] replace "nullable" env processor by improving the "default" one --- .../Component/DependencyInjection/CHANGELOG.md | 3 +-- .../DependencyInjection/EnvVarProcessor.php | 17 +++++++++-------- .../RegisterEnvVarProcessorsPassTest.php | 1 - .../Tests/EnvVarProcessorTest.php | 2 +- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index e5fcd4f768..7390e4289f 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -5,8 +5,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 + * added `%env(default:param_name:...)%` processor to fallback to a parameter or to null when using `%env(default::...)%` * added support for deprecating aliases * made `ContainerParametersResource` final and not implement `Serializable` anymore * added ability to define an index for a tagged collection diff --git a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php index 137fc97798..d734cf5b8f 100644 --- a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php +++ b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php @@ -41,7 +41,6 @@ class EnvVarProcessor implements EnvVarProcessorInterface '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', @@ -84,15 +83,21 @@ class EnvVarProcessor implements EnvVarProcessorInterface $next = substr($name, $i + 1); $default = substr($name, 0, $i); - if (!$this->container->hasParameter($default)) { + if ('' !== $default && !$this->container->hasParameter($default)) { throw new RuntimeException(sprintf('Invalid env fallback in "default:%s": parameter "%s" not found.', $name, $default)); } try { - return $getEnv($next); + $env = $getEnv($next); + + if ('' !== $env && null !== $env) { + return $env; + } } catch (EnvNotFoundException $e) { - return $this->container->getParameter($default); + // no-op } + + return '' === $default ? null : $this->container->getParameter($default); } if ('file' === $prefix) { @@ -196,10 +201,6 @@ class EnvVarProcessor implements EnvVarProcessorInterface 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 7d73f6cfb3..f376165dfc 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php @@ -39,7 +39,6 @@ class RegisterEnvVarProcessorsPassTest extends TestCase '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 31614ce7d0..7192127691 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php @@ -440,7 +440,7 @@ class EnvVarProcessorTest extends TestCase public function testGetEnvNullable($value, $processed) { $processor = new EnvVarProcessor(new Container()); - $result = $processor->getEnv('nullable', 'foo', function ($name) use ($value) { + $result = $processor->getEnv('default', ':foo', function ($name) use ($value) { $this->assertSame('foo', $name); return $value;