Skip to content

Commit 5868190

Browse files
committed
Documented the ability of define service decoration priority
1 parent 889db17 commit 5868190

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

Diff for: components/dependency_injection/advanced.rst

+73
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,79 @@ You can change the inner service name if you want to:
219219
->setPublic(false)
220220
->setDecoratedService('foo', 'bar.wooz');
221221
222+
.. versionadded:: 2.8
223+
The ability to define the decoration priority was introduced in Symfony 2.8.
224+
Prior to Symfony 2.8, the priority depends on the order in
225+
which definitions are found.
226+
227+
If you want to apply more than one decorator to a service, you can control their
228+
order by configuring the priority of decoration, this can be any integer number
229+
(decorators with higher priorities will be applied first).
230+
231+
.. configuration-block::
232+
233+
.. code-block:: yaml
234+
235+
foo:
236+
class: Foo
237+
238+
bar:
239+
class: Bar
240+
public: false
241+
decorates: foo
242+
decoration_priority: 5
243+
arguments: ['@bar.inner']
244+
245+
baz:
246+
class: Baz
247+
public: false
248+
decorates: foo
249+
decoration_priority: 1
250+
arguments: ['@baz.inner']
251+
252+
.. code-block:: xml
253+
254+
<?xml version="1.0" encoding="UTF-8" ?>
255+
256+
<container xmlns="http://symfony.com/schema/dic/services"
257+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
258+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
259+
260+
<services>
261+
<service id="foo" class="Foo" />
262+
263+
<service id="bar" class="Bar" decorates="foo" decoration-priority="5" public="false">
264+
<argument type="service" id="bar.inner" />
265+
</service>
266+
267+
<service id="baz" class="Baz" decorates="foo" decoration-priority="1" public="false">
268+
<argument type="service" id="baz.inner" />
269+
</service>
270+
</services>
271+
</container>
272+
273+
.. code-block:: php
274+
275+
use Symfony\Component\DependencyInjection\Reference;
276+
277+
$container->register('foo', 'Foo')
278+
279+
$container->register('bar', 'Bar')
280+
->addArgument(new Reference('bar.inner'))
281+
->setPublic(false)
282+
->setDecoratedService('foo', null, 5);
283+
284+
$container->register('baz', 'Baz')
285+
->addArgument(new Reference('baz.inner'))
286+
->setPublic(false)
287+
->setDecoratedService('foo', null, 1);
288+
289+
The generated code will be the following:
290+
291+
.. code-block:: php
292+
293+
$this->services['foo'] = new Baz(new Bar(new Foo())));
294+
222295
Deprecating Services
223296
--------------------
224297

0 commit comments

Comments
 (0)