diff --git a/service_container/tags.rst b/service_container/tags.rst index 9c89ef8c85a..e5e996a076e 100644 --- a/service_container/tags.rst +++ b/service_container/tags.rst @@ -591,6 +591,76 @@ application handlers:: } } +If for some reason you need to exclude one or multiple services when using a tagged +iterator, this can be done by using the ``exclude`` option: + +.. configuration-block:: + + .. code-block:: yaml + + # config/services.yaml + services: + # ... + + # 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'] } + + .. code-block:: xml + + + + + + + + + + + + + + + + + App\Handler\Three + + + + + + .. code-block:: php + + // config/services.php + namespace Symfony\Component\DependencyInjection\Loader\Configurator; + + return function(ContainerConfigurator $configurator) { + $services = $configurator->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])]) + ; + }; + +.. versionadded:: 6.1 + + The ``exclude`` option was introduced in Symfony 6.1. + .. seealso:: See also :doc:`tagged locator services `