From 93d0c87b1ebea4e1f0465d5949093916ae7efc8d Mon Sep 17 00:00:00 2001 From: Bart van den Burg Date: Thu, 31 May 2012 16:55:20 +0200 Subject: [PATCH 1/3] added documentation on how to extend bundle services --- cookbook/bundles/override.rst | 42 ++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/cookbook/bundles/override.rst b/cookbook/bundles/override.rst index a01a73294ff..bd8e836d88d 100644 --- a/cookbook/bundles/override.rst +++ b/cookbook/bundles/override.rst @@ -7,6 +7,13 @@ How to Override any Part of a Bundle This document is a quick reference for how to override different parts of third-party bundles. +.. note:: + + Whenever you are extending a (part of a) bundle, make sure that your bundle + is registered in the kernel **after** the bundle you're trying to override + parts of. Otherwise, your config that is supposed to override bundle + configuration, is instead overridden by it! + Templates --------- @@ -35,7 +42,40 @@ inheritance. For more information, see :doc:`/cookbook/bundles/inheritance`. Services & Configuration ------------------------ -In progress... +In order to completely override a service, just define the service as you would +usual, but making sure the id of the service is identical to the one you are +overriding. + +In order to extend a service (e.g. just add a method, but leaving the +dependencies or tags intact), make sure the class name is defined as a parameter +in the service config of the bundle containing the service. Then, in your bundle +you can override the class name by setting the parameter directly in the +container in the Extension class of your bundle: + +.. code-block:: html+php + processConfiguration($configuration, $configs); + + $container->setParameter('parameter_name.containing.service_class', 'Foo\BarBundle\Service\Service'); + + $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.xml'); + } + } Entities & Entity mapping ------------------------- From dd28f3546a85247c3214c2a86b5d6b25b8dd1bb8 Mon Sep 17 00:00:00 2001 From: Bart van den Burg Date: Thu, 14 Jun 2012 20:20:00 +0200 Subject: [PATCH 2/3] moved note to relevant section, added info on how to override a parameter and added section about how to add method calls to services --- cookbook/bundles/override.rst | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/cookbook/bundles/override.rst b/cookbook/bundles/override.rst index bd8e836d88d..bb80e3464d7 100644 --- a/cookbook/bundles/override.rst +++ b/cookbook/bundles/override.rst @@ -7,13 +7,6 @@ How to Override any Part of a Bundle This document is a quick reference for how to override different parts of third-party bundles. -.. note:: - - Whenever you are extending a (part of a) bundle, make sure that your bundle - is registered in the kernel **after** the bundle you're trying to override - parts of. Otherwise, your config that is supposed to override bundle - configuration, is instead overridden by it! - Templates --------- @@ -42,15 +35,24 @@ inheritance. For more information, see :doc:`/cookbook/bundles/inheritance`. Services & Configuration ------------------------ +.. note:: + + Whenever you are extending a (part of a) bundle, make sure that your bundle + is registered in the kernel **after** the bundle you're trying to override + parts of. Otherwise, your config that is supposed to override bundle + configuration, is instead overridden by it! + In order to completely override a service, just define the service as you would usual, but making sure the id of the service is identical to the one you are overriding. In order to extend a service (e.g. just add a method, but leaving the dependencies or tags intact), make sure the class name is defined as a parameter -in the service config of the bundle containing the service. Then, in your bundle -you can override the class name by setting the parameter directly in the -container in the Extension class of your bundle: +in the service config of the bundle containing the service. You can then either +set this parameter in your config.yml, or, if you're going to reuse your bundle +and it should always override the class, in your bundle you can override the +class name by setting the parameter directly in the container in the Extension +class of your bundle: .. code-block:: html+php Date: Sat, 16 Jun 2012 15:08:44 +0200 Subject: [PATCH 3/3] information on how to use a compiler pass to override a service --- cookbook/bundles/override.rst | 61 +++++++++++++---------------------- 1 file changed, 22 insertions(+), 39 deletions(-) diff --git a/cookbook/bundles/override.rst b/cookbook/bundles/override.rst index bb80e3464d7..80dba155c3f 100644 --- a/cookbook/bundles/override.rst +++ b/cookbook/bundles/override.rst @@ -35,53 +35,36 @@ inheritance. For more information, see :doc:`/cookbook/bundles/inheritance`. Services & Configuration ------------------------ -.. note:: - - Whenever you are extending a (part of a) bundle, make sure that your bundle - is registered in the kernel **after** the bundle you're trying to override - parts of. Otherwise, your config that is supposed to override bundle - configuration, is instead overridden by it! - -In order to completely override a service, just define the service as you would -usual, but making sure the id of the service is identical to the one you are -overriding. - -In order to extend a service (e.g. just add a method, but leaving the -dependencies or tags intact), make sure the class name is defined as a parameter -in the service config of the bundle containing the service. You can then either -set this parameter in your config.yml, or, if you're going to reuse your bundle -and it should always override the class, in your bundle you can override the -class name by setting the parameter directly in the container in the Extension -class of your bundle: - -.. code-block:: html+php - processConfiguration($configuration, $configs); - - $container->setParameter('parameter_name.containing.service_class', 'Foo\BarBundle\Service\Service'); - - $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); - $loader->load('services.xml'); + $definition = $container->getDefinition('original-service-id'); + $definition->setClass('Foo\BarBundle\YourService'); } } -If you want to do something beyond just overriding a parameter - like adding a -method call - it must be done as a compiler pass. See -`/cookbook/service_container/compiler_passes` +In this example we fetch the service definition of the original service, and set +it's class name to our own class. + +See `/cookbook/service_container/compiler_passes` for information on how to use +compiler passes. If you want to do something beyond just overriding the class - +like adding a method call - You can only use the compiler pass method. Entities & Entity mapping -------------------------