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

Skip to content

Commit b6c5a54

Browse files
committed
[DependencyInjection] Resolve expressions in CheckTypeDeclarationsPass
1 parent 1f00705 commit b6c5a54

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1818
use Symfony\Component\DependencyInjection\Exception\InvalidParameterTypeException;
1919
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
20+
use Symfony\Component\DependencyInjection\ExpressionLanguage;
2021
use Symfony\Component\DependencyInjection\Parameter;
2122
use Symfony\Component\DependencyInjection\Reference;
2223
use Symfony\Component\DependencyInjection\ServiceLocator;
24+
use Symfony\Component\ExpressionLanguage\Expression;
2325

2426
/**
2527
* Checks whether injected parameters are compatible with type declarations.
@@ -39,6 +41,8 @@ final class CheckTypeDeclarationsPass extends AbstractRecursivePass
3941

4042
private $autoload;
4143

44+
private $expressionLanguage;
45+
4246
/**
4347
* @param bool $autoload Whether services who's class in not loaded should be checked or not.
4448
* Defaults to false to save loading code during compilation.
@@ -172,6 +176,8 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
172176

173177
if ($value instanceof Parameter) {
174178
$value = $this->container->getParameter($value);
179+
} elseif ($value instanceof Expression) {
180+
$value = $this->getExpressionLanguage()->evaluate($value, ['container' => $this->container]);
175181
} elseif (\is_string($value) && '%' === ($value[0] ?? '') && preg_match('/^%([^%]+)%$/', $value, $match)) {
176182
$value = $this->container->getParameter($match[1]);
177183
}
@@ -202,4 +208,13 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
202208
throw new InvalidParameterTypeException($this->currentId, \is_object($value) ? \get_class($value) : \gettype($value), $parameter);
203209
}
204210
}
211+
212+
private function getExpressionLanguage(): ExpressionLanguage
213+
{
214+
if (null === $this->expressionLanguage) {
215+
$this->expressionLanguage = new ExpressionLanguage(null, $this->container->getExpressionLanguageProviders());
216+
}
217+
218+
return $this->expressionLanguage;
219+
}
205220
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\BarOptionalArgumentNotNull;
2424
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Foo;
2525
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\FooObject;
26+
use Symfony\Component\ExpressionLanguage\Expression;
2627

2728
/**
2829
* @author Nicolas Grekas <[email protected]>
@@ -569,4 +570,18 @@ public function testProcessThrowsOnIterableTypeWhenScalarPassed()
569570

570571
$this->assertInstanceOf(\stdClass::class, $container->get('bar')->foo);
571572
}
573+
574+
public function testProcessResolveExpressions()
575+
{
576+
$container = new ContainerBuilder();
577+
$container->setParameter('ccc', ['array']);
578+
579+
$container
580+
->register('foobar', BarMethodCall::class)
581+
->addMethodCall('setArray', [new Expression("parameter('ccc')")]);
582+
583+
(new CheckTypeDeclarationsPass(true))->process($container);
584+
585+
$this->addToAssertionCount(1);
586+
}
572587
}

0 commit comments

Comments
 (0)