Skip to content

Commit f8d7e6e

Browse files
matthiasnobackweaverryan
authored andcommitted
Renamed TransportBundle to MailerBundle, then updated all references and renamed the services and parameters to conform to the naming convention (as pointed out by @stof). Also added a note about this convention.
1 parent b183b93 commit f8d7e6e

File tree

1 file changed

+50
-42
lines changed

1 file changed

+50
-42
lines changed

cookbook/service_container/tags.rst

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This post focuses mainly on the dependency injection part of the story.
1414

1515
To begin with, define the ``TransportChain`` class.
1616

17-
namespace Acme\TransportBundle;
17+
namespace Acme\MailerBundle;
1818

1919
class TransportChain
2020
{
@@ -37,34 +37,34 @@ Then, define the chain as a service:
3737

3838
.. code-block:: yaml
3939
40-
# src/Acme/TransportBundle/Resources/config/services.yml
40+
# src/Acme/MailerBundle/Resources/config/services.yml
4141
parameters:
42-
transport_chain.class: Acme\TransportBundle\TransportChain
42+
acme_mailer.transport_chain.class: Acme\MailerBundle\TransportChain
4343
4444
services:
45-
transport_chain:
46-
class: %transport_chain.class%
45+
acme_mailer.transport_chain:
46+
class: %acme_mailer.transport_chain.class%
4747
4848
.. code-block:: xml
4949
50-
<!-- src/Acme/TransportBundle/Resources/config/services.xml -->
50+
<!-- src/Acme/MailerBundle/Resources/config/services.xml -->
5151
5252
<parameters>
53-
<parameter key="transport_chain.class">Acme\TransportBundle\TransportChain</parameter>
53+
<parameter key="acme_mailer.transport_chain.class">Acme\MailerBundle\TransportChain</parameter>
5454
</parameters>
5555
5656
<services>
57-
<service id="transport_chain" class="%transport_chain.class%" />
57+
<service id="acme_mailer.transport_chain" class="%acme_mailer.transport_chain.class%" />
5858
</services>
5959
6060
.. code-block:: php
6161
62-
// src/Acme/TransportBundle/Resources/config/services.php
62+
// src/Acme/MailerBundle/Resources/config/services.php
6363
use Symfony\Component\DependencyInjection\Definition;
6464
65-
$container->setParameter('transport_chain.class', 'Acme\TransportBundle\TransportChain');
65+
$container->setParameter('acme_mailer.transport_chain.class', 'Acme\MailerBundle\TransportChain');
6666
67-
$container->setDefinition('transport_chain', new Definition('%transport_chain.class'));
67+
$container->setDefinition('acme_mailer.transport_chain', new Definition('%acme_mailer.transport_chain.class%'));
6868
6969
Define Services with a Custom Tag
7070
---------------------------------
@@ -76,55 +76,57 @@ as services:
7676
.. configuration-block::
7777

7878
.. code-block:: yaml
79-
79+
80+
# src/Acme/MailerBundle/Resources/config/services.yml
8081
services:
81-
transport.smtp:
82+
acme_mailer.transport.smtp:
8283
class: \Swift_SmtpTransport
8384
arguments:
8485
- %mailer_host%
8586
tags:
86-
- { name: mailer.transport }
87-
transport.sendmail:
87+
- { name: acme_mailer.transport }
88+
acme_mailer.transport.sendmail:
8889
class: \Swift_SendmailTransport
8990
tags:
90-
- { name: mailer.transport }
91+
- { name: acme_mailer.transport }
9192
9293
.. code-block:: xml
9394
94-
<service id="transport.smtp" class="\Swift_SmtpTransport">
95+
<!-- src/Acme/MailerBundle/Resources/config/services.xml -->
96+
<service id="acme_mailer.transport.smtp" class="\Swift_SmtpTransport">
9597
<argument>%mailer_host%</argument>
96-
<tag name="mailer.transport" />
98+
<tag name="acme_mailer.transport" />
9799
</service>
98100
99-
<service id="transport.sendmail" class="\Swift_SendmailTransport">
100-
<tag name="mailer.transport" />
101+
<service id="acme_mailer.transport.sendmail" class="\Swift_SendmailTransport">
102+
<tag name="acme_mailer.transport" />
101103
</service>
102104
103105
.. code-block:: php
104106
105-
// src/Acme/TransportBundle/Resources/config/services.php
107+
// src/Acme/MailerBundle/Resources/config/services.php
106108
use Symfony\Component\DependencyInjection\Definition;
107109
108110
$definitionSmtp = new Definition('\Swift_SmtpTransport', array('%mailer_host%'));
109-
$definitionSmtp->addTag('mailer.transport');
110-
$container->setDefinition('transport.smtp', $definitionSmtp);
111+
$definitionSmtp->addTag('acme_mailer.transport');
112+
$container->setDefinition('acme_mailer.transport.smtp', $definitionSmtp);
111113
112114
$definitionSendmail = new Definition('\Swift_SendmailTransport');
113-
$definitionSendmail->addTag('mailer.transport');
114-
$container->setDefinition('transport.sendmail', $definitionSendmail);
115+
$definitionSendmail->addTag('acme_mailer.transport');
116+
$container->setDefinition('acme_mailer.transport.sendmail', $definitionSendmail);
115117
116-
Notice the tags named "mailer.transport". We want the bundle to recognize these transports
118+
Notice the tags named "acme_mailer.transport". We want the bundle to recognize these transports
117119
and add them to the chain all by itself. In order to achieve this, we need to
118-
add a ``build()`` method to the ``AcmeTransportBundle`` class:
120+
add a ``build()`` method to the ``AcmeMailerBundle`` class:
119121

120-
namespace Acme\TransportBundle;
122+
namespace Acme\MailerBundle;
121123

122124
use Symfony\Component\HttpKernel\Bundle\Bundle;
123125
use Symfony\Component\DependencyInjection\ContainerBuilder;
124126

125-
use Acme\TransportBundle\DependencyInjection\Compiler\TransportCompilerPass;
127+
use Acme\MailerBundle\DependencyInjection\Compiler\TransportCompilerPass;
126128

127-
class AcmeTransportBundle extends Bundle
129+
class AcmeMailerBundleBundle extends Bundle
128130
{
129131
public function build(ContainerBuilder $container)
130132
{
@@ -138,11 +140,11 @@ Create a ``CompilerPass``
138140
-------------------------
139141

140142
You will have spotted a reference to the not yet existing ``TransportCompilerPass`` class.
141-
This class will make sure that all services with a tag "mailer.transport" will be added to
143+
This class will make sure that all services with a tag "acme_mailer.transport" will be added to
142144
the ``TransportChain`` class by calling the ``addTransport()`` method.
143145
The ``TransportCompilerPass`` should look like this:
144146

145-
namespace Acme\TransportBundle\DependencyInjection\Compiler;
147+
namespace Acme\MailerBundle\DependencyInjection\Compiler;
146148

147149
use Symfony\Component\DependencyInjection\ContainerBuilder;
148150
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
@@ -152,23 +154,29 @@ The ``TransportCompilerPass`` should look like this:
152154
{
153155
public function process(ContainerBuilder $container)
154156
{
155-
if (false === $container->hasDefinition('transport_chain')) {
157+
if (false === $container->hasDefinition('acme_mailer.transport_chain')) {
156158
return;
157159
}
158160

159-
$definition = $container->getDefinition('transport_chain');
161+
$definition = $container->getDefinition('acme_mailer.transport_chain');
160162

161-
foreach ($container->findTaggedServiceIds('mailer.transport') as $id => $attributes) {
163+
foreach ($container->findTaggedServiceIds('acme_mailer.transport') as $id => $attributes) {
162164
$definition->addMethodCall('addTransport', array(new Reference($id)));
163165
}
164166
}
165167
}
166168

167-
The ``process()`` method checks for the existence of the ``transport_chain`` service,
168-
then looks for all services tagged "mailer.transport". It adds to the definition of the
169-
``transport_chain`` service a call to ``addTransport()`` for each "mailer.transport" service
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
170172
it has found. The first argument of each of these calls will be the mailer transport service itself.
171173

174+
.. note::
175+
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".
179+
172180
The Compiled Service Definition
173181
-------------------------------
174182

@@ -177,12 +185,12 @@ in the compiled service container. In case you are working in the "dev" environm
177185
``/cache/dev/appDevDebugProjectContainer.php`` and look for the method ``getTransportChainService()``.
178186
It should look like this:
179187

180-
protected function getTransportChainService()
188+
protected function getAcmeMailer_TransportChainService()
181189
{
182-
$this->services['transport_chain'] = $instance = new \Acme\TransportBundle\TransportChain();
190+
$this->services['acme_mailer.transport_chain'] = $instance = new \Acme\MailerBundle\TransportChain();
183191

184-
$instance->addTransport($this->get('transport.smtp'));
185-
$instance->addTransport($this->get('transport.sendmail'));
192+
$instance->addTransport($this->get('acme_mailer.transport.smtp'));
193+
$instance->addTransport($this->get('acme_mailer.transport.sendmail'));
186194

187195
return $instance;
188196
}

0 commit comments

Comments
 (0)