Skip to content

Commit 60de1c9

Browse files
[DependencyInjection] Add #[Target] to tell how a dependency is used and hint named autowiring aliases
1 parent c998faa commit 60de1c9

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

DependencyInjection/RegisterControllerArgumentLocatorsPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpKernel\DependencyInjection;
1313

1414
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15+
use Symfony\Component\DependencyInjection\Attribute\Target;
1516
use Symfony\Component\DependencyInjection\ChildDefinition;
1617
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1718
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
@@ -148,7 +149,7 @@ public function process(ContainerBuilder $container)
148149
} elseif ($p->allowsNull() && !$p->isOptional()) {
149150
$invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
150151
}
151-
} elseif (isset($bindings[$bindingName = $type.' $'.$p->name]) || isset($bindings[$bindingName = '$'.$p->name]) || isset($bindings[$bindingName = $type])) {
152+
} elseif (isset($bindings[$bindingName = $type.' $'.$name = Target::parseName($p)]) || isset($bindings[$bindingName = '$'.$name]) || isset($bindings[$bindingName = $type])) {
152153
$binding = $bindings[$bindingName];
153154

154155
[$bindingValue, $bindingId, , $bindingType, $bindingFile] = $binding->getValues();

Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
16+
use Symfony\Component\DependencyInjection\Attribute\Target;
1617
use Symfony\Component\DependencyInjection\ChildDefinition;
1718
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1819
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
@@ -397,6 +398,27 @@ public function testAlias()
397398
$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
398399
$this->assertSame([RegisterTestController::class.'::fooAction', 'foo::fooAction'], array_keys($locator));
399400
}
401+
402+
/**
403+
* @requires PHP 8
404+
*/
405+
public function testBindWithTarget()
406+
{
407+
$container = new ContainerBuilder();
408+
$resolver = $container->register('argument_resolver.service')->addArgument([]);
409+
410+
$container->register('foo', WithTarget::class)
411+
->setBindings(['string $someApiKey' => new Reference('the_api_key')])
412+
->addTag('controller.service_arguments');
413+
414+
(new RegisterControllerArgumentLocatorsPass())->process($container);
415+
416+
$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
417+
$locator = $container->getDefinition((string) $locator['foo::fooAction']->getValues()[0]);
418+
419+
$expected = ['apiKey' => new ServiceClosureArgument(new Reference('the_api_key'))];
420+
$this->assertEquals($expected, $locator->getArgument(0));
421+
}
400422
}
401423

402424
class RegisterTestController
@@ -458,3 +480,12 @@ public function fooAction(string $someArg)
458480
{
459481
}
460482
}
483+
484+
class WithTarget
485+
{
486+
public function fooAction(
487+
#[Target('some.api.key')]
488+
string $apiKey
489+
) {
490+
}
491+
}

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"symfony/config": "^5.0",
3333
"symfony/console": "^4.4|^5.0",
3434
"symfony/css-selector": "^4.4|^5.0",
35-
"symfony/dependency-injection": "^5.1.8",
35+
"symfony/dependency-injection": "^5.3",
3636
"symfony/dom-crawler": "^4.4|^5.0",
3737
"symfony/expression-language": "^4.4|^5.0",
3838
"symfony/finder": "^4.4|^5.0",
@@ -53,7 +53,7 @@
5353
"symfony/config": "<5.0",
5454
"symfony/console": "<4.4",
5555
"symfony/form": "<5.0",
56-
"symfony/dependency-injection": "<5.1.8",
56+
"symfony/dependency-injection": "<5.3",
5757
"symfony/doctrine-bridge": "<5.0",
5858
"symfony/http-client": "<5.0",
5959
"symfony/mailer": "<5.0",

0 commit comments

Comments
 (0)