8000 Docs for referencing tagged services in config · symfony/symfony-docs@da034d2 · GitHub
[go: up one dir, main page]

Skip to content
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 da034d2

Browse files
authored
Docs for referencing tagged services in config
1 parent 103bdc2 commit da034d2

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

service_container/tags.rst

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,92 @@ The double loop may be confusing. This is because a service can have more
405405
than one tag. You tag a service twice or more with the ``app.mail_transport``
406406
tag. The second foreach loop iterates over the ``app.mail_transport``
407407
tags set for the current service and gives you the attributes.
408+
409+
Reference tagged services
410+
~~~~~~~~~~~~~~~~~~~~~~~~~
411+
412+
In case your tag doesn't require any further additional attributes writing compiler
413+
passes per tag might become tedious. A way to overcome this is is to make your compiler
414+
pass more generic. The downside of this approach is you have to write and maintain
415+
additional code, considering you want to reuse it over multiple projects.
416+
417+
ThereBecause this task is so generic and common to do, Symfony provides a way to achieve this
418+
directly in your service container confguration. This enables to inject services tagged
419+
with e.g. `app.handler` into another service that collects all handlers.
420+
421+
.. configuration-block::
422+
423+
.. code-block:: yaml
424+
425+
services:
426+
App\Handler\One:
427+
tags: [app.handler]
428+
429+
App\Handler\Two:
430+
tags: [app.handler]
431+
432+
App\HandlerCollection:
433+
arguments: [!tagged app.handler]
434+
435+
.. code-block:: xml
436+
437+
<?xml version="1.0" encoding="UTF-8" ?>
438+
<container xmlns="http://symfony.com/schema/dic/services"
439+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
440+
xsi:schemaLocation="http://symfony.com/schema/dic/services
441+
http://symfony.com/schema/dic/services/services-1.0.xsd">
442+
443+
<services>
444+
<service id="App\Handler\One">
445+
<tag name="app.handler" />
446+
</service>
447+
448+
<service id="App\Handler\Two">
449+
<tag name="app.handler" />
450+
</service>
451+
452+
<service id="App\HandlerCollection">
453+
<argument type="tagged" tag="app.handler" />
454+
</service>
455+
</services>
456+
</container>
457+
458+
.. code-block:: php
459+
460+
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
461+
462+
$container->register(\App\Handler\One::class)
463+
->addTag('app.handler');
464+
465+
$container->register(\App\Handler\One::class)
466+
->addTag('app.handler');
467+
468+
$container->register(\App\HandlerCollection::class)
469+
->addArgument(new TaggedIteratorArgument('app.handler'));
470+
471+
After compilation the `HandlerCollection` service is able to iterate over your application handlers.
472+
473+
.. code-block:: php
474+
475+
class HandlerCollection
476+
{
477+
public function __construct(iterable $handlers)
478+
{
479+
}
480+
}
481+
482+
.. tip::
483+
484+
The collected services can be prioritized using the `priority` attribute.
485+
486+
.. code-block:: yaml
487+
488+
services:
489+
App\Handler\One:
490+
tags:
491+
- { name: app.handler, priority: 20 }
492+
493+
.. versionadded:: 3.4
494+
495+
Support for the tagged service notation in YAML, XML and PHP was introduced
496+
in Symfony 3.4.

0 commit comments

Comments
 (0)
0