Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7f56758

Browse filesBrowse files
committed
bug #34794 [DependencyInjection] Resolve expressions in CheckTypeDeclarationsPass (fancyweb)
This PR was merged into the 4.4 branch. Discussion ---------- [DependencyInjection] Resolve expressions in CheckTypeDeclarationsPass | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #34752 | License | MIT | Doc PR | - One more case we forgot 😅 Commits ------- b6c5a54 [DependencyInjection] Resolve expressions in CheckTypeDeclarationsPass
2 parents 4af59c2 + b6c5a54 commit 7f56758
Copy full SHA for 7f56758

File tree

Expand file treeCollapse file tree

2 files changed

+30
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+30
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php
+15Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php
+15Lines 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 <p@tchwork.com>
@@ -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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.