diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index 7e429971f39c5..98a3a3f836ab1 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -218,7 +218,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a if (TaggedLocator::class === $attribute->getName()) { $attribute = $attribute->newInstance(); - $arguments[$index] = new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute)); + $arguments[$index] = new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute, null, true)); break; } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php index 48f7c0186e2b4..848bb7445e10a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php @@ -32,6 +32,7 @@ use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumer; use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerConsumer; use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerFactory; +use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithoutIndex; use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService1; use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService2; use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3; @@ -403,6 +404,35 @@ public function testTaggedLocatorConfiguredViaAttribute() self::assertSame($container->get(FooTagClass::class), $locator->get('foo')); } + /** + * @requires PHP 8 + */ + public function testTaggedLocatorConfiguredViaAttributeWithoutIndex() + { + $container = new ContainerBuilder(); + $container->register(BarTagClass::class) + ->setPublic(true) + ->addTag('foo_bar') + ; + $container->register(FooTagClass::class) + ->setPublic(true) + ->addTag('foo_bar') + ; + $container->register(LocatorConsumerWithoutIndex::class) + ->setAutowired(true) + ->setPublic(true) + ; + + $container->compile(); + + /** @var LocatorConsumerWithoutIndex $s */ + $s = $container->get(LocatorConsumerWithoutIndex::class); + + $locator = $s->getLocator(); + self::assertSame($container->get(BarTagClass::class), $locator->get(BarTagClass::class)); + self::assertSame($container->get(FooTagClass::class), $locator->get(FooTagClass::class)); + } + /** * @requires PHP 8 */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/LocatorConsumerWithoutIndex.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/LocatorConsumerWithoutIndex.php new file mode 100644 index 0000000000000..74b81659527ca --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/LocatorConsumerWithoutIndex.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Tests\Fixtures; + +use Psr\Container\ContainerInterface; +use Symfony\Component\DependencyInjection\Attribute\TaggedLocator; + +final class LocatorConsumerWithoutIndex +{ + public function __construct( + #[TaggedLocator('foo_bar')] + private ContainerInterface $locator, + ) { + } + + public function getLocator(): ContainerInterface + { + return $this->locator; + } +}