1
- How to Make Your Services Use Tags
1
+ .. index ::
2
+ single: Service Container; Tags
3
+
4
+ How to make your Services use Tags
2
5
==================================
3
6
4
- Several of Symfony2's core services depend on tags to recognize which services
5
- should be loaded, notified of events, etc. For example, Twig uses the tag
6
- ``twig.extension `` to load extra extensions.
7
+ Several of Symfony2's core services depend on tags to recognize which services
8
+ should be loaded, notified of events, or handled in some other special way.
9
+ For example, Twig uses the tag ``twig.extension `` to load extra extensions.
7
10
8
- But you can also use tags in your own bundles. For example in case your service
9
- handles a collection of some kind, or implements a "chain", in which several alternative
10
- strategies are tried until one of them is successful. In this article I will use the example
11
- of a "transport chain", which is a collection of classes implementing ``\Swift_Transport ``.
12
- Using the chain, the Swift mailer may try several ways of transport, until one succeeds.
11
+ But you can also use tags in your own bundles. For example in case your service
12
+ handles a collection of some kind, or implements a "chain", in which several alternative
13
+ strategies are tried until one of them is successful. In this article I will use the example
14
+ of a "transport chain", which is a collection of classes implementing ``\Swift_Transport ``.
15
+ Using the chain, the Swift mailer may try several ways of transport, until one succeeds.
13
16
This post focuses mainly on the dependency injection part of the story.
14
17
15
- To begin with, define the ``TransportChain `` class.
18
+ To begin with, define the ``TransportChain `` class::
16
19
17
20
namespace Acme\MailerBundle;
18
21
@@ -69,9 +72,9 @@ Then, define the chain as a service:
69
72
Define Services with a Custom Tag
70
73
---------------------------------
71
74
72
- Now we want several of the ``\Swift_Transport `` classes to be instantiated and added to the chain
73
- automatically using the ``addTransport() `` method. As an example we add the following transports
74
- as services:
75
+ Now we want several of the ``\Swift_Transport `` classes to be instantiated
76
+ and added to the chain automatically using the ``addTransport() `` method.
77
+ As an example we add the following transports as services:
75
78
76
79
.. configuration-block ::
77
80
@@ -115,9 +118,9 @@ as services:
115
118
$definitionSendmail->addTag('acme_mailer.transport');
116
119
$container->setDefinition('acme_mailer.transport.sendmail', $definitionSendmail);
117
120
118
- Notice the tags named "acme_mailer.transport". We want the bundle to recognize these transports
119
- and add them to the chain all by itself. In order to achieve this, we need to
120
- add a ``build() `` method to the ``AcmeMailerBundle `` class:
121
+ Notice the tags named "acme_mailer.transport". We want the bundle to recognize
122
+ these transports and add them to the chain all by itself. In order to achieve
123
+ this, we need to add a ``build() `` method to the ``AcmeMailerBundle `` class: :
121
124
122
125
namespace Acme\MailerBundle;
123
126
@@ -126,7 +129,7 @@ add a ``build()`` method to the ``AcmeMailerBundle`` class:
126
129
127
130
use Acme\MailerBundle\DependencyInjection\Compiler\TransportCompilerPass;
128
131
129
- class AcmeMailerBundleBundle extends Bundle
132
+ class AcmeMailerBundle extends Bundle
130
133
{
131
134
public function build(ContainerBuilder $container)
132
135
{
@@ -139,10 +142,10 @@ add a ``build()`` method to the ``AcmeMailerBundle`` class:
139
142
Create a ``CompilerPass ``
140
143
-------------------------
141
144
142
- You will have spotted a reference to the not yet existing ``TransportCompilerPass `` class.
143
- This class will make sure that all services with a tag " acme_mailer.transport" will be added to
144
- the ``TransportChain `` class by calling the ``addTransport() `` method.
145
- The ``TransportCompilerPass `` should look like this:
145
+ You will have spotted a reference to the not yet existing ``TransportCompilerPass `` class.
146
+ This class will make sure that all services with a tag `` acme_mailer.transport ``
147
+ will be added to the ``TransportChain `` class by calling the ``addTransport() ``
148
+ method. The ``TransportCompilerPass `` should look like this: :
146
149
147
150
namespace Acme\MailerBundle\DependencyInjection\Compiler;
148
151
@@ -166,24 +169,26 @@ The ``TransportCompilerPass`` should look like this:
166
169
}
167
170
}
168
171
169
- The ``process() `` method checks for the existence of the ``acme_mailer.transport_chain `` service,
170
- then looks for all services tagged "acme_mailer.transport". It adds to the definition of the
171
- ``acme_mailer.transport_chain `` service a call to ``addTransport() `` for each "acme_mailer.transport" service
172
- it has found. The first argument of each of these calls will be the mailer transport service itself.
172
+ The ``process() `` method checks for the existence of the ``acme_mailer.transport_chain ``
173
+ service, then looks for all services tagged ``acme_mailer.transport ``. It adds
174
+ to the definition of the ``acme_mailer.transport_chain `` service a call to
175
+ ``addTransport() `` for each "acme_mailer.transport" service it has found.
176
+ The first argument of each of these calls will be the mailer transport service
177
+ itself.
173
178
174
179
.. note ::
175
180
176
- By convention, tag names consist of the name of the bundle (lowercase, underscores as separators),
177
- followed by a dot, and finally the "real" name, so the tag "transport" in the AcmeMailerBundle should be:
178
- " acme_mailer.transport" .
181
+ By convention, tag names consist of the name of the bundle (lowercase,
182
+ underscores as separators), followed by a dot, and finally the "real"
183
+ name, so the tag "transport" in the AcmeMailerBundle should be: `` acme_mailer.transport `` .
179
184
180
185
The Compiled Service Definition
181
186
-------------------------------
182
187
183
- Adding the compiler pass will result in the automatic generation of the following lines of code
184
- in the compiled service container. In case you are working in the "dev" environment, open the file
185
- ``/cache/dev/appDevDebugProjectContainer.php `` and look for the method `` getTransportChainService() ``.
186
- It should look like this:
188
+ Adding the compiler pass will result in the automatic generation of the following
189
+ lines of code in the compiled service container. In case you are working
190
+ in the "dev" environment, open the file ``/cache/dev/appDevDebugProjectContainer.php ``
191
+ and look for the method `` getTransportChainService() ``. It should look like this: :
187
192
188
193
protected function getAcmeMailer_TransportChainService()
189
194
{
0 commit comments