@@ -585,6 +585,10 @@ application handlers::
585
585
}
586
586
}
587
587
588
+ .. seealso ::
589
+
590
+ See also :doc: `tagged locator services </service_container/service_subscribers_locators >`
591
+
588
592
Tagged Services with Priority
589
593
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
590
594
@@ -694,22 +698,20 @@ in the configuration of the collecting service:
694
698
695
699
$services->set(App\HandlerCollection::class)
696
700
->args([
697
- tagged_iterator('app.handler', null, null, 'getPriority'),
698
- ]
699
- )
700
- ;
701
+ tagged_iterator('app.handler', null, null, 'getPriority'),
702
+ ])
703
+ ;
701
704
};
702
705
703
- Tagged Services Collection with Index
704
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
706
+ Tagged Services with Index
707
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
705
708
706
709
If 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 ``.
709
712
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:
713
715
714
716
.. configuration-block ::
715
717
@@ -726,8 +728,7 @@ but we can now access a specific injected service:
726
728
- { name: 'app.handler', key: 'handler_two' }
727
729
728
730
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' }]
731
732
732
733
.. code-block :: xml
733
734
@@ -748,26 +749,36 @@ but we can now access a specific injected service:
748
749
</service >
749
750
750
751
<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" />
753
753
</service >
754
754
</services >
755
755
</container >
756
756
757
757
.. code-block :: php
758
758
759
759
// config/services.php
760
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
761
+
762
+ use App\Handler\One;
763
+ use App\Handler\Two;
760
764
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
761
765
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']);
764
771
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']);
767
774
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
+ };
771
782
772
783
After compilation the ``HandlerCollection `` is able to iterate over your
773
784
application 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::
789
800
790
801
.. tip ::
791
802
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 ``)::
796
806
797
807
// src/Handler/One.php
798
808
namespace App\Handler;
799
809
800
810
class One
801
811
{
812
+ // ...
802
813
public static function getDefaultIndexName(): string
803
814
{
804
815
return 'handler_one';
805
816
}
806
817
}
807
818
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
-
848
819
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:
865
821
866
822
.. configuration-block ::
867
823
@@ -872,8 +828,8 @@ to get an array and then retrieve the ``handler_two`` handler::
872
828
# ...
873
829
874
830
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 ' }]
877
833
878
834
.. code-block :: xml
879
835
@@ -885,23 +841,35 @@ to get an array and then retrieve the ``handler_two`` handler::
885
841
http://symfony.com/schema/dic/services/services-1.0.xsd" >
886
842
887
843
<services >
888
-
889
844
<!-- ... --!>
890
845
891
846
<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
+ />
894
852
</service >
895
853
</services >
896
854
</container >
897
855
898
856
.. code-block :: php
899
857
900
858
// config/services.php
901
- // ...
859
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
902
860
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();
906
866
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