@@ -406,17 +406,21 @@ than one tag. You tag a service twice or more with the ``app.mail_transport``
406
406
tag. The second foreach loop iterates over the ``app.mail_transport ``
407
407
tags set for the current service and gives you the attributes.
408
408
409
- Reference tagged services
409
+ Reference Tagged Services
410
410
~~~~~~~~~~~~~~~~~~~~~~~~~
411
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 .
412
+ .. versionadded :: 3.4
413
+
414
+ Support for the tagged service notation in YAML, XML and PHP was introduced
415
+ in Symfony 3.4 .
416
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.
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:
420
424
421
425
.. configuration-block ::
422
426
@@ -430,6 +434,7 @@ with e.g. `app.handler` into another service that collects all handlers.
430
434
tags : [app.handler]
431
435
432
436
App\HandlerCollection :
437
+ # inject all services tagged with app.handler as first argument
433
438
arguments : [!tagged app.handler]
434
439
435
440
.. code-block :: xml
@@ -450,6 +455,7 @@ with e.g. `app.handler` into another service that collects all handlers.
450
455
</service >
451
456
452
457
<service id =" App\HandlerCollection" >
458
+ <!-- inject all services tagged with app.handler as first argument -->
453
459
<argument type =" tagged" tag =" app.handler" />
454
460
</service >
455
461
</services >
@@ -466,18 +472,19 @@ with e.g. `app.handler` into another service that collects all handlers.
466
472
->addTag('app.handler');
467
473
468
474
$container->register(\App\HandlerCollection::class)
475
+ // inject all services tagged with app.handler as first argument
469
476
->addArgument(new TaggedIteratorArgument('app.handler'));
470
477
471
478
After compilation the `HandlerCollection ` service is able to iterate over your application handlers.
472
479
473
- .. code-block :: php
480
+ .. code-block :: php
474
481
475
- class HandlerCollection
482
+ class HandlerCollection
483
+ {
484
+ public function __construct(iterable $handlers)
476
485
{
477
- public function __construct(iterable $handlers)
478
- {
479
- }
480
486
}
487
+ }
481
488
482
489
.. tip ::
483
490
@@ -489,8 +496,3 @@ After compilation the `HandlerCollection` service is able to iterate over your a
489
496
App\Handler\One :
490
497
tags :
491
498
- { 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