Skip to content

Commit 963ed7f

Browse files
committed
feature #5444 Documented the "auto_alias" feature (javiereguiluz)
This PR was merged into the 2.7 branch. Discussion ---------- Documented the "auto_alias" feature | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | 2.7+ | Fixed tickets | #4992 Commits ------- 1524f5a Fixed an error in the auto_alias format value bab745d Minor grammar issue 4d0f6ea Minor fixes 4c50cb0 Fixed some errors and added a new note e24f77e Removed an extra blank line 69152e7 Added the "versionadded: 2.7" directive a5f0eec Documented the "auto_alias" feature
2 parents 9755846 + 1524f5a commit 963ed7f

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

reference/dic_tags.rst

Lines changed: 115 additions & 0 deletions
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,120 @@ 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+
.. versionadded:: 2.7
235+
The ``auto_alias`` tag was introduced in Symfony 2.7.
236+
237+
**Purpose**: Define aliases based on the value of container parameters
238+
239+
Consider the following configuration that defines three different but related
240+
services:
241+
242+
.. configuration-block::
243+
244+
.. code-block:: yaml
245+
246+
services:
247+
app.mysql_lock:
248+
class: AppBundle\Lock\MysqlLock
249+
app.postgresql_lock:
250+
class: AppBundle\Lock\PostgresqlLock
251+
app.sqlite_lock:
252+
class: AppBundle\Lock\SqliteLock
253+
254+
.. code-block:: xml
255+
256+
<?xml version="1.0" encoding="UTF-8" ?>
257+
<container xmlns="http://symfony.com/schema/dic/services"
258+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
259+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
260+
261+
<services>
262+
<service id="app.mysql_lock" class="AppBundle\Lock\MysqlLock" />
263+
<service id="app.postgresql_lock" class="AppBundle\Lock\PostgresqlLock" />
264+
<service id="app.sqlite_lock" class="AppBundle\Lock\SqliteLock" />
265+
</services>
266+
</container>
267+
268+
.. code-block:: php
269+
270+
$container
271+
->register('app.mysql_lock', 'AppBundle\Lock\MysqlLock')
272+
->register('app.postgresql_lock', 'AppBundle\Lock\PostgresqlLock')
273+
->register('app.sqlite_lock', 'AppBundle\Lock\SqliteLock')
274+
;
275+
276+
Instead of dealing with these three services, your application needs a generic
277+
``app.lock`` service. This service must be an alias to any of the other services.
278+
Thanks to the ``auto_alias`` option, you can automatically create that alias
279+
based on the value of a configuration parameter.
280+
281+
Considering that a configuration parameter called ``database_type`` exists,
282+
the generic ``app.lock`` service can be defined as follows:
283+
284+
.. configuration-block::
285+
286+
.. code-block:: yaml
287+
288+
services:
289+
app.mysql_lock:
290+
class: AppBundle\Lock\MysqlLock
291+
public: false
292+
app.postgresql_lock:
293+
class: AppBundle\Lock\PostgresqlLock
294+
public: false
295+
app.sqlite_lock:
296+
class: AppBundle\Lock\SqliteLock
297+
public: false
298+
app.lock:
299+
tags:
300+
- { name: auto_alias, format: "app.%database_type%_lock" }
301+
302+
.. code-block:: xml
303+
304+
<?xml version="1.0" encoding="UTF-8" ?>
305+
<container xmlns="http://symfony.com/schema/dic/services"
306+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
307+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
308+
309+
<services>
310+
<service id="app.mysql_lock" public="false"
311+
class="AppBundle\Lock\MysqlLock" />
312+
<service id="app.postgresql_lock" public="false"
313+
class="AppBundle\Lock\PostgresqlLock" />
314+
<service id="app.sqlite_lock" public="false"
315+
class="AppBundle\Lock\SqliteLock" />
316+
317+
<service id="app.lock">
318+
<tag name="auto_alias" format="app.%database_type%_lock" />
319+
</service>
320+
</services>
321+
</container>
322+
323+
.. code-block:: php
324+
325+
$container
326+
->register('app.mysql_lock', 'AppBundle\Lock\MysqlLock')->setPublic(false)
327+
->register('app.postgresql_lock', 'AppBundle\Lock\PostgresqlLock')->setPublic(false)
328+
->register('app.sqlite_lock', 'AppBundle\Lock\SqliteLock')->setPublic(false)
329+
330+
->register('app.lock')
331+
->addTag('auto_alias', array('format' => 'app.%database_type%_lock'))
332+
;
333+
334+
The ``format`` parameter defines the expression used to construct the name of
335+
the service to alias. This expression can use any container parameter (as usual,
336+
wrapping their names with ``%`` characters).
337+
338+
.. note::
339+
340+
When using the ``auto_alias`` tag, it's not mandatory to define the aliased
341+
services as private. However, doing that (like in the above example) makes
342+
sense most of the times to prevent accessing those services directly instead
343+
of using the generic service alias.
344+
230345
console.command
231346
---------------
232347

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy