8000 [DependencyInjection] Mention `exclude_self` · symfony/symfony-docs@8fd5554 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8fd5554

Browse files
[DependencyInjection] Mention exclude_self
1 parent 4e3a968 commit 8fd5554

File tree

1 file changed

+83
-5
lines changed

1 file changed

+83
-5
lines changed

service_container/tags.rst

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -842,19 +842,97 @@ iterator, add the ``exclude`` option:
842842
;
843843
};
844844
845-
.. note::
845+
In the case the referencing service is itself tagged with the tag being used in the tagged
846+
iterator, it is automatically excluded from the injected iterable. This behavior can be
847+
disabled by setting the ``exclude_self`` option to ``false``:
848+
849+
.. configuration-block::
850+
851+
.. code-block:: php-attributes
852+
853+
// src/HandlerCollection.php
854+
namespace App;
855+
856+
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
857+
858+
class HandlerCollection
859+
{
860+
public function __construct(
861+
#[TaggedIterator('app.handler', exclude: ['App\Handler\Three'], excludeSelf: false)]
862+
iterable $handlers
863+
) {
864+
}
865+
}
866+
867+
.. code-block:: yaml
868+
869+
# config/services.yaml
870+
services:
871+
# ...
846872
847-
In the case the referencing service is itself tagged with the tag being used in the tagged
848-
iterator, it is automatically excluded from the injected iterable.
873+
# This is the service we want to exclude, even if the 'app.handler' tag is attached
874+
App\Handler\Three:
875+
tags: ['app.handler']
876+
877+
App\HandlerCollection:
878+
arguments:
879+
- !tagged_iterator { tag: app.handler, exclude: ['App\Handler\Three'], exclude_self: false }
880+
881+
.. code-block:: xml
882+
883+
<!-- config/services.xml -->
884+
<?xml version="1.0" encoding="UTF-8" ?>
885+
<container xmlns="http://symfony.com/schema/dic/services"
886+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
887+
xsi:schemaLocation="http://symfony.com/schema/dic/services
888+
https://symfony.com/schema/dic/services/services-1.0.xsd">
889+
890+
<services>
891+
<!-- ... -->
892+
893+
<!-- This is the service we want to exclude, even if the 'app.handler' tag is attached -->
894+
<service id="App\Handler\Three">
895+
<tag name="app.handler"/>
896+
</service>
897+
898+
<service id="App\HandlerCollection">
899+
<!-- inject all services tagged with app.handler as first argument -->
900+
<argument type="tagged_iterator" tag="app.handler" exclude-self="false">
901+
<exclude>App\Handler\Three</exclude>
902+
</argument>
903+
</service>
904+
</services>
905+
</container>
906+
907+
.. code-block:: php
908+
909+
// config/services.php
910+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
911+
912+
return function(ContainerConfigurator $containerConfigurator) {
913+
$services = $containerConfigurator->services();
914+
915+
// ...
916+
917+
// This is the service we want to exclude, even if the 'app.handler' tag is attached
918+
$services->set(App\Handler\Three::class)
919+
->tag('app.handler')
920+
;
921+
922+
$services->set(App\HandlerCollection::class)
923+
// inject all services tagged with app.handler as first argument
924+
->args([tagged_iterator('app.handler', exclude: [App\Handler\Three::class], excludeSelf: false)])
925+
;
926+
};
849927
850928
.. versionadded:: 6.1
851929

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

854932
.. versionadded:: 6.3
855933

856-
The automatic exclusion of the referencing service in the injected iterable was
857-
introduced in Symfony 6.3.
934+
The ``exclude_self`` option and the automatic exclusion of the referencing
935+
service in the injected iterable were introduced in Symfony 6.3.
858936

859937
.. seealso::
860938

0 commit comments

Comments
 (0)
0