@@ -585,6 +585,10 @@ application handlers::
585585 }
586586 }
587587
588+ .. seealso ::
589+
590+ See also :doc: `tagged locator services </service_container/service_subscribers_locators >`
591+
588592Tagged Services with Priority
589593~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
590594
@@ -694,22 +698,20 @@ in the configuration of the collecting service:
694698
695699 $services->set(App\HandlerCollection::class)
696700 ->args([
697- tagged_iterator('app.handler', null, null, 'getPriority'),
698- ]
699- )
700- ;
701+ tagged_iterator('app.handler', null, null, 'getPriority'),
702+ ])
703+ ;
701704 };
702705
703- Tagged Services Collection with Index
704- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
706+ Tagged Services with Index
707+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
705708
706709If you want to retrieve a specific service within the injected collection
707- you can use the ``index_by `` and ``default_index_method `` options of the argument
708- in combination with ``!tagged ``.
710+ you can use the ``index_by `` and ``default_index_method `` options of the
711+ argument in combination with ``!tagged ``.
709712
710- In the following example, all services tagged with ``app.handler `` are passed as
711- first constructor argument to ``App\Handler\HandlerCollection ``,
712- but we can now access a specific injected service:
713+ Using the previous example, this service configuration creates a collection
714+ indexed by the ``key `` attribute:
713715
714716.. configuration-block ::
715717
@@ -726,8 +728,7 @@ but we can now access a specific injected service:
726728 - { name: 'app.handler', key: 'handler_two' }
727729
728730 App\HandlerCollection :
729- # inject all services tagged with app.handler as first argument
730- arguments : [!tagged { tag: 'app.handler', index_by: 'key' }]
731+ arguments : [!tagged_iterator { tag: 'app.handler', index_by: 'key' }]
731732
732733 .. code-block :: xml
733734
@@ -748,26 +749,36 @@ but we can now access a specific injected service:
748749 </service >
749750
750751 <service id =" App\HandlerCollection" >
751- <!-- inject all services tagged with app.handler as first argument -->
752- <argument type =" tagged" tag =" app.handler" index-by =" key" />
752+ <argument type =" tagged_iterator" tag =" app.handler" index-by =" key" />
753753 </service >
754754 </services >
755755 </container >
756756
757757 .. code-block :: php
758758
759759 // config/services.php
760+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
761+
762+ use App\Handler\One;
763+ use App\Handler\Two;
760764 use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
761765
762- $container->register(App\Handler\One::class)
763- ->addTag('app.handler', ['key' => 'handler_one']);
766+ return function (ContainerConfigurator $configurator) {
767+ $services = $configurator->services();
768+
769+ $services->set(One::class)
770+ ->tag('app.handler', ['key' => 'handler_one']);
764771
765- $container->register(App\Handler\ Two::class)
766- ->addTag ('app.handler', ['key' => 'handler_two']);
772+ $services->set( Two::class)
773+ ->tag ('app.handler', ['key' => 'handler_two']);
767774
768- $container->register(App\Handler\HandlerCollection::class)
769- // inject all services tagged with app.handler as first argument
770- ->addArgument(new TaggedIteratorArgument('app.handler', 'key'));
775+ $services->set(App\HandlerCollection::class)
776+ ->args([
777+ // 2nd argument is the index attribute name
778+ tagged_iterator('app.handler', 'key'),
779+ ])
780+ ;
781+ };
771782
772783 After compilation the ``HandlerCollection `` is able to iterate over your
773784application handlers. To retrieve a specific service by it's ``key `` attribute
@@ -789,79 +800,24 @@ to get an array and then retrieve the ``handler_two`` handler::
789800
790801.. tip ::
791802
792- You can omit the ``index_attribute_name `` attribute, by implementing a static
793- method ``getDefaultIndexAttributeName `` to the handler.
794-
795- Based on the previous example ``App\Handler\One `` should look like this::
803+ Just like the priority, you can also implement a static
804+ ``getDefaultIndexAttributeName() `` method in the handlers and omit the
805+ index attribute (``key ``)::
796806
797807 // src/Handler/One.php
798808 namespace App\Handler;
799809
800810 class One
801811 {
812+ // ...
802813 public static function getDefaultIndexName(): string
803814 {
804815 return 'handler_one';
805816 }
806817 }
807818
808- And the configuration:
809-
810- .. configuration-block ::
811-
812- .. code-block :: yaml
813-
814- # config/services.yaml
815- services :
816- App\Handler\One :
817- tags :
818- - { name: 'app.handler', priority: 20 }
819-
820- # ...
821-
822- .. code-block :: xml
823-
824- <!-- config/services.xml -->
825- <?xml version =" 1.0" encoding =" UTF-8" ?>
826- <container xmlns =" http://symfony.com/schema/dic/services"
827- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
828- xsi : schemaLocation =" http://symfony.com/schema/dic/services
829- http://symfony.com/schema/dic/services/services-1.0.xsd" >
830-
831- <services >
832- <service id =" App\Handler\One" >
833- <tag name =" app.handler" priority =" 20" />
834- </service >
835-
836- <!-- ... -->
837- </services >
838- </container >
839-
840- .. code-block :: php
841-
842- // config/services.php
843- $container->register(App\Handler\One::class)
844- ->addTag('app.handler', ['priority' => 20]);
845-
846- // ...
847-
848819 You also can define the name of the static method to implement on each service
849- with the ``default_index_method `` attribute on the argument.
850-
851- Based on the previous example ``App\Handler\One `` should look like::
852-
853- // src/Handler/One.php
854- namespace App\Handler;
855-
856- class One
857- {
858- public static function someFunctionName(): string
859- {
860- return 'handler_one';
861- }
862- }
863-
864- And the configuration:
820+ with the ``default_index_method `` attribute on the tagged argument:
865821
866822 .. configuration-block ::
867823
@@ -872,8 +828,8 @@ to get an array and then retrieve the ``handler_two`` handler::
872828 # ...
873829
874830 App\HandlerCollection :
875- # inject all services tagged with app.handler as first argument
876- arguments : [!tagged { tag: 'app.handler', index_by: 'key', default_index_method: 'someFunctionName ' }]
831+ # use getIndex() instead of getDefaultIndexName()
832+ arguments : [!tagged_iterator { tag: 'app.handler', default_index_method: 'getIndex ' }]
877833
878834 .. code-block :: xml
879835
@@ -885,23 +841,35 @@ to get an array and then retrieve the ``handler_two`` handler::
885841 http://symfony.com/schema/dic/services/services-1.0.xsd" >
886842
887843 <services >
888-
889844 <!-- ... --!>
890845
891846 <service id="App\HandlerCollection">
892- <!-- inject all services tagged with app.handler as first argument -->
893- <argument type =" tagged" tag =" app.handler" index-by =" key" default-index-method =" someFunctionName" />
847+ <!-- use getIndex() instead of getDefaultIndexName() -->
848+ <argument type =" tagged_iterator"
849+ tag =" app.handler"
850+ default-index-method =" someFunctionName"
851+ />
894852 </service >
895853 </services >
896854 </container >
897855
898856 .. code-block :: php
899857
900858 // config/services.php
901- // ...
859+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
902860
903- $container->register(App\HandlerCollection::class)
904- // inject all services tagged with app.handler as first argument
905- ->addArgument(new TaggedIteratorArgument('app.handler', 'key', 'someFunctionName'));
861+ use App\HandlerCollection;
862+ use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
863+
864+ return function (ContainerConfigurator $configurator) {
865+ $services = $configurator->services();
906866
907- See also :doc: `tagged locator services </service_container/service_subscribers_locators >`
867+ // ...
868+
869+ // use getIndex() instead of getDefaultIndexName()
870+ $services->set(HandlerCollection::class)
871+ ->args([
872+ tagged_iterator('app.handler', null, 'getIndex'),
873+ ])
874+ ;
875+ };
0 commit comments