From cb545ff7db13d0f057abc8cb4a9c509d07b9f67a Mon Sep 17 00:00:00 2001 From: danut007ro Date: Fri, 2 Jun 2017 00:35:19 +0300 Subject: [PATCH 1/3] [DI] Automatically applying a tag to all services implementing an interface --- service_container/tags.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/service_container/tags.rst b/service_container/tags.rst index 37368f23137..dddc4b63f07 100644 --- a/service_container/tags.rst +++ b/service_container/tags.rst @@ -70,6 +70,20 @@ then some tags are automatically applied for you. That's true for the ``twig.ext tag: the container sees that your class extends ``Twig_Extension`` (or more accurately, that it implements ``Twig_ExtensionInterface``) and adds the tag for you. +In order to have a tag automatically applied to all your services that implement an interface, +you should create an extension following the steps described in :doc:`/bundles/extension`, +then in the ``load()`` method call ``registerForAutoconfiguration()`` and apply your tag:: + + // src/AppBundle/DependencyInjection/AppExtension.php + + // ... + public function load(array $configs, ContainerBuilder $container) + { + $container->registerForAutoconfiguration(CustomInterface::class) + ->addTag('app.custom_tag') + ; + } + Creating custom Tags -------------------- From e5b1758b44ab15e11019f8c40a39662e7260940c Mon Sep 17 00:00:00 2001 From: Daniel Gorgan Date: Sun, 11 Jun 2017 13:28:55 +0300 Subject: [PATCH 2/3] Update tags.rst --- service_container/tags.rst | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/service_container/tags.rst b/service_container/tags.rst index dddc4b63f07..2ffef708031 100644 --- a/service_container/tags.rst +++ b/service_container/tags.rst @@ -70,19 +70,24 @@ then some tags are automatically applied for you. That's true for the ``twig.ext tag: the container sees that your class extends ``Twig_Extension`` (or more accurately, that it implements ``Twig_ExtensionInterface``) and adds the tag for you. -In order to have a tag automatically applied to all your services that implement an interface, -you should create an extension following the steps described in :doc:`/bundles/extension`, -then in the ``load()`` method call ``registerForAutoconfiguration()`` and apply your tag:: - - // src/AppBundle/DependencyInjection/AppExtension.php +.. tip:: - // ... - public function load(array $configs, ContainerBuilder $container) - { - $container->registerForAutoconfiguration(CustomInterface::class) - ->addTag('app.custom_tag') - ; - } + To apply a tag to all your autoconfigured services extending a class or an + interface, call ``ContainerBuilder::registerForAutoconfiguration()`` in an + :doc:`extension ` or from your kernel:: + + // app/AppKernel.php + class AppKernel extends Kernel + { + // ... + + protected function build(ContainerBuilder $container) + { + $container->registerForAutoconfiguration(CustomInterface::class) + ->addTag('app.custom_tag') + ; + } + } Creating custom Tags -------------------- From c31e24c854065228ad92cfbd50da43926de1ea58 Mon Sep 17 00:00:00 2001 From: Daniel Gorgan Date: Sun, 11 Jun 2017 15:00:57 +0300 Subject: [PATCH 3/3] Create tags.rst --- service_container/tags.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/service_container/tags.rst b/service_container/tags.rst index 2ffef708031..d90016790f6 100644 --- a/service_container/tags.rst +++ b/service_container/tags.rst @@ -73,8 +73,8 @@ that it implements ``Twig_ExtensionInterface``) and adds the tag for you. .. tip:: To apply a tag to all your autoconfigured services extending a class or an - interface, call ``ContainerBuilder::registerForAutoconfiguration()`` in an - :doc:`extension ` or from your kernel:: + interface, call :method:`Symfony\\Component\\DependencyInjection\\ContainerBuilder::registerForAutoconfiguration` + method in an :doc:`extension ` or from your kernel:: // app/AppKernel.php class AppKernel extends Kernel