Skip to content

[DependencyInjection] Documented the ability of define the service decoration priority #5600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions components/dependency_injection/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,79 @@ You can change the inner service name if you want to:
->setPublic(false)
->setDecoratedService('foo', 'bar.wooz');

.. versionadded:: 2.8
The ability to define the decoration priority was introduced in Symfony 2.8.
Prior to Symfony 2.8, the priority depends on the order in
which definitions are found.

If you want to apply more than one decorator to a service, you can control their
order by configuring the priority of decoration, this can be any integer number
(decorators with higher priorities will be applied first).

.. configuration-block::

.. code-block:: yaml

foo:
class: Foo

bar:
class: Bar
public: false
decorates: foo
decoration_priority: 5
arguments: ['@bar.inner']

baz:
class: Baz
public: false
decorates: foo
decoration_priority: 1
arguments: ['@baz.inner']

.. code-block:: xml

<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="foo" class="Foo" />

<service id="bar" class="Bar" decorates="foo" decoration-priority="5" public="false">
<argument type="service" id="bar.inner" />
</service>

<service id="baz" class="Baz" decorates="foo" decoration-priority="1" public="false">
<argument type="service" id="baz.inner" />
</service>
</services>
</container>

.. code-block:: php

use Symfony\Component\DependencyInjection\Reference;

$container->register('foo', 'Foo')

$container->register('bar', 'Bar')
->addArgument(new Reference('bar.inner'))
->setPublic(false)
->setDecoratedService('foo', null, 5);

$container->register('baz', 'Baz')
->addArgument(new Reference('baz.inner'))
->setPublic(false)
->setDecoratedService('foo', null, 1);

The generated code will be the following:

.. code-block:: php

$this->services['foo'] = new Baz(new Bar(new Foo())));

Deprecating Services
--------------------

Expand Down