@@ -1010,97 +1010,110 @@ array element. For example, to retrieve the ``handler_two`` handler::
10101010 }
10111011 }
10121012
1013- .. tip ::
1013+ You can omit the index attribute (``key `` in the previous example) by setting
1014+ the ``index_by `` attribute on the ``tagged_iterator `` tag. In this case, you
1015+ must define a static method whose name follows the pattern:
1016+ ``getDefault<CamelCase index_by value>Name ``.
1017+
1018+ For example, if ``index_by `` is ``handler ``, the method name must be
1019+ ``getDefaultHandlerName() ``:
1020+
1021+ .. code-block :: yaml
1022+
1023+ # config/services.yaml
1024+ services :
1025+ # ...
1026+
1027+ App\HandlerCollection :
1028+ arguments : [!tagged_iterator { tag: 'app.handler', index_by: 'handler' }]
10141029
1015- Just like the priority, you can also implement a static
1016- ``getDefaultIndexName() `` method in the handlers and omit the
1017- index attribute (``key ``)::
1030+ .. code-block :: php
10181031
1019- // src/Handler/One.php
1020- namespace App\Handler;
1032+ // src/Handler/One.php
1033+ namespace App\Handler;
10211034
1022- class One
1035+ class One
1036+ {
1037+ // ...
1038+ public static function getDefaultHandlerName(): string
10231039 {
1024- // ...
1025- public static function getDefaultIndexName(): string
1026- {
1027- return 'handler_one';
1028- }
1040+ return 'handler_one';
10291041 }
1042+ }
10301043
1031- You also can define the name of the static method to implement on each service
1032- with the ``default_index_method `` attribute on the tagged argument:
1044+ You also can define the name of the static method to implement on each service
1045+ with the ``default_index_method `` attribute on the tagged argument:
10331046
1034- .. configuration-block ::
1047+ .. configuration-block ::
10351048
1036- .. code-block :: php-attributes
1049+ .. code-block :: php-attributes
10371050
1038- // src/HandlerCollection.php
1039- namespace App;
1051+ // src/HandlerCollection.php
1052+ namespace App;
10401053
1041- use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
1054+ use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
10421055
1043- class HandlerCollection
1044- {
1045- public function __construct(
1046- #[TaggedIterator('app.handler', defaultIndexMethod: 'getIndex')]
1047- iterable $handlers
1048- ) {
1049- }
1056+ class HandlerCollection
1057+ {
1058+ public function __construct(
1059+ #[TaggedIterator('app.handler', defaultIndexMethod: 'getIndex')]
1060+ iterable $handlers
1061+ ) {
10501062 }
1063+ }
10511064
1052- .. code-block :: yaml
1065+ .. code-block :: yaml
10531066
1054- # config/services.yaml
1055- services :
1056- # ...
1067+ # config/services.yaml
1068+ services :
1069+ # ...
10571070
1058- App\HandlerCollection :
1059- # use getIndex() instead of getDefaultIndexName()
1060- arguments : [!tagged_iterator { tag: 'app.handler', default_index_method: 'getIndex' }]
1071+ App\HandlerCollection :
1072+ # use getIndex() instead of getDefaultIndexName()
1073+ arguments : [!tagged_iterator { tag: 'app.handler', default_index_method: 'getIndex' }]
10611074
1062- .. code-block :: xml
1075+ .. code-block :: xml
10631076
1064- <!-- config/services.xml -->
1065- <?xml version =" 1.0" encoding =" UTF-8" ?>
1066- <container xmlns =" http://symfony.com/schema/dic/services"
1067- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1068- xsi : schemaLocation =" http://symfony.com/schema/dic/services
1069- https://symfony.com/schema/dic/services/services-1.0.xsd" >
1077+ <!-- config/services.xml -->
1078+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1079+ <container xmlns =" http://symfony.com/schema/dic/services"
1080+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1081+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
1082+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
10701083
1071- <services >
1072- <!-- ... -->
1084+ <services >
1085+ <!-- ... -->
10731086
1074- <service id =" App\HandlerCollection" >
1075- <!-- use getIndex() instead of getDefaultIndexName() -->
1076- <argument type =" tagged_iterator"
1077- tag =" app.handler"
1078- default-index-method =" someFunctionName"
1079- />
1080- </service >
1081- </services >
1082- </container >
1087+ <service id =" App\HandlerCollection" >
1088+ <!-- use getIndex() instead of getDefaultIndexName() -->
1089+ <argument type =" tagged_iterator"
1090+ tag =" app.handler"
1091+ default-index-method =" someFunctionName"
1092+ />
1093+ </service >
1094+ </services >
1095+ </container >
10831096
1084- .. code-block :: php
1097+ .. code-block :: php
10851098
1086- // config/services.php
1087- namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1099+ // config/services.php
1100+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
10881101
1089- use App\HandlerCollection;
1090- use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1102+ use App\HandlerCollection;
1103+ use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
10911104
1092- return function (ContainerConfigurator $container): void {
1093- $services = $container->services();
1105+ return function (ContainerConfigurator $container) {
1106+ $services = $container->services();
10941107
1095- // ...
1108+ // ...
10961109
1097- // use getIndex() instead of getDefaultIndexName()
1098- $services->set(HandlerCollection::class)
1099- ->args([
1100- tagged_iterator('app.handler', null, 'getIndex'),
1101- ])
1102- ;
1103- };
1110+ // use getIndex() instead of getDefaultIndexName()
1111+ $services->set(HandlerCollection::class)
1112+ ->args([
1113+ tagged_iterator('app.handler', null, 'getIndex'),
1114+ ])
1115+ ;
1116+ };
11041117
11051118 .. _tags_as-tagged-item :
11061119
0 commit comments