|
4 | 4 | How to make your Services use Tags
|
5 | 5 | ==================================
|
6 | 6 |
|
7 |
| -You can ask a container builder for any services that were tagged when registered |
8 |
| -with it. This is useful in compiler passes where you can find services which |
9 |
| -were registered in config files you do not have control over and make use |
10 |
| -of them. This is useful if your service handles a collection of some kind, |
11 |
| -or implements a "chain", in which several alternative strategies are tried |
12 |
| -until one of them is successful. You can then allow services to be registered |
13 |
| -with a tag and add these services to your collection or chain. |
14 |
| - |
15 |
| -For example, if you are using Swift Mailer and want to implement a |
16 |
| -of a "transport chain", which is a collection of classes implementing ``\Swift_Transport``. |
17 |
| -Using the chain, Swift Mailer can try several ways of transport, until one |
18 |
| -succeeds. |
| 7 | +Tags are a generic string (along with some options) that can be applied to |
| 8 | +any service. By themselves, tags don't actually the functionality of your |
| 9 | +services in any way. But if you choose to, you can ask a container builder |
| 10 | +for a list of all services that were tagged with some specific tag. This |
| 11 | +is useful in compiler passes where you can find these services and use or |
| 12 | +modify them in some specific way. |
| 13 | + |
| 14 | +For example, if you are using Swift Mailer you might imagine that you want |
| 15 | +to implement a "transport chain", which is a collection of classes implementing |
| 16 | +``\Swift_Transport``. Using the chain, you'll want Swift Mailer to try several |
| 17 | +ways of transporting the message until one succeeds. |
19 | 18 |
|
20 | 19 | To begin with, define the ``TransportChain`` class::
|
21 | 20 |
|
@@ -111,12 +110,14 @@ As an example we add the following transports as services:
|
111 | 110 | $definitionSendmail->addTag('acme_mailer.transport');
|
112 | 111 | $container->setDefinition('acme_mailer.transport.sendmail', $definitionSendmail);
|
113 | 112 |
|
114 |
| -Notice the tags named "acme_mailer.transport". This is the custom tag to use ion your compiler pass:: |
| 113 | +Notice that each was given a tag named ``acme_mailer.transport``. This is |
| 114 | +the custom tag that you'll use in your compiler pass. The compiler pass |
| 115 | +is what makes this tag "mean" something. |
115 | 116 |
|
116 | 117 | Create a ``CompilerPass``
|
117 | 118 | -------------------------
|
118 | 119 |
|
119 |
| -Your compiler pass can then ask the container for any services with the |
| 120 | +Your compiler pass can now ask the container for any services with the |
120 | 121 | custom tag::
|
121 | 122 |
|
122 | 123 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
@@ -146,7 +147,7 @@ to the definition of the ``acme_mailer.transport_chain`` service a call to
|
146 | 147 | The first argument of each of these calls will be the mailer transport service
|
147 | 148 | itself.
|
148 | 149 |
|
149 |
| -Register the pass with the container |
| 150 | +Register the Pass with the Container |
150 | 151 | ------------------------------------
|
151 | 152 |
|
152 | 153 | You also need to register the pass with the container, it will then be
|
|
0 commit comments