@@ -511,11 +511,40 @@ Symfony provides a shortcut to inject all services tagged with a specific tag,
511511which is a common need in some applications, so you don't have to write a
512512compiler pass just for that.
513513
514- In the following example, all services tagged with ``app.handler `` are passed as
515- first constructor argument to the ``App\HandlerCollection `` service:
514+ Consider the following ``HandlerCollection `` class where you want to inject
515+ all services tagged with ``app.handler `` into its constructor argument::
516+
517+ // src/HandlerCollection.php
518+ namespace App;
519+
520+ class HandlerCollection
521+ {
522+ public function __construct(iterable $handlers)
523+ {
524+ }
525+ }
526+
527+ Symfony allows you to inject the services using YAML/XML/PHP configuration or
528+ directly via PHP attributes:
516529
517530.. configuration-block ::
518531
532+ .. code-block :: php-attributes
533+
534+ // src/HandlerCollection.php
535+ namespace App;
536+
537+ use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
538+
539+ class HandlerCollection
540+ {
541+ public function __construct(
542+ // the attribute must be applied directly to the argument to autowire
543+ #[TaggedIterator('app.handler')] iterable $handlers
544+ ) {
545+ }
546+ }
547+
519548 .. code-block :: yaml
520549
521550 # config/services.yaml
@@ -578,24 +607,26 @@ first constructor argument to the ``App\HandlerCollection`` service:
578607 ;
579608 };
580609
581- After compilation the ``HandlerCollection `` service is able to iterate over your
582- application handlers::
583-
584- // src/HandlerCollection.php
585- namespace App;
586-
587- class HandlerCollection
588- {
589- public function __construct(iterable $handlers)
590- {
591- }
592- }
593-
594610 If for some reason you need to exclude one or more services when using a tagged
595611iterator, add the ``exclude `` option:
596612
597613.. configuration-block ::
598614
615+ .. code-block :: php-attributes
616+
617+ // src/HandlerCollection.php
618+ namespace App;
619+
620+ use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
621+
622+ class HandlerCollection
623+ {
624+ public function __construct(
625+ #[TaggedIterator('app.handler', exclude: ['App\Handler\Three'])] iterable $handlers
626+ ) {
627+ }
628+ }
629+
599630 .. code-block :: yaml
600631
601632 # config/services.yaml
0 commit comments