@@ -1000,97 +1000,110 @@ array element. For example, to retrieve the ``handler_two`` handler::
10001000 }
10011001 }
10021002
1003- .. tip ::
1003+ You can omit the index attribute (``key `` in the previous example) by setting
1004+ the ``index_by `` attribute on the ``tagged_iterator `` tag. In this case, you
1005+ must define a static method whose name follows the pattern:
1006+ ``getDefault<CamelCase index_by value>Name ``.
1007+
1008+ For example, if ``index_by `` is ``handler ``, the method name must be
1009+ ``getDefaultHandlerName() ``:
1010+
1011+ .. code-block :: yaml
1012+
1013+ # config/services.yaml
1014+ services :
1015+ # ...
1016+
1017+ App\HandlerCollection :
1018+ arguments : [!tagged_iterator { tag: 'app.handler', index_by: 'handler' }]
10041019
1005- Just like the priority, you can also implement a static
1006- ``getDefaultIndexName() `` method in the handlers and omit the
1007- index attribute (``key ``)::
1020+ .. code-block :: php
10081021
1009- // src/Handler/One.php
1010- namespace App\Handler;
1022+ // src/Handler/One.php
1023+ namespace App\Handler;
10111024
1012- class One
1025+ class One
1026+ {
1027+ // ...
1028+ public static function getDefaultHandlerName(): string
10131029 {
1014- // ...
1015- public static function getDefaultIndexName(): string
1016- {
1017- return 'handler_one';
1018- }
1030+ return 'handler_one';
10191031 }
1032+ }
10201033
1021- You also can define the name of the static method to implement on each service
1022- with the ``default_index_method `` attribute on the tagged argument:
1034+ You also can define the name of the static method to implement on each service
1035+ with the ``default_index_method `` attribute on the tagged argument:
10231036
1024- .. configuration-block ::
1037+ .. configuration-block ::
10251038
1026- .. code-block :: php-attributes
1039+ .. code-block :: php-attributes
10271040
1028- // src/HandlerCollection.php
1029- namespace App;
1041+ // src/HandlerCollection.php
1042+ namespace App;
10301043
1031- use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
1044+ use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
10321045
1033- class HandlerCollection
1034- {
1035- public function __construct(
1036- #[TaggedIterator('app.handler', defaultIndexMethod: 'getIndex')]
1037- iterable $handlers
1038- ) {
1039- }
1046+ class HandlerCollection
1047+ {
1048+ public function __construct(
1049+ #[TaggedIterator('app.handler', defaultIndexMethod: 'getIndex')]
1050+ iterable $handlers
1051+ ) {
10401052 }
1053+ }
10411054
1042- .. code-block :: yaml
1055+ .. code-block :: yaml
10431056
1044- # config/services.yaml
1045- services :
1046- # ...
1057+ # config/services.yaml
1058+ services :
1059+ # ...
10471060
1048- App\HandlerCollection :
1049- # use getIndex() instead of getDefaultIndexName()
1050- arguments : [!tagged_iterator { tag: 'app.handler', default_index_method: 'getIndex' }]
1061+ App\HandlerCollection :
1062+ # use getIndex() instead of getDefaultIndexName()
1063+ arguments : [!tagged_iterator { tag: 'app.handler', default_index_method: 'getIndex' }]
10511064
1052- .. code-block :: xml
1065+ .. code-block :: xml
10531066
1054- <!-- config/services.xml -->
1055- <?xml version =" 1.0" encoding =" UTF-8" ?>
1056- <container xmlns =" http://symfony.com/schema/dic/services"
1057- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1058- xsi : schemaLocation =" http://symfony.com/schema/dic/services
1059- https://symfony.com/schema/dic/services/services-1.0.xsd" >
1067+ <!-- config/services.xml -->
1068+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1069+ <container xmlns =" http://symfony.com/schema/dic/services"
1070+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1071+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
1072+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
10601073
1061- <services >
1062- <!-- ... -->
1074+ <services >
1075+ <!-- ... -->
10631076
1064- <service id =" App\HandlerCollection" >
1065- <!-- use getIndex() instead of getDefaultIndexName() -->
1066- <argument type =" tagged_iterator"
1067- tag =" app.handler"
1068- default-index-method =" someFunctionName"
1069- />
1070- </service >
1071- </services >
1072- </container >
1077+ <service id =" App\HandlerCollection" >
1078+ <!-- use getIndex() instead of getDefaultIndexName() -->
1079+ <argument type =" tagged_iterator"
1080+ tag =" app.handler"
1081+ default-index-method =" someFunctionName"
1082+ />
1083+ </service >
1084+ </services >
1085+ </container >
10731086
1074- .. code-block :: php
1087+ .. code-block :: php
10751088
1076- // config/services.php
1077- namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1089+ // config/services.php
1090+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
10781091
1079- use App\HandlerCollection;
1080- use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1092+ use App\HandlerCollection;
1093+ use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
10811094
1082- return function (ContainerConfigurator $container): void {
1083- $services = $container->services();
1095+ return function (ContainerConfigurator $container) {
1096+ $services = $container->services();
10841097
1085- // ...
1098+ // ...
10861099
1087- // use getIndex() instead of getDefaultIndexName()
1088- $services->set(HandlerCollection::class)
1089- ->args([
1090- tagged_iterator('app.handler', null, 'getIndex'),
1091- ])
1092- ;
1093- };
1100+ // use getIndex() instead of getDefaultIndexName()
1101+ $services->set(HandlerCollection::class)
1102+ ->args([
1103+ tagged_iterator('app.handler', null, 'getIndex'),
1104+ ])
1105+ ;
1106+ };
10941107
10951108 .. _tags_as-tagged-item :
10961109
0 commit comments