Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd51aeb

Browse files
committed
bug #37227 [DependencyInjection][CheckTypeDeclarationsPass] Handle unresolved parameters pointing to environment variables (fancyweb)
This PR was merged into the 4.4 branch. Discussion ---------- [DependencyInjection][CheckTypeDeclarationsPass] Handle unresolved parameters pointing to environment variables | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Follow up to #37193 Unresolved parameters pointing to environment variables can be added to definitions after ResolveParameterPlaceHoldersPass execution. It is the case in the initial reported bug (RegisterListenersPass is executed after ResolveParameterPlaceHoldersPass). In this case, the parameter value is an env placeholder, so we need to try to resolve it. Commits ------- dac3c8f [DependencyInjection][CheckTypeDeclarationsPass] Handle unresolved parameters pointing to environment variables
2 parents f1989fe + dac3c8f commit fd51aeb

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
199199
} elseif (\is_string($value)) {
200200
if ('%' === ($value[0] ?? '') && preg_match('/^%([^%]+)%$/', $value, $match)) {
201201
$value = $this->container->getParameter(substr($value, 1, -1));
202-
} elseif ($envPlaceholderUniquePrefix && false !== strpos($value, 'env_')) {
202+
}
203+
204+
if ($envPlaceholderUniquePrefix && \is_string($value) && false !== strpos($value, 'env_')) {
203205
// 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.
204206
// We don't need to change the value because it is already a string.
205207
if ('' === preg_replace('/'.$envPlaceholderUniquePrefix.'_\w+_[a-f0-9]{32}/U', '', $value, -1, $c) && 1 === $c) {

src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,17 +780,27 @@ public function testExpressionLanguageWithSyntheticService()
780780

781781
public function testProcessResolveParameters()
782782
{
783-
$container = new ContainerBuilder();
783+
putenv('ARRAY={"foo":"bar"}');
784+
785+
$container = new ContainerBuilder(new EnvPlaceholderParameterBag([
786+
'env_array_param' => '%env(json:ARRAY)%',
787+
]));
784788
$container->setParameter('array_param', ['foobar']);
785789
$container->setParameter('string_param', 'ccc');
786790

787-
$container
788-
->register('foobar', BarMethodCall::class)
791+
$definition = $container->register('foobar', BarMethodCall::class);
792+
$definition
789793
->addMethodCall('setArray', ['%array_param%'])
790794
->addMethodCall('setString', ['%string_param%']);
791795

796+
(new ResolveParameterPlaceHoldersPass())->process($container);
797+
798+
$definition->addMethodCall('setArray', ['%env_array_param%']);
799+
792800
(new CheckTypeDeclarationsPass(true))->process($container);
793801

794802
$this->addToAssertionCount(1);
803+
804+
putenv('ARRAY=');
795805
}
796806
}

0 commit comments

Comments
 (0)