Skip to content

Commit a2e9654

Browse files
committed
[cookbook][bundles] Tweaking and adding some additional details to #1457
1 parent 04d64b6 commit a2e9654

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

cookbook/bundles/override.rst

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

38-
In order to override/extend a service, there are two options. Firstly, you can
38+
In order to override/extend a service, there are two options. First, you can
3939
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
40+
it in ``app/config/config.yml``. This of course is only possible if the class name is
4141
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:
42+
service. For example, to override the class used for Symfony's ``translator``
43+
service, you would override the ``translator.class`` parameter. Knowing exactly
44+
which parameter to override may take some research. For the translator, the
45+
parameter is defined and used in the ``Resources/config/translation.xml`` file
46+
in the core FrameworkBundle:
47+
48+
.. configuration-block::
49+
50+
.. code-block:: yaml
51+
52+
# app/config/config.yml
53+
parameters:
54+
translator.class: Acme\HelloBundle\Translation\Translator
55+
56+
.. code-block:: xml
57+
58+
<!-- app/config/config.xml -->
59+
<parameters>
60+
<parameter key="translator.class">Acme\HelloBundle\Translation\Translator</parameter>
61+
</parameters>
62+
63+
.. code-block:: php
64+
65+
// app/config/config.php
66+
67+
$container->setParameter('translator.class', 'Acme\HelloBundle\Translation\Translator');
68+
69+
Secondly, if the class is not available as a parameter, you want to make sure the
70+
class is always overridden when your bundle is used, or you need to modify
71+
something beyond just the class name, you should use a compiler pass::
4572

46-
.. code-block:: php
4773
namespace Foo\BarBundle\DependencyInjection\Compiler;
4874

4975
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
5076
use Symfony\Component\DependencyInjection\ContainerBuilder;
5177

5278
class OverrideServiceCompilerPass implements CompilerPassInterface
5379
{
54-
5580
public function process(ContainerBuilder $container)
5681
{
5782
$definition = $container->getDefinition('original-service-id');
@@ -60,11 +85,11 @@ pass:
6085
}
6186

6287
In this example we fetch the service definition of the original service, and set
63-
it's class name to our own class.
88+
its class name to our own class.
6489

65-
See `/cookbook/service_container/compiler_passes` for information on how to use
90+
See :doc:`/cookbook/service_container/compiler_passes` for information on how to use
6691
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.
92+
like adding a method call - you can only use the compiler pass method.
6893

6994
Entities & Entity mapping
7095
-------------------------

0 commit comments

Comments
 (0)