From 0db72289d3e167731fe5e198c2062f25ce6725a2 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Wed, 4 Jan 2023 14:01:39 +0100 Subject: [PATCH] [DependencyInjection] Autowire arguments using the #[TaggedIterator] attribute --- service_container/tags.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/service_container/tags.rst b/service_container/tags.rst index 9c89ef8c85a..c834c0e6b17 100644 --- a/service_container/tags.rst +++ b/service_container/tags.rst @@ -591,6 +591,26 @@ application handlers:: } } +Injecting tagged services can be also be done through autowiring thanks to the +``#[TaggedIterator]`` attribute. This attribute must be directly used on the +argument to autowire:: + + // src/HandlerCollection.php + namespace App; + + use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; + + class HandlerCollection + { + public function __construct(#[TaggedIterator('app.handler')] iterable $handlers) + { + } + } + +.. versionadded:: 5.3 + + The ``#[TaggedIterator]`` attribute was introduced in Symfony 5.3 and requires PHP 8. + .. seealso:: See also :doc:`tagged locator services `