From 38a8ab92fa7da6adf416cb584d329861b842bb8b Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Fri, 12 Oct 2018 16:41:04 +0200 Subject: [PATCH] [DI] Default undefined env to empty string during compile --- .../Compiler/ValidateEnvPlaceholdersPass.php | 2 +- .../Tests/Compiler/ValidateEnvPlaceholdersPassTest.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ValidateEnvPlaceholdersPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ValidateEnvPlaceholdersPass.php index 06b655328b43d..2524a5dca7c1e 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ValidateEnvPlaceholdersPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ValidateEnvPlaceholdersPass.php @@ -48,7 +48,7 @@ public function process(ContainerBuilder $container) foreach ($resolvingBag->getEnvPlaceholders() + $resolvingBag->getUnusedEnvPlaceholders() as $env => $placeholders) { $values = array(); if (false === $i = strpos($env, ':')) { - $default = $defaultBag->has("env($env)") ? $defaultBag->get("env($env)") : null; + $default = $defaultBag->has("env($env)") ? $defaultBag->get("env($env)") : self::$typeFixtures['string']; $defaultType = null !== $default ? self::getType($default) : 'string'; $values[$defaultType] = $default; } else { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php index d9636ee05f781..6a877233c9a40 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php @@ -48,6 +48,7 @@ public function testDefaultEnvWithoutPrefixIsValidatedInConfig() $container->registerExtension($ext = new EnvExtension()); $container->prependExtensionConfig('env_extension', $expected = array( 'float_node' => '%env(FLOATISH)%', + 'string_node' => '%env(UNDEFINED)%', )); $this->doProcess($container); @@ -311,6 +312,14 @@ public function getConfigTreeBuilder() ->arrayNode('simple_array_node')->end() ->enumNode('enum_node')->values(array('a', 'b'))->end() ->variableNode('variable_node')->end() + ->scalarNode('string_node') + ->validate() + ->ifTrue(function ($value) { + return !\is_string($value); + }) + ->thenInvalid('%s is not a string') + ->end() + ->end() ->end(); return $treeBuilder;