Skip to content

[DependencyInjection] Mention exclude_self #19240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2023
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
88 changes: 83 additions & 5 deletions service_container/tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -842,19 +842,97 @@ 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

<!-- 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-self="false">
<exclude>App\Handler\Three</exclude>
</argument>
</service>
</services>
</container>

.. 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

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

.. 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::

Expand Down