Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04d64b6

Browse files
committed
Merge pull request symfony#1457 from Burgov/override-service-and-configuration
Override service and configuration
2 parents 93af775 + fdbcd1d commit 04d64b6

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

cookbook/bundles/override.rst

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,36 @@ inheritance. For more information, see :doc:`/cookbook/bundles/inheritance`.
3535
Services & Configuration
3636
------------------------
3737

38-
In progress...
38+
In order to override/extend a service, there are two options. Firstly, you can
39+
set the parameter holding the service's class name to your own class by setting
40+
it in the config.yml. This of course is only possible if the class name is
41+
defined as a parameter in the service config of the bundle containing the
42+
service. Secondly, if this is not the case, or if you want to make sure the
43+
class is always overridden when your bundle is used, you should use a compiler
44+
pass:
45+
46+
.. code-block:: php
47+
namespace Foo\BarBundle\DependencyInjection\Compiler;
48+
49+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
50+
use Symfony\Component\DependencyInjection\ContainerBuilder;
51+
52+
class OverrideServiceCompilerPass implements CompilerPassInterface
53+
{
54+
55+
public function process(ContainerBuilder $container)
56+
{
57+
$definition = $container->getDefinition('original-service-id');
58+
$definition->setClass('Foo\BarBundle\YourService');
59+
}
60+
}
61+
62+
In this example we fetch the service definition of the original service, and set
63+
it's class name to our own class.
64+
65+
See `/cookbook/service_container/compiler_passes` for information on how to use
66+
compiler passes. If you want to do something beyond just overriding the class -
67+
like adding a method call - You can only use the compiler pass method.
3968

4069
Entities & Entity mapping
4170
-------------------------

0 commit comments

Comments
 (0)