Skip to content

Commit 1c98e4c

Browse files
committed
Minor tweaks to PR #1471
1 parent 07678e5 commit 1c98e4c

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

components/dependency_injection/tags.rst

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Adding additional attributes on Tags
162162
------------------------------------
163163

164164
Sometimes you need additional information about each service that's tagged with your tag.
165-
For example, add an alias to our TransportChain
165+
For example, you might want to add an alias to each TransportChain.
166166

167167
To begin with, change the ``TransportChain`` class::
168168

@@ -175,7 +175,7 @@ To begin with, change the ``TransportChain`` class::
175175
$this->transports = array();
176176
}
177177

178-
public function addTransport(\Swift_Transport $transport, $alias)
178+
public function addTransport(\Swift_Transport $transport, $alias)
179179
{
180180
$this->transports[$alias] = $transport;
181181
}
@@ -190,9 +190,12 @@ To begin with, change the ``TransportChain`` class::
190190
}
191191
}
192192
}
193-
194193

195-
Change the service delaration:
194+
As you can see, when ``addTransport`` is called, it takes not only a ``Swift_Transport``
195+
object, but also a string alias for that transport. So, how can we allow
196+
each tagged transport service to also supply an alias?
197+
198+
To answer this, change the service declaration:
196199

197200
.. configuration-block::
198201

@@ -222,8 +225,8 @@ Change the service delaration:
222225
<tag name="acme_mailer.transport" alias="bar" />
223226
</service>
224227
225-
226-
The last step is to update the compiler to take care of your additional information::
228+
Notice that you've added a generic ``alias`` key to the tag. To actually
229+
use this, update the compiler::
227230

228231
use Symfony\Component\DependencyInjection\ContainerBuilder;
229232
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
@@ -246,6 +249,8 @@ The last step is to update the compiler to take care of your additional informat
246249
}
247250
}
248251
}
249-
250-
Take care of ``$attributes`` variable. Because you can use the same tag many times on the same service,
251-
``$attributes`` is an array of attributes for each tag that refers to the same service.
252+
253+
The trickiest part is the ``$attributes`` variable. Because you can use the
254+
same tag many times on the same service (e.g. you could theoretically tag
255+
the same service 5 times with the ``acme_mailer.transport`` tag), ``$attributes``
256+
is an array of the tag information for each tag on that service.

0 commit comments

Comments
 (0)