Skip to content

Commit 3512e05

Browse files
committed
Tweaking the wonderful new cookbook entry about using tags in your own services
1 parent f8d7e6e commit 3512e05

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

cookbook/service_container/tags.rst

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
How to Make Your Services Use Tags
1+
.. index::
2+
single: Service Container; Tags
3+
4+
How to make your Services use Tags
25
==================================
36

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.
710

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.
1316
This post focuses mainly on the dependency injection part of the story.
1417

15-
To begin with, define the ``TransportChain`` class.
18+
To begin with, define the ``TransportChain`` class::
1619

1720
namespace Acme\MailerBundle;
1821
@@ -69,9 +72,9 @@ Then, define the chain as a service:
6972
Define Services with a Custom Tag
7073
---------------------------------
7174

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:
7578

7679
.. configuration-block::
7780

@@ -115,9 +118,9 @@ as services:
115118
$definitionSendmail->addTag('acme_mailer.transport');
116119
$container->setDefinition('acme_mailer.transport.sendmail', $definitionSendmail);
117120
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::
121124

122125
namespace Acme\MailerBundle;
123126
@@ -126,7 +129,7 @@ add a ``build()`` method to the ``AcmeMailerBundle`` class:
126129
127130
use Acme\MailerBundle\DependencyInjection\Compiler\TransportCompilerPass;
128131
129-
class AcmeMailerBundleBundle extends Bundle
132+
class AcmeMailerBundle extends Bundle
130133
{
131134
public function build(ContainerBuilder $container)
132135
{
@@ -139,10 +142,10 @@ add a ``build()`` method to the ``AcmeMailerBundle`` class:
139142
Create a ``CompilerPass``
140143
-------------------------
141144

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::
146149

147150
namespace Acme\MailerBundle\DependencyInjection\Compiler;
148151
@@ -166,24 +169,26 @@ The ``TransportCompilerPass`` should look like this:
166169
}
167170
}
168171

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.
173178

174179
.. note::
175180

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``.
179184

180185
The Compiled Service Definition
181186
-------------------------------
182187

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::
187192

188193
protected function getAcmeMailer_TransportChainService()
189194
{

0 commit comments

Comments
 (0)