@@ -14,7 +14,7 @@ This post focuses mainly on the dependency injection part of the story.
14
14
15
15
To begin with, define the ``TransportChain `` class.
16
16
17
- namespace Acme\T ransportBundle ;
17
+ namespace Acme\M ailerBundle ;
18
18
19
19
class TransportChain
20
20
{
@@ -37,34 +37,34 @@ Then, define the chain as a service:
37
37
38
38
.. code-block :: yaml
39
39
40
- # src/Acme/TransportBundle /Resources/config/services.yml
40
+ # src/Acme/MailerBundle /Resources/config/services.yml
41
41
parameters :
42
- transport_chain.class : Acme\TransportBundle \TransportChain
42
+ acme_mailer. transport_chain.class : Acme\MailerBundle \TransportChain
43
43
44
44
services :
45
- transport_chain :
46
- class : %transport_chain.class%
45
+ acme_mailer. transport_chain :
46
+ class : %acme_mailer. transport_chain.class%
47
47
48
48
.. code-block :: xml
49
49
50
- <!-- src/Acme/TransportBundle /Resources/config/services.xml -->
50
+ <!-- src/Acme/MailerBundle /Resources/config/services.xml -->
51
51
52
52
<parameters >
53
- <parameter key =" transport_chain.class" >Acme\TransportBundle \TransportChain</parameter >
53
+ <parameter key =" acme_mailer. transport_chain.class" >Acme\MailerBundle \TransportChain</parameter >
54
54
</parameters >
55
55
56
56
<services >
57
- <service id =" transport_chain" class =" %transport_chain.class%" />
57
+ <service id =" acme_mailer. transport_chain" class =" %acme_mailer. transport_chain.class%" />
58
58
</services >
59
59
60
60
.. code-block :: php
61
61
62
- // src/Acme/TransportBundle /Resources/config/services.php
62
+ // src/Acme/MailerBundle /Resources/config/services.php
63
63
use Symfony\Component\DependencyInjection\Definition;
64
64
65
- $container->setParameter('transport_chain.class', 'Acme\TransportBundle \TransportChain');
65
+ $container->setParameter('acme_mailer. transport_chain.class', 'Acme\MailerBundle \TransportChain');
66
66
67
- $container->setDefinition('transport_chain', new Definition('%transport_chain.class'));
67
+ $container->setDefinition('acme_mailer. transport_chain', new Definition('%acme_mailer. transport_chain.class% '));
68
68
69
69
Define Services with a Custom Tag
70
70
---------------------------------
@@ -76,55 +76,57 @@ as services:
76
76
.. configuration-block ::
77
77
78
78
.. code-block :: yaml
79
-
79
+
80
+ # src/Acme/MailerBundle/Resources/config/services.yml
80
81
services :
81
- transport.smtp :
82
+ acme_mailer. transport.smtp :
82
83
class : \Swift_SmtpTransport
83
84
arguments :
84
85
- %mailer_host%
85
86
tags :
86
- - { name: mailer .transport }
87
- transport.sendmail :
87
+ - { name: acme_mailer .transport }
88
+ acme_mailer. transport.sendmail :
88
89
class : \Swift_SendmailTransport
89
90
tags :
90
- - { name: mailer .transport }
91
+ - { name: acme_mailer .transport }
91
92
92
93
.. code-block :: xml
93
94
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" >
95
97
<argument >%mailer_host%</argument >
96
- <tag name =" mailer .transport" />
98
+ <tag name =" acme_mailer .transport" />
97
99
</service >
98
100
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" />
101
103
</service >
102
104
103
105
.. code-block :: php
104
106
105
- // src/Acme/TransportBundle /Resources/config/services.php
107
+ // src/Acme/MailerBundle /Resources/config/services.php
106
108
use Symfony\Component\DependencyInjection\Definition;
107
109
108
110
$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);
111
113
112
114
$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);
115
117
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
117
119
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:
119
121
120
- namespace Acme\T ransportBundle ;
122
+ namespace Acme\M ailerBundle ;
121
123
122
124
use Symfony\C omponent\H ttpKernel\B undle\B undle;
123
125
use Symfony\C omponent\D ependencyInjection\C ontainerBuilder;
124
126
125
- use Acme\T ransportBundle \D ependencyInjection\C ompiler\T ransportCompilerPass;
127
+ use Acme\M ailerBundle \D ependencyInjection\C ompiler\T ransportCompilerPass;
126
128
127
- class AcmeTransportBundle extends Bundle
129
+ class AcmeMailerBundleBundle extends Bundle
128
130
{
129
131
public function build(ContainerBuilder $container)
130
132
{
@@ -138,11 +140,11 @@ Create a ``CompilerPass``
138
140
-------------------------
139
141
140
142
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
142
144
the ``TransportChain `` class by calling the ``addTransport() `` method.
143
145
The ``TransportCompilerPass `` should look like this:
144
146
145
- namespace Acme\T ransportBundle \D ependencyInjection\C ompiler;
147
+ namespace Acme\M ailerBundle \D ependencyInjection\C ompiler;
146
148
147
149
use Symfony\C omponent\D ependencyInjection\C ontainerBuilder;
148
150
use Symfony\C omponent\D ependencyInjection\C ompiler\C ompilerPassInterface;
@@ -152,23 +154,29 @@ The ``TransportCompilerPass`` should look like this:
152
154
{
153
155
public function process(ContainerBuilder $container)
154
156
{
155
- if (false === $container->hasDefinition('transport_chain')) {
157
+ if (false === $container->hasDefinition('acme_mailer. transport_chain')) {
156
158
return;
157
159
}
158
160
159
- $definition = $container->getDefinition('transport_chain');
161
+ $definition = $container->getDefinition('acme_mailer. transport_chain');
160
162
161
- foreach ($container->findTaggedServiceIds('mailer .transport') as $id => $attributes) {
163
+ foreach ($container->findTaggedServiceIds('acme_mailer .transport') as $id => $attributes) {
162
164
$definition->addMethodCall('addTransport', array(new Reference($id)));
163
165
}
164
166
}
165
167
}
166
168
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
170
172
it has found. The first argument of each of these calls will be the mailer transport service itself.
171
173
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
+
172
180
The Compiled Service Definition
173
181
-------------------------------
174
182
@@ -177,12 +185,12 @@ in the compiled service container. In case you are working in the "dev" environm
177
185
``/cache/dev/appDevDebugProjectContainer.php `` and look for the method ``getTransportChainService() ``.
178
186
It should look like this:
179
187
180
- protected function getTransportChainService ()
188
+ protected function getAcmeMailer_TransportChainService ()
181
189
{
182
- $this->services['transport_chain'] = $instance = new \A cme\T ransportBundle \T ransportChain();
190
+ $this->services['acme_mailer. transport_chain'] = $instance = new \A cme\M ailerBundle \T ransportChain();
183
191
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'));
186
194
187
195
return $instance;
188
196
}
0 commit comments