@@ -219,78 +219,106 @@ automatically changed to ``'.inner'``):
219219 ->args([service('.inner')]);
220220 };
221221
222- .. note ::
222+ When decorating a service, the original service (e.g. ``App\Mailer ``) is available
223+ inside the decorating service (e.g. ``App\DecoratingMailer ``) using an ID constructed
224+ as "Decorating service ID" + the ``.inner `` suffix (e.g. in the previous example,
225+ the ID is ``'App\DecoratingMailer.inner' ``). You can control the inner service
226+ name via the ``decoration_inner_name `` option:
223227
224- The visibility of the decorated ``App\Mailer `` service (which is an alias
225- for the new service) will still be the same as the original ``App\Mailer ``
226- visibility.
228+ .. configuration-block ::
227229
228- .. note ::
230+ .. code-block :: php-attributes
229231
230- All custom :doc: `service tags </service_container/tags >` from the decorated
231- service are removed in the new service. Only certain built-in service tags
232- defined by Symfony are retained: ``container.service_locator ``, ``container.service_subscriber ``,
233- ``kernel.event_subscriber ``, ``kernel.event_listener ``, ``kernel.locale_aware ``,
234- and ``kernel.reset ``.
232+ // when using the #[AutowireDecorated] attribute, you can name the argument
233+ // that holds the decorated service however you like, without needing to
234+ // configure that name explicitly
235+ #[AutowireDecorated] private Mailer $originalMailer,
235236
236- .. note ::
237+ .. code-block :: yaml
237238
238- The generated inner id is based on the id of the decorator service
239- (``App\DecoratingMailer `` here), not of the decorated service (``App\Mailer ``
240- here). You can control the inner service name via the ``decoration_inner_name ``
241- option:
239+ # config/services.yaml
240+ services :
241+ App\DecoratingMailer :
242+ # ...
243+ decoration_inner_name : ' original_mailer'
244+ arguments : ['@original_mailer']
242245
243- .. configuration-block ::
246+ # if you decorate a lot of services, consider adding the full
247+ # original service ID as part of the new ID
248+ decoration_inner_name : ' App\Mailer.original'
249+ arguments : ['@App\Mailer.original']
244250
245- .. code-block :: yaml
251+ .. code-block :: xml
246252
247- # config/services.yaml
248- services :
249- App\DecoratingMailer :
250- # ...
251- decoration_inner_name : App\DecoratingMailer.wooz
252- arguments : ['@App\DecoratingMailer.wooz']
253+ <!-- config/services.xml -->
254+ <?xml version =" 1.0" encoding =" UTF-8" ?>
255+ <container xmlns =" http://symfony.com/schema/dic/services"
256+ xmlns : xsd =" http://www.w3.org/2001/XMLSchema-instance"
257+ xsd : schemaLocation =" http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd" >
253258
254- .. code-block :: xml
259+ <services >
260+ <!-- ... -->
255261
256- <!-- config/services.xml -->
257- <?xml version =" 1.0" encoding =" UTF-8" ?>
258- <container xmlns =" http://symfony.com/schema/dic/services"
259- xmlns : xsd =" http://www.w3.org/2001/XMLSchema-instance"
260- xsd : schemaLocation =" http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd" >
261-
262- <services >
263- <!-- ... -->
264-
265- <service
266- id =" App\DecoratingMailer"
267- decorates =" App\Mailer"
268- decoration-inner-name =" App\DecoratingMailer.wooz"
269- public =" false"
270- >
271- <argument type =" service" id =" App\DecoratingMailer.wooz" />
272- </service >
262+ <service
263+ id =" App\DecoratingMailer"
264+ decorates =" App\Mailer"
265+ decoration-inner-name =" original_mailer"
266+ public =" false"
267+ >
268+ <argument type =" service" id =" original_mailer" />
269+ </service >
273270
274- </services >
275- </container >
271+ <!-- if you decorate a lot of services, consider adding the full
272+ original service ID as part of the new ID -->
273+ <service
274+ id =" App\DecoratingMailer"
275+ decorates =" App\Mailer"
276+ decoration-inner-name =" App\Mailer.original"
277+ public =" false"
278+ >
279+ <argument type =" service" id =" App\Mailer.original" />
280+ </service >
276281
277- .. code-block :: php
282+ </services >
283+ </container >
278284
279- // config/services.php
280- namespace Symfony\Component\DependencyInjection\Loader\Configurator;
285+ .. code-block :: php
281286
282- use App\DecoratingMailer;
283- use App\Mailer ;
287+ // config/services.php
288+ namespace Symfony\Component\DependencyInjection\Loader\Configurator ;
284289
285- return function(ContainerConfigurator $container): void {
286- $services = $container->services();
290+ use App\DecoratingMailer;
291+ use App\Mailer;
292+
293+ return function(ContainerConfigurator $container): void {
294+ $services = $container->services();
295+
296+ $services->set(Mailer::class);
287297
288- $services->set(Mailer::class);
298+ $services->set(DecoratingMailer::class)
299+ ->decorate(Mailer::class, 'original_mailer')
300+ ->args([service('original_mailer')]);
289301
290- $services->set(DecoratingMailer::class)
291- ->decorate(Mailer::class, DecoratingMailer::class.'.wooz')
292- ->args([service(DecoratingMailer::class.'.wooz')]);
293- };
302+ // if you decorate a lot of services, consider adding the full
303+ // original service ID as part of the new ID
304+ $services->set(DecoratingMailer::class)
305+ ->decorate(Mailer::class, DecoratingMailer::class.'.original')
306+ ->args([service(DecoratingMailer::class.'.original')]);
307+ };
308+
309+ .. note ::
310+
311+ The visibility of the decorated ``App\Mailer `` service (which is an alias
312+ for the new service) will still be the same as the original ``App\Mailer ``
313+ visibility.
314+
315+ .. note ::
316+
317+ All custom :doc: `service tags </service_container/tags >` from the decorated
318+ service are removed in the new service. Only certain built-in service tags
319+ defined by Symfony are retained: ``container.service_locator ``, ``container.service_subscriber ``,
320+ ``kernel.event_subscriber ``, ``kernel.event_listener ``, ``kernel.locale_aware ``,
321+ and ``kernel.reset ``.
294322
295323Decoration Priority
296324-------------------
0 commit comments