@@ -22,6 +22,7 @@ Tag Name Usage
22
22
`assetic.formula_resource `_ Adds a resource to the current asset manager
23
23
`assetic.templating.php `_ Remove this service if PHP templating is disabled
24
24
`assetic.templating.twig `_ Remove this service if Twig templating is disabled
25
+ `auto_alias `_ Define aliases based on the value of container parameters
25
26
`console.command `_ Add a command
26
27
`data_collector `_ Create a class that collects custom data for the profiler
27
28
`doctrine.event_listener `_ Add a Doctrine event listener
@@ -227,6 +228,120 @@ assetic.templating.twig
227
228
The tagged service will be removed from the container if
228
229
``framework.templating.engines `` config section does not contain ``twig ``.
229
230
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
+
230
345
console.command
231
346
---------------
232
347
0 commit comments