Skip to content

Commit a5f0eec

Browse files
committed
Documented the "auto_alias" feature
1 parent 6d17756 commit a5f0eec

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

reference/dic_tags.rst

+100
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Tag Name Usage
2222
`assetic.formula_resource`_ Adds a resource to the current asset manager
2323
`assetic.templating.php`_ Remove this service if PHP templating is disabled
2424
`assetic.templating.twig`_ Remove this service if Twig templating is disabled
25+
`auto_alias`_ Define aliases based on the value of container parameters
2526
`console.command`_ Add a command
2627
`data_collector`_ Create a class that collects custom data for the profiler
2728
`doctrine.event_listener`_ Add a Doctrine event listener
@@ -227,6 +228,105 @@ assetic.templating.twig
227228
The tagged service will be removed from the container if
228229
``framework.templating.engines`` config section does not contain ``twig``.
229230

231+
auto_alias
232+
----------
233+
234+
**Purpose**: Define aliases based on the value of container parameters
235+
236+
Consider the following configuration that defines three different but related
237+
services:
238+
239+
.. configuration-block::
240+
241+
.. code-block:: yaml
242+
243+
services:
244+
app.mysql_lock:
245+
class: AppBundle\Lock\MysqlLock
246+
app.postgresql_lock:
247+
class: AppBundle\Lock\PostgresqlLock
248+
app.sqlite_lock:
249+
class: AppBundle\Lock\SqliteLock
250+
251+
252+
.. code-block:: xml
253+
254+
<?xml version="1.0" encoding="UTF-8" ?>
255+
<container xmlns="http://symfony.com/schema/dic/services"
256+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
257+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
258+
259+
<services>
260+
<service id="app.mysql_lock" class="AppBundle\Lock\MysqlLock" />
261+
<service id="app.postgresql_lock" class="AppBundle\Lock\PostgresqlLock" />
262+
<service id="app.sqlite_lock" class="AppBundle\Lock\SqliteLock" />
263+
</services>
264+
</container>
265+
266+
.. code-block:: php
267+
268+
$container
269+
->register('app.mysql_lock', 'AppBundle\Lock\MysqlLock')
270+
->register('app.postgresql_lock', 'AppBundle\Lock\PostgresqlLock')
271+
->register('app.sqlite_lock', 'AppBundle\Lock\SqliteLock')
272+
;
273+
274+
Instead of dealing with these three services, your application needs a generic
275+
``app.lock`` service. This service must be an alias to any of the other services.
276+
Thanks to the ``auto_alias`` option, you can automatically create that alias
277+
based on the value of a configuration parameter.
278+
279+
Considering that a configuration parameter called ``database_type`` exists,
280+
the generic ``app.lock`` service can be defined as follows:
281+
282+
.. configuration-block::
283+
284+
.. code-block:: yaml
285+
286+
services:
287+
app.mysql_lock:
288+
class: AppBundle\Lock\MysqlLock
289+
app.postgresql_lock:
290+
class: AppBundle\Lock\PostgresqlLock
291+
app.sqlite_lock:
292+
class: AppBundle\Lock\SqliteLock
293+
app.lock:
294+
tags:
295+
- { name: auto_alias, format: "%database_type%.lock" }
296+
297+
.. code-block:: xml
298+
299+
<?xml version="1.0" encoding="UTF-8" ?>
300+
<container xmlns="http://symfony.com/schema/dic/services"
301+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
302+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
303+
304+
<services>
305+
<service id="app.mysql_lock" class="AppBundle\Lock\MysqlLock" />
306+
<service id="app.postgresql_lock" class="AppBundle\Lock\PostgresqlLock" />
307+
<service id="app.sqlite_lock" class="AppBundle\Lock\SqliteLock" />
308+
309+
<service id="app.lock">
310+
<tag name="auto_alias" format="%database_type%.lock" />
311+
</service>
312+
</services>
313+
</container>
314+
315+
.. code-block:: php
316+
317+
$container
318+
->register('app.mysql_lock', 'AppBundle\Lock\MysqlLock')
319+
->register('app.postgresql_lock', 'AppBundle\Lock\PostgresqlLock')
320+
->register('app.sqlite_lock', 'AppBundle\Lock\SqliteLock')
321+
322+
->register('app.lock')
323+
->addTag('auto_alias', array('format' => '%database_type%.lock'))
324+
;
325+
326+
The ``format`` parameter defines the expression used to construct the name of
327+
the service to alias. This expression can use any container parameter (as usual,
328+
wrapping their names with ``%`` characters).
329+
230330
console.command
231331
---------------
232332

0 commit comments

Comments
 (0)