|
14 | 14 | use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
|
15 | 15 | use Symfony\Component\DependencyInjection\Container;
|
16 | 16 | use Symfony\Component\DependencyInjection\Definition;
|
| 17 | +use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException; |
17 | 18 | use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
18 | 19 | use Symfony\Component\DependencyInjection\Exception\InvalidParameterTypeException;
|
19 | 20 | use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
20 | 21 | use Symfony\Component\DependencyInjection\ExpressionLanguage;
|
21 | 22 | use Symfony\Component\DependencyInjection\Parameter;
|
| 23 | +use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag; |
22 | 24 | use Symfony\Component\DependencyInjection\Reference;
|
23 | 25 | use Symfony\Component\DependencyInjection\ServiceLocator;
|
24 | 26 | use Symfony\Component\ExpressionLanguage\Expression;
|
@@ -104,27 +106,29 @@ private function checkTypeDeclarations(Definition $checkedDefinition, \Reflectio
|
104 | 106 | $reflectionParameters = $reflectionFunction->getParameters();
|
105 | 107 | $checksCount = min($reflectionFunction->getNumberOfParameters(), \count($values));
|
106 | 108 |
|
| 109 | + $envPlaceholderUniquePrefix = $this->container->getParameterBag() instanceof EnvPlaceholderParameterBag ? $this->container->getParameterBag()->getEnvPlaceholderUniquePrefix() : null; |
| 110 | + |
107 | 111 | for ($i = 0; $i < $checksCount; ++$i) {
|
108 | 112 | if (!$reflectionParameters[$i]->hasType() || $reflectionParameters[$i]->isVariadic()) {
|
109 | 113 | continue;
|
110 | 114 | }
|
111 | 115 |
|
112 |
| - $this->checkType($checkedDefinition, $values[$i], $reflectionParameters[$i]); |
| 116 | + $this->checkType($checkedDefinition, $values[$i], $reflectionParameters[$i], $envPlaceholderUniquePrefix); |
113 | 117 | }
|
114 | 118 |
|
115 | 119 | if ($reflectionFunction->isVariadic() && ($lastParameter = end($reflectionParameters))->hasType()) {
|
116 | 120 | $variadicParameters = \array_slice($values, $lastParameter->getPosition());
|
117 | 121 |
|
118 | 122 | foreach ($variadicParameters as $variadicParameter) {
|
119 |
| - $this->checkType($checkedDefinition, $variadicParameter, $lastParameter); |
| 123 | + $this->checkType($checkedDefinition, $variadicParameter, $lastParameter, $envPlaceholderUniquePrefix); |
120 | 124 | }
|
121 | 125 | }
|
122 | 126 | }
|
123 | 127 |
|
124 | 128 | /**
|
125 | 129 | * @throws InvalidParameterTypeException When a parameter is not compatible with the declared type
|
126 | 130 | */
|
127 |
| - private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter): void |
| 131 | + private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter, ?string $envPlaceholderUniquePrefix): void |
128 | 132 | {
|
129 | 133 | $type = $parameter->getType()->getName();
|
130 | 134 |
|
@@ -178,8 +182,22 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
|
178 | 182 | $value = $this->container->getParameter($value);
|
179 | 183 | } elseif ($value instanceof Expression) {
|
180 | 184 | $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]); |
| 185 | + } elseif (\is_string($value)) { |
| 186 | + if ('%' === ($value[0] ?? '') && preg_match('/^%([^%]+)%$/', $value, $match)) { |
| 187 | + // Only array parameters are not inlined when dumped. |
| 188 | + $value = []; |
| 189 | + } elseif ($envPlaceholderUniquePrefix && false !== strpos($value, 'env_')) { |
| 190 | + // 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. |
| 191 | + // We don't need to change the value because it is already a string. |
| 192 | + if ('' === preg_replace('/'.$envPlaceholderUniquePrefix.'_\w+_[a-f0-9]{32}/U', '', $value, -1, $c) && 1 === $c) { |
| 193 | + try { |
| 194 | + $value = $this->container->resolveEnvPlaceholders($value, true); |
| 195 | + } catch (EnvNotFoundException | RuntimeException $e) { |
| 196 | + // If an env placeholder cannot be resolved, we skip the validation. |
| 197 | + return; |
| 198 | + } |
| 199 | + } |
| 200 | + } |
183 | 201 | }
|
184 | 202 |
|
185 | 203 | if (null === $value && $parameter->allowsNull()) {
|
|
0 commit comments