Skip to content

Commit 93d0c87

Browse files
author
Bart van den Burg
committed
added documentation on how to extend bundle services
1 parent 240cf46 commit 93d0c87

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

cookbook/bundles/override.rst

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ How to Override any Part of a Bundle
77
This document is a quick reference for how to override different parts of
88
third-party bundles.
99

10+
.. note::
11+
12+
Whenever you are extending a (part of a) bundle, make sure that your bundle
13+
is registered in the kernel **after** the bundle you're trying to override
14+
parts of. Otherwise, your config that is supposed to override bundle
15+
configuration, is instead overridden by it!
16+
1017
Templates
1118
---------
1219

@@ -35,7 +42,40 @@ inheritance. For more information, see :doc:`/cookbook/bundles/inheritance`.
3542
Services & Configuration
3643
------------------------
3744

38-
In progress...
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. Then, in your bundle
52+
you can override the class name by setting the parameter directly in the
53+
container in the Extension class of your bundle:
54+
55+
.. code-block:: html+php
56+
<?php
57+
58+
namespace Foo\BarBundle\DependencyInjection;
59+
60+
use Symfony\Component\DependencyInjection\ContainerBuilder;
61+
use Symfony\Component\Config\FileLocator;
62+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
63+
use Symfony\Component\DependencyInjection\Loader;
64+
65+
class FooBarExtension extends Extension
66+
{
67+
68+
public function load(array $configs, ContainerBuilder $container)
69+
{
70+
$configuration = new Configuration();
71+
$config = $this->processConfiguration($configuration, $configs);
72+
73+
$container->setParameter('parameter_name.containing.service_class', 'Foo\BarBundle\Service\Service');
74+
75+
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
76+
$loader->load('services.xml');
77+
}
78+
}
3979

4080
Entities & Entity mapping
4181
-------------------------

0 commit comments

Comments
 (0)