From dac3c8fae8029d7bbf9c7645cfa16a483ac53790 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Thu, 11 Jun 2020 19:27:38 +0200 Subject: [PATCH] [DependencyInjection][CheckTypeDeclarationsPass] Handle unresolved parameters pointing to environment variables --- .../Compiler/CheckTypeDeclarationsPass.php | 4 +++- .../Compiler/CheckTypeDeclarationsPassTest.php | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php b/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php index c15932b06cb4..cf255640c225 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php @@ -199,7 +199,9 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar } elseif (\is_string($value)) { if ('%' === ($value[0] ?? '') && preg_match('/^%([^%]+)%$/', $value, $match)) { $value = $this->container->getParameter(substr($value, 1, -1)); - } elseif ($envPlaceholderUniquePrefix && false !== strpos($value, 'env_')) { + } + + if ($envPlaceholderUniquePrefix && \is_string($value) && false !== strpos($value, 'env_')) { // If the value is an env placeholder that is either mixed with a string or with another env placeholder, then its resolved value will always be a string, so we don't need to resolve it. // We don't need to change the value because it is already a string. if ('' === preg_replace('/'.$envPlaceholderUniquePrefix.'_\w+_[a-f0-9]{32}/U', '', $value, -1, $c) && 1 === $c) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php index 18e06b3d2948..0842f2687968 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php @@ -780,17 +780,27 @@ public function testExpressionLanguageWithSyntheticService() public function testProcessResolveParameters() { - $container = new ContainerBuilder(); + putenv('ARRAY={"foo":"bar"}'); + + $container = new ContainerBuilder(new EnvPlaceholderParameterBag([ + 'env_array_param' => '%env(json:ARRAY)%', + ])); $container->setParameter('array_param', ['foobar']); $container->setParameter('string_param', 'ccc'); - $container - ->register('foobar', BarMethodCall::class) + $definition = $container->register('foobar', BarMethodCall::class); + $definition ->addMethodCall('setArray', ['%array_param%']) ->addMethodCall('setString', ['%string_param%']); + (new ResolveParameterPlaceHoldersPass())->process($container); + + $definition->addMethodCall('setArray', ['%env_array_param%']); + (new CheckTypeDeclarationsPass(true))->process($container); $this->addToAssertionCount(1); + + putenv('ARRAY='); } }