@@ -35,53 +35,36 @@ inheritance. For more information, see :doc:`/cookbook/bundles/inheritance`.
35
35
Services & Configuration
36
36
------------------------
37
37
38
- .. note ::
39
-
40
- Whenever you are extending a (part of a) bundle, make sure that your bundle
41
- is registered in the kernel **after ** the bundle you're trying to override
42
- parts of. Otherwise, your config that is supposed to override bundle
43
- configuration, is instead overridden by it!
44
-
45
- In order to completely override a service, just define the service as you would
46
- usual, but making sure the id of the service is identical to the one you are
47
- overriding.
48
-
49
- In order to extend a service (e.g. just add a method, but leaving the
50
- dependencies or tags intact), make sure the class name is defined as a parameter
51
- in the service config of the bundle containing the service. You can then either
52
- set this parameter in your config.yml, or, if you're going to reuse your bundle
53
- and it should always override the class, in your bundle you can override the
54
- class name by setting the parameter directly in the container in the Extension
55
- class of your bundle:
56
-
57
- .. code-block :: html+php
58
- <?php
59
-
60
- namespace Foo\B arBundle\D ependencyInjection;
61
-
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;
62
50
use Symfony\Component\DependencyInjection\ContainerBuilder;
63
- use Symfony\C omponent\C onfig\F ileLocator;
64
- use Symfony\C omponent\H ttpKernel\D ependencyInjection\E xtension;
65
- use Symfony\C omponent\D ependencyInjection\L oader;
66
51
67
- class FooBarExtension extends Extension
52
+ class OverrideServiceCompilerPass implements CompilerPassInterface
68
53
{
69
54
70
- public function load(array $configs, ContainerBuilder $container)
55
+ public function process( ContainerBuilder $container)
71
56
{
72
- $configuration = new Configuration();
73
- $config = $this->processConfiguration($configuration, $configs);
74
-
75
- $container->setParameter('parameter_name.containing.service_class', 'Foo\B arBundle\S ervice\S ervice');
76
-
77
- $loader = new Loader\X mlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
78
- $loader->load('services.xml');
57
+ $definition = $container->getDefinition('original-service-id');
58
+ $definition->setClass('Foo\BarBundle\YourService');
79
59
}
80
60
}
81
61
82
- If you want to do something beyond just overriding a parameter - like adding a
83
- method call - it must be done as a compiler pass. See
84
- `/cookbook/service_container/compiler_passes `
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.
85
68
86
69
Entities & Entity mapping
87
70
-------------------------
0 commit comments