Skip to content

Commit b65762d

Browse files
committed
Merge branch '6.4' into 7.3
* 6.4: [DependencyInjection] Updated the explanation of the inner argument renaming
2 parents 3a4cfd1 + d15081e commit b65762d

File tree

1 file changed

+83
-55
lines changed

1 file changed

+83
-55
lines changed

service_container/service_decoration.rst

Lines changed: 83 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -209,78 +209,106 @@ automatically changed to ``'.inner'``):
209209
->args([service('.inner')]);
210210
};
211211
212-
.. note::
212+
When decorating a service, the original service (e.g. ``App\Mailer``) is available
213+
inside the decorating service (e.g. ``App\DecoratingMailer``) using an ID constructed
214+
as "Decorating service ID" + the ``.inner`` suffix (e.g. in the previous example,
215+
the ID is ``'App\DecoratingMailer.inner'``). You can control the inner service
216+
name via the ``decoration_inner_name`` option:
213217

214-
The visibility of the decorated ``App\Mailer`` service (which is an alias
215-
for the new service) will still be the same as the original ``App\Mailer``
216-
visibility.
218+
.. configuration-block::
217219

218-
.. note::
220+
.. code-block:: php-attributes
219221
220-
All custom :doc:`service tags </service_container/tags>` from the decorated
221-
service are removed in the new service. Only certain built-in service tags
222-
defined by Symfony are retained: ``container.service_locator``, ``container.service_subscriber``,
223-
``kernel.event_subscriber``, ``kernel.event_listener``, ``kernel.locale_aware``,
224-
and ``kernel.reset``.
222+
// when using the #[AutowireDecorated] attribute, you can name the argument
223+
// that holds the decorated service however you like, without needing to
224+
// configure that name explicitly
225+
#[AutowireDecorated] private Mailer $originalMailer,
225226
226-
.. note::
227+
.. code-block:: yaml
227228
228-
The generated inner id is based on the id of the decorator service
229-
(``App\DecoratingMailer`` here), not of the decorated service (``App\Mailer``
230-
here). You can control the inner service name via the ``decoration_inner_name``
231-
option:
229+
# config/services.yaml
230+
services:
231+
App\DecoratingMailer:
232+
# ...
233+
decoration_inner_name: 'original_mailer'
234+
arguments: ['@original_mailer']
232235
233-
.. configuration-block::
236+
# if you decorate a lot of services, consider adding the full
237+
# original service ID as part of the new ID
238+
decoration_inner_name: 'App\Mailer.original'
239+
arguments: ['@App\Mailer.original']
234240
235-
.. code-block:: yaml
241+
.. code-block:: xml
236242
237-
# config/services.yaml
238-
services:
239-
App\DecoratingMailer:
240-
# ...
241-
decoration_inner_name: App\DecoratingMailer.wooz
242-
arguments: ['@App\DecoratingMailer.wooz']
243+
<!-- config/services.xml -->
244+
<?xml version="1.0" encoding="UTF-8" ?>
245+
<container xmlns="http://symfony.com/schema/dic/services"
246+
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
247+
xsd:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
243248
244-
.. code-block:: xml
249+
<services>
250+
<!-- ... -->
245251
246-
<!-- config/services.xml -->
247-
<?xml version="1.0" encoding="UTF-8" ?>
248-
<container xmlns="http://symfony.com/schema/dic/services"
249-
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
250-
xsd:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
251-
252-
<services>
253-
<!-- ... -->
254-
255-
<service
256-
id="App\DecoratingMailer"
257-
decorates="App\Mailer"
258-
decoration-inner-name="App\DecoratingMailer.wooz"
259-
public="false"
260-
>
261-
<argument type="service" id="App\DecoratingMailer.wooz"/>
262-
</service>
252+
<service
253+
id="App\DecoratingMailer"
254+
decorates="App\Mailer"
255+
decoration-inner-name="original_mailer"
256+
public="false"
257+
>
258+
<argument type="service" id="original_mailer"/>
259+
</service>
263260
264-
</services>
265-
</container>
261+
<!-- if you decorate a lot of services, consider adding the full
262+
original service ID as part of the new ID -->
263+
<service
264+
id="App\DecoratingMailer"
265+
decorates="App\Mailer"
266+
decoration-inner-name="App\Mailer.original"
267+
public="false"
268+
>
269+
<argument type="service" id="App\Mailer.original"/>
270+
</service>
266271
267-
.. code-block:: php
272+
</services>
273+
</container>
268274
269-
// config/services.php
270-
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
275+
.. code-block:: php
271276
272-
use App\DecoratingMailer;
273-
use App\Mailer;
277+
// config/services.php
278+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
274279
275-
return function(ContainerConfigurator $container): void {
276-
$services = $container->services();
280+
use App\DecoratingMailer;
281+
use App\Mailer;
282+
283+
return function(ContainerConfigurator $container): void {
284+
$services = $container->services();
285+
286+
$services->set(Mailer::class);
277287
278-
$services->set(Mailer::class);
288+
$services->set(DecoratingMailer::class)
289+
->decorate(Mailer::class, 'original_mailer')
290+
->args([service('original_mailer')]);
279291
280-
$services->set(DecoratingMailer::class)
281-
->decorate(Mailer::class, DecoratingMailer::class.'.wooz')
282-
->args([service(DecoratingMailer::class.'.wooz')]);
283-
};
292+
// if you decorate a lot of services, consider adding the full
293+
// original service ID as part of the new ID
294+
$services->set(DecoratingMailer::class)
295+
->decorate(Mailer::class, DecoratingMailer::class.'.original')
296+
->args([service(DecoratingMailer::class.'.original')]);
297+
};
298+
299+
.. note::
300+
301+
The visibility of the decorated ``App\Mailer`` service (which is an alias
302+
for the new service) will still be the same as the original ``App\Mailer``
303+
visibility.
304+
305+
.. note::
306+
307+
All custom :doc:`service tags </service_container/tags>` from the decorated
308+
service are removed in the new service. Only certain built-in service tags
309+
defined by Symfony are retained: ``container.service_locator``, ``container.service_subscriber``,
310+
``kernel.event_subscriber``, ``kernel.event_listener``, ``kernel.locale_aware``,
311+
and ``kernel.reset``.
284312

285313
Decoration Priority
286314
-------------------

0 commit comments

Comments
 (0)