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

Skip to content

Commit fdbcd1d

Browse files
committed
information on how to use a compiler pass to override a service
1 parent dd28f35 commit fdbcd1d

File tree

1 file changed

+22
-39
lines changed

1 file changed

+22
-39
lines changed

cookbook/bundles/override.rst

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

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\BarBundle\DependencyInjection;
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;
6250
use Symfony\Component\DependencyInjection\ContainerBuilder;
63-
use Symfony\Component\Config\FileLocator;
64-
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
65-
use Symfony\Component\DependencyInjection\Loader;
6651
67-
class FooBarExtension extends Extension
52+
class OverrideServiceCompilerPass implements CompilerPassInterface
6853
{
6954
70-
public function load(array $configs, ContainerBuilder $container)
55+
public function process(ContainerBuilder $container)
7156
{
72-
$configuration = new Configuration();
73-
$config = $this->processConfiguration($configuration, $configs);
74-
75-
$container->setParameter('parameter_name.containing.service_class', 'Foo\BarBundle\Service\Service');
76-
77-
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
78-
$loader->load('services.xml');
57+
$definition = $container->getDefinition('original-service-id');
58+
$definition->setClass('Foo\BarBundle\YourService');
7959
}
8060
}
8161
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.
8568

8669
Entities & Entity mapping
8770
-------------------------

0 commit comments

Comments
 (0)