From a5f0eec0dfc3e147057f5de258f2cadb0c66006b Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 26 Jun 2015 11:24:24 +0200 Subject: [PATCH 1/7] Documented the "auto_alias" feature --- reference/dic_tags.rst | 100 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/reference/dic_tags.rst b/reference/dic_tags.rst index e4db9851aaa..84b7d96cd59 100644 --- a/reference/dic_tags.rst +++ b/reference/dic_tags.rst @@ -22,6 +22,7 @@ Tag Name Usage `assetic.formula_resource`_ Adds a resource to the current asset manager `assetic.templating.php`_ Remove this service if PHP templating is disabled `assetic.templating.twig`_ Remove this service if Twig templating is disabled +`auto_alias`_ Define aliases based on the value of container parameters `console.command`_ Add a command `data_collector`_ Create a class that collects custom data for the profiler `doctrine.event_listener`_ Add a Doctrine event listener @@ -227,6 +228,105 @@ assetic.templating.twig The tagged service will be removed from the container if ``framework.templating.engines`` config section does not contain ``twig``. +auto_alias +---------- + +**Purpose**: Define aliases based on the value of container parameters + +Consider the following configuration that defines three different but related +services: + +.. configuration-block:: + + .. code-block:: yaml + + services: + app.mysql_lock: + class: AppBundle\Lock\MysqlLock + app.postgresql_lock: + class: AppBundle\Lock\PostgresqlLock + app.sqlite_lock: + class: AppBundle\Lock\SqliteLock + + + .. code-block:: xml + + + + + + + + + + + + .. code-block:: php + + $container + ->register('app.mysql_lock', 'AppBundle\Lock\MysqlLock') + ->register('app.postgresql_lock', 'AppBundle\Lock\PostgresqlLock') + ->register('app.sqlite_lock', 'AppBundle\Lock\SqliteLock') + ; + +Instead of dealing with these three services, your application needs a generic +``app.lock`` service. This service must be an alias to any of the other services. +Thanks to the ``auto_alias`` option, you can automatically create that alias +based on the value of a configuration parameter. + +Considering that a configuration parameter called ``database_type`` exists, +the generic ``app.lock`` service can be defined as follows: + +.. configuration-block:: + + .. code-block:: yaml + + services: + app.mysql_lock: + class: AppBundle\Lock\MysqlLock + app.postgresql_lock: + class: AppBundle\Lock\PostgresqlLock + app.sqlite_lock: + class: AppBundle\Lock\SqliteLock + app.lock: + tags: + - { name: auto_alias, format: "%database_type%.lock" } + + .. code-block:: xml + + + + + + + + + + + + + + + + .. code-block:: php + + $container + ->register('app.mysql_lock', 'AppBundle\Lock\MysqlLock') + ->register('app.postgresql_lock', 'AppBundle\Lock\PostgresqlLock') + ->register('app.sqlite_lock', 'AppBundle\Lock\SqliteLock') + + ->register('app.lock') + ->addTag('auto_alias', array('format' => '%database_type%.lock')) + ; + +The ``format`` parameter defines the expression used to construct the name of +the service to alias. This expression can use any container parameter (as usual, +wrapping their names with ``%`` characters). + console.command --------------- From 69152e7036d1d309380c2022a8f85a662d3ad08b Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 26 Jun 2015 11:37:48 +0200 Subject: [PATCH 2/7] Added the "versionadded: 2.7" directive --- reference/dic_tags.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/reference/dic_tags.rst b/reference/dic_tags.rst index 84b7d96cd59..34a7952b3e7 100644 --- a/reference/dic_tags.rst +++ b/reference/dic_tags.rst @@ -231,6 +231,9 @@ The tagged service will be removed from the container if auto_alias ---------- +.. versionadded:: 2.7 + The ``auto_alias`` tag was introduced in Symfony 2.7. + **Purpose**: Define aliases based on the value of container parameters Consider the following configuration that defines three different but related From e24f77ed372b0086cd74279647de90dcf6842f7c Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 26 Jun 2015 11:38:31 +0200 Subject: [PATCH 3/7] Removed an extra blank line --- reference/dic_tags.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/reference/dic_tags.rst b/reference/dic_tags.rst index 34a7952b3e7..1a9cd187814 100644 --- a/reference/dic_tags.rst +++ b/reference/dic_tags.rst @@ -251,7 +251,6 @@ services: app.sqlite_lock: class: AppBundle\Lock\SqliteLock - .. code-block:: xml From 4c50cb07b999c5b58abb673484dd18114dcc8442 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 26 Jun 2015 12:09:09 +0200 Subject: [PATCH 4/7] Fixed some errors and added a new note --- reference/dic_tags.rst | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/reference/dic_tags.rst b/reference/dic_tags.rst index 1a9cd187814..20794ff3319 100644 --- a/reference/dic_tags.rst +++ b/reference/dic_tags.rst @@ -288,13 +288,16 @@ the generic ``app.lock`` service can be defined as follows: services: app.mysql_lock: class: AppBundle\Lock\MysqlLock + public: false app.postgresql_lock: class: AppBundle\Lock\PostgresqlLock + public: false app.sqlite_lock: class: AppBundle\Lock\SqliteLock + public: false app.lock: tags: - - { name: auto_alias, format: "%database_type%.lock" } + - { name: auto_alias, format: "app.%database_type%.lock" } .. code-block:: xml @@ -304,12 +307,15 @@ the generic ``app.lock`` service can be defined as follows: xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - - + + + - + @@ -317,18 +323,25 @@ the generic ``app.lock`` service can be defined as follows: .. code-block:: php $container - ->register('app.mysql_lock', 'AppBundle\Lock\MysqlLock') - ->register('app.postgresql_lock', 'AppBundle\Lock\PostgresqlLock') - ->register('app.sqlite_lock', 'AppBundle\Lock\SqliteLock') + ->register('app.mysql_lock', 'AppBundle\Lock\MysqlLock')->setPublic(false) + ->register('app.postgresql_lock', 'AppBundle\Lock\PostgresqlLock')->setPublic(false) + ->register('app.sqlite_lock', 'AppBundle\Lock\SqliteLock')->setPublic(false) ->register('app.lock') - ->addTag('auto_alias', array('format' => '%database_type%.lock')) + ->addTag('auto_alias', array('format' => 'app.%database_type%.lock')) ; The ``format`` parameter defines the expression used to construct the name of the service to alias. This expression can use any container parameter (as usual, wrapping their names with ``%`` characters). +.. note:: + + When using the ``auto_alias`` tag is not mandatory to define the aliased + services as private. However, doing that (like in the above example) makes + sense most of the times to prevent accessing those services directly instead + of using the generic service. + console.command --------------- From 4d0f6ea1bcb2e197c388d822698c39bd66ebabe5 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 30 Jun 2015 16:04:27 +0200 Subject: [PATCH 5/7] Minor fixes --- reference/dic_tags.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reference/dic_tags.rst b/reference/dic_tags.rst index 20794ff3319..ea752f9d6c1 100644 --- a/reference/dic_tags.rst +++ b/reference/dic_tags.rst @@ -337,10 +337,10 @@ wrapping their names with ``%`` characters). .. note:: - When using the ``auto_alias`` tag is not mandatory to define the aliased + When using the ``auto_alias`` tag it's not mandatory to define the aliased services as private. However, doing that (like in the above example) makes sense most of the times to prevent accessing those services directly instead - of using the generic service. + of using the generic service alias. console.command --------------- From bab745d6628e2694bba9ade1fd46a13b21b67726 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 2 Jul 2015 22:49:15 +0200 Subject: [PATCH 6/7] Minor grammar issue --- reference/dic_tags.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reference/dic_tags.rst b/reference/dic_tags.rst index ea752f9d6c1..9788888e43b 100644 --- a/reference/dic_tags.rst +++ b/reference/dic_tags.rst @@ -337,7 +337,7 @@ wrapping their names with ``%`` characters). .. note:: - When using the ``auto_alias`` tag it's not mandatory to define the aliased + When using the ``auto_alias`` tag, it's not mandatory to define the aliased services as private. However, doing that (like in the above example) makes sense most of the times to prevent accessing those services directly instead of using the generic service alias. From 1524f5a66ec34bbaefad6802aec3163a4b2eedbd Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 14 Jul 2015 21:34:50 +0200 Subject: [PATCH 7/7] Fixed an error in the auto_alias format value --- reference/dic_tags.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/reference/dic_tags.rst b/reference/dic_tags.rst index 9788888e43b..56013b4a52e 100644 --- a/reference/dic_tags.rst +++ b/reference/dic_tags.rst @@ -297,7 +297,7 @@ the generic ``app.lock`` service can be defined as follows: public: false app.lock: tags: - - { name: auto_alias, format: "app.%database_type%.lock" } + - { name: auto_alias, format: "app.%database_type%_lock" } .. code-block:: xml @@ -315,7 +315,7 @@ the generic ``app.lock`` service can be defined as follows: class="AppBundle\Lock\SqliteLock" /> - + @@ -328,7 +328,7 @@ the generic ``app.lock`` service can be defined as follows: ->register('app.sqlite_lock', 'AppBundle\Lock\SqliteLock')->setPublic(false) ->register('app.lock') - ->addTag('auto_alias', array('format' => 'app.%database_type%.lock')) + ->addTag('auto_alias', array('format' => 'app.%database_type%_lock')) ; The ``format`` parameter defines the expression used to construct the name of