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

Skip to content

Commit 28dc7ca

Browse files
committed
[DependencyInjection] Skip env var placeholders in CheckTypeDeclarationsPass
1 parent bfe697b commit 28dc7ca

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2020
use Symfony\Component\DependencyInjection\ExpressionLanguage;
2121
use Symfony\Component\DependencyInjection\Parameter;
22+
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
2223
use Symfony\Component\DependencyInjection\Reference;
2324
use Symfony\Component\DependencyInjection\ServiceLocator;
2425
use Symfony\Component\ExpressionLanguage\Expression;
@@ -104,27 +105,29 @@ private function checkTypeDeclarations(Definition $checkedDefinition, \Reflectio
104105
$reflectionParameters = $reflectionFunction->getParameters();
105106
$checksCount = min($reflectionFunction->getNumberOfParameters(), \count($values));
106107

108+
$envPlaceholderUniquePrefix = $this->container->getParameterBag() instanceof EnvPlaceholderParameterBag ? $this->container->getParameterBag()->getEnvPlaceholderUniquePrefix() : null;
109+
107110
for ($i = 0; $i < $checksCount; ++$i) {
108111
if (!$reflectionParameters[$i]->hasType() || $reflectionParameters[$i]->isVariadic()) {
109112
continue;
110113
}
111114

112-
$this->checkType($checkedDefinition, $values[$i], $reflectionParameters[$i]);
115+
$this->checkType($checkedDefinition, $values[$i], $reflectionParameters[$i], $envPlaceholderUniquePrefix);
113116
}
114117

115118
if ($reflectionFunction->isVariadic() && ($lastParameter = end($reflectionParameters))->hasType()) {
116119
$variadicParameters = \array_slice($values, $lastParameter->getPosition());
117120

118121
foreach ($variadicParameters as $variadicParameter) {
119-
$this->checkType($checkedDefinition, $variadicParameter, $lastParameter);
122+
$this->checkType($checkedDefinition, $variadicParameter, $lastParameter, $envPlaceholderUniquePrefix);
120123
}
121124
}
122125
}
123126

124127
/**
125128
* @throws InvalidParameterTypeException When a parameter is not compatible with the declared type
126129
*/
127-
private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter): void
130+
private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter, ?string $envPlaceholderUniquePrefix): void
128131
{
129132
$type = $parameter->getType()->getName();
130133

@@ -178,8 +181,14 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
178181
$value = $this->container->getParameter($value);
179182
} elseif ($value instanceof Expression) {
180183
$value = $this->getExpressionLanguage()->evaluate($value, ['container' => $this->container]);
181-
} elseif (\is_string($value) && '%' === ($value[0] ?? '') && preg_match('/^%([^%]+)%$/', $value, $match)) {
182-
$value = $this->container->getParameter($match[1]);
184+
} elseif (\is_string($value)) {
185+
if ($envPlaceholderUniquePrefix && 0 === strpos($value, $envPlaceholderUniquePrefix)) {
186+
return;
187+
}
188+
189+
if ('%' === ($value[0] ?? '') && preg_match('/^%([^%]+)%$/', $value, $match)) {
190+
$value = $this->container->getParameter($match[1]);
191+
}
183192
}
184193

185194
if (null === $value && $parameter->allowsNull()) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
1616
use Symfony\Component\DependencyInjection\Compiler\CheckTypeDeclarationsPass;
17+
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass;
1718
use Symfony\Component\DependencyInjection\ContainerBuilder;
1819
use Symfony\Component\DependencyInjection\Definition;
20+
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
1921
use Symfony\Component\DependencyInjection\Reference;
2022
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Bar;
2123
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\BarMethodCall;
@@ -584,4 +586,20 @@ public function testProcessResolveExpressions()
584586

585587
$this->addToAssertionCount(1);
586588
}
589+
590+
public function testProcessSkipEnvPlaceholders()
591+
{
592+
$container = new ContainerBuilder(new EnvPlaceholderParameterBag([
593+
'ccc' => '%env(json:ARRAY)%',
594+
]));
595+
596+
$container
597+
->register('foobar', BarMethodCall::class)
598+
->addMethodCall('setArray', ['%ccc%']);
599+
600+
(new ResolveParameterPlaceHoldersPass())->process($container);
601+
(new CheckTypeDeclarationsPass(true))->process($container);
602+
603+
$this->addToAssertionCount(1);
604+
}
587605
}

0 commit comments

Comments
 (0)