Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61c74da

Browse files
authored
Update tags.rst
1 parent da034d2 commit 61c74da

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

service_container/tags.rst

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -406,17 +406,21 @@ 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.
408408

409-
Reference tagged services
409+
Reference Tagged Services
410410
~~~~~~~~~~~~~~~~~~~~~~~~~
411411

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.
412+
.. versionadded:: 3.4
413+
414+
Support for the tagged service notation in YAML, XML and PHP was introduced
415+
in Symfony 3.4.
416416

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.
417+
If you use tags to inject a list of services as an argument, writing a compiler
418+
pass is a bit tedious. As this is a very common case, Symfony provides a way to
419+
inject all services tagged with a specific tag.
420+
421+
The downside of this feature is that you can't have any custom attributes. In the
422+
example below, all services tagged with app.handler are passed in an array as the
423+
first constructor argument to the ``App\HandlerCollection`` service:
420424

421425
.. configuration-block::
422426

@@ -430,6 +434,7 @@ with e.g. `app.handler` into another service that collects all handlers.
430434
tags: [app.handler]
431435
432436
App\HandlerCollection:
437+
# inject all services tagged with app.handler as first argument
433438
arguments: [!tagged app.handler]
434439
435440
.. code-block:: xml
@@ -450,6 +455,7 @@ with e.g. `app.handler` into another service that collects all handlers.
450455
</service>
451456
452457
<service id="App\HandlerCollection">
458+
<!-- inject all services tagged with app.handler as first argument -->
453459
<argument type="tagged" tag="app.handler" />
454460
</service>
455461
</services>
@@ -466,18 +472,19 @@ with e.g. `app.handler` into another service that collects all handlers.
466472
->addTag('app.handler');
467473
468474
$container->register(\App\HandlerCollection::class)
475+
// inject all services tagged with app.handler as first argument
469476
->addArgument(new TaggedIteratorArgument('app.handler'));
470477
471478
After compilation the `HandlerCollection` service is able to iterate over your application handlers.
472479

473-
.. code-block:: php
480+
.. code-block:: php
474481
475-
class HandlerCollection
482+
class HandlerCollection
483+
{
484+
public function __construct(iterable $handlers)
476485
{
477-
public function __construct(iterable $handlers)
478-
{
479-
}
480486
}
487+
}
481488
482489
.. tip::
483490

@@ -489,8 +496,3 @@ After compilation the `HandlerCollection` service is able to iterate over your a
489496
App\Handler\One:
490497
tags:
491498
- { 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)