@@ -1000,97 +1000,110 @@ array element. For example, to retrieve the ``handler_two`` handler::
1000
1000
}
1001
1001
}
1002
1002
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' }]
1004
1019
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
1008
1021
1009
- // src/Handler/One.php
1010
- namespace App\Handler;
1022
+ // src/Handler/One.php
1023
+ namespace App\Handler;
1011
1024
1012
- class One
1025
+ class One
1026
+ {
1027
+ // ...
1028
+ public static function getDefaultHandlerName(): string
1013
1029
{
1014
- // ...
1015
- public static function getDefaultIndexName(): string
1016
- {
1017
- return 'handler_one';
1018
- }
1030
+ return 'handler_one';
1019
1031
}
1032
+ }
1020
1033
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:
1023
1036
1024
- .. configuration-block ::
1037
+ .. configuration-block ::
1025
1038
1026
- .. code-block :: php-attributes
1039
+ .. code-block :: php-attributes
1027
1040
1028
- // src/HandlerCollection.php
1029
- namespace App;
1041
+ // src/HandlerCollection.php
1042
+ namespace App;
1030
1043
1031
- use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
1044
+ use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
1032
1045
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
+ ) {
1040
1052
}
1053
+ }
1041
1054
1042
- .. code-block :: yaml
1055
+ .. code-block :: yaml
1043
1056
1044
- # config/services.yaml
1045
- services :
1046
- # ...
1057
+ # config/services.yaml
1058
+ services :
1059
+ # ...
1047
1060
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' }]
1051
1064
1052
- .. code-block :: xml
1065
+ .. code-block :: xml
1053
1066
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" >
1060
1073
1061
- <services >
1062
- <!-- ... -->
1074
+ <services >
1075
+ <!-- ... -->
1063
1076
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 >
1073
1086
1074
- .. code-block :: php
1087
+ .. code-block :: php
1075
1088
1076
- // config/services.php
1077
- namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1089
+ // config/services.php
1090
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1078
1091
1079
- use App\HandlerCollection;
1080
- use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1092
+ use App\HandlerCollection;
1093
+ use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1081
1094
1082
- return function (ContainerConfigurator $container): void {
1083
- $services = $container->services();
1095
+ return function (ContainerConfigurator $container) {
1096
+ $services = $container->services();
1084
1097
1085
- // ...
1098
+ // ...
1086
1099
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
+ };
1094
1107
1095
1108
.. _tags_as-tagged-item :
1096
1109
0 commit comments