@@ -1010,97 +1010,110 @@ array element. For example, to retrieve the ``handler_two`` handler::
1010
1010
}
1011
1011
}
1012
1012
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' }]
1014
1029
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
1018
1031
1019
- // src/Handler/One.php
1020
- namespace App\Handler;
1032
+ // src/Handler/One.php
1033
+ namespace App\Handler;
1021
1034
1022
- class One
1035
+ class One
1036
+ {
1037
+ // ...
1038
+ public static function getDefaultHandlerName(): string
1023
1039
{
1024
- // ...
1025
- public static function getDefaultIndexName(): string
1026
- {
1027
- return 'handler_one';
1028
- }
1040
+ return 'handler_one';
1029
1041
}
1042
+ }
1030
1043
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:
1033
1046
1034
- .. configuration-block ::
1047
+ .. configuration-block ::
1035
1048
1036
- .. code-block :: php-attributes
1049
+ .. code-block :: php-attributes
1037
1050
1038
- // src/HandlerCollection.php
1039
- namespace App;
1051
+ // src/HandlerCollection.php
1052
+ namespace App;
1040
1053
1041
- use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
1054
+ use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
1042
1055
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
+ ) {
1050
1062
}
1063
+ }
1051
1064
1052
- .. code-block :: yaml
1065
+ .. code-block :: yaml
1053
1066
1054
- # config/services.yaml
1055
- services :
1056
- # ...
1067
+ # config/services.yaml
1068
+ services :
1069
+ # ...
1057
1070
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' }]
1061
1074
1062
- .. code-block :: xml
1075
+ .. code-block :: xml
1063
1076
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" >
1070
1083
1071
- <services >
1072
- <!-- ... -->
1084
+ <services >
1085
+ <!-- ... -->
1073
1086
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 >
1083
1096
1084
- .. code-block :: php
1097
+ .. code-block :: php
1085
1098
1086
- // config/services.php
1087
- namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1099
+ // config/services.php
1100
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1088
1101
1089
- use App\HandlerCollection;
1090
- use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1102
+ use App\HandlerCollection;
1103
+ use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1091
1104
1092
- return function (ContainerConfigurator $container): void {
1093
- $services = $container->services();
1105
+ return function (ContainerConfigurator $container) {
1106
+ $services = $container->services();
1094
1107
1095
- // ...
1108
+ // ...
1096
1109
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
+ };
1104
1117
1105
1118
.. _tags_as-tagged-item :
1106
1119
0 commit comments