Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions service_container/tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<!-- ... -->

<!-- This is the service we want to exclude, even if the 'app.handler' tag is attached -->
<service id="App\Handler\Three">
<tag name="app.handler"/>
</service>

<service id="App\HandlerCollection">
<!-- inject all services tagged with app.handler as first argument -->
<argument type="tagged_iterator" tag="app.handler">
<exclude>App\Handler\Three</exclude>
</argument>
</service>
</services>
</container>

.. 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
Copy link
Contributor

@dbrumann dbrumann Jan 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right? symfony/symfony#47902 was merged into the 6.2 branch.

Nevermind, I just saw that the initial implementation was done in 6.1.


The ``exclude`` option was introduced in Symfony 6.1.

.. seealso::

See also :doc:`tagged locator services </service_container/service_subscribers_locators>`
Expand Down