diff --git a/service_container/tags.rst b/service_container/tags.rst index 084e0fbf4f7..f99dd5e3a2c 100644 --- a/service_container/tags.rst +++ b/service_container/tags.rst @@ -842,10 +842,88 @@ iterator, add the ``exclude`` option: ; }; -.. note:: +In the case the referencing service is itself tagged with the tag being used in the tagged +iterator, it is automatically excluded from the injected iterable. This behavior can be +disabled by setting the ``exclude_self`` option to ``false``: + +.. configuration-block:: + + .. code-block:: php-attributes + + // src/HandlerCollection.php + namespace App; + + use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; + + class HandlerCollection + { + public function __construct( + #[TaggedIterator('app.handler', exclude: ['App\Handler\Three'], excludeSelf: false)] + iterable $handlers + ) { + } + } + + .. code-block:: yaml + + # config/services.yaml + services: + # ... - In the case the referencing service is itself tagged with the tag being used in the tagged - iterator, it is automatically excluded from the injected iterable. + # This is the service we want to exclude, even if the 'app.handler' tag is attached + App\Handler\Three: + tags: ['app.handler'] + + App\HandlerCollection: + arguments: + - !tagged_iterator { tag: app.handler, exclude: ['App\Handler\Three'], exclude_self: false } + + .. code-block:: xml + + + + + + + + + + + + + + + + + App\Handler\Three + + + + + + .. code-block:: php + + // config/services.php + namespace Symfony\Component\DependencyInjection\Loader\Configurator; + + return function(ContainerConfigurator $containerConfigurator) { + $services = $containerConfigurator->services(); + + // ... + + // This is the service we want to exclude, even if the 'app.handler' tag is attached + $services->set(App\Handler\Three::class) + ->tag('app.handler') + ; + + $services->set(App\HandlerCollection::class) + // inject all services tagged with app.handler as first argument + ->args([tagged_iterator('app.handler', exclude: [App\Handler\Three::class], excludeSelf: false)]) + ; + }; .. versionadded:: 6.1 @@ -853,8 +931,8 @@ iterator, add the ``exclude`` option: .. versionadded:: 6.3 - The automatic exclusion of the referencing service in the injected iterable was - introduced in Symfony 6.3. + The ``exclude_self`` option and the automatic exclusion of the referencing + service in the injected iterable were introduced in Symfony 6.3. .. seealso::