Skip to content

Commit 91cd416

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: [DependencyInjection] Updated the explanation of the inner argument renaming
2 parents 734508f + e319ee7 commit 91cd416

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

service_container/service_decoration.rst

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -157,55 +157,72 @@ automatically changed to ``'.inner'``):
157157
->args([service('.inner')]);
158158
};
159159
160-
.. note::
160+
When decorating a service, the original service (e.g. ``App\Mailer``) is available
161+
inside the decorating service (e.g. ``App\DecoratingMailer``) using an ID constructed
162+
as "Decorating service ID" + the ``.inner`` suffix (e.g. in the previous example,
163+
the ID is ``'App\DecoratingMailer.inner'``). You can control the inner service
164+
name via the ``decoration_inner_name`` option:
161165

162-
The visibility of the decorated ``App\Mailer`` service (which is an alias
163-
for the new service) will still be the same as the original ``App\Mailer``
164-
visibility.
166+
.. configuration-block::
165167

166-
.. note::
168+
.. code-block:: php-attributes
167169
168-
All custom :doc:`service tags </service_container/tags>` from the decorated
169-
service are removed in the new service. Only certain built-in service tags
170-
defined by Symfony are retained: ``container.service_locator``, ``container.service_subscriber``,
171-
``kernel.event_subscriber``, ``kernel.event_listener``, ``kernel.locale_aware``,
172-
and ``kernel.reset``.
170+
// when using the #[AutowireDecorated] attribute, you can name the argument
171+
// that holds the decorated service however you like, without needing to
172+
// configure that name explicitly
173+
#[AutowireDecorated] private Mailer $originalMailer,
173174
174-
.. note::
175+
.. code-block:: yaml
175176
176-
The generated inner id is based on the id of the decorator service
177-
(``App\DecoratingMailer`` here), not of the decorated service (``App\Mailer``
178-
here). You can control the inner service name via the ``decoration_inner_name``
179-
option:
177+
# config/services.yaml
178+
services:
179+
App\DecoratingMailer:
180+
# ...
181+
decoration_inner_name: 'original_mailer'
182+
arguments: ['@original_mailer']
180183
181-
.. configuration-block::
184+
# if you decorate a lot of services, consider adding the full
185+
# original service ID as part of the new ID
186+
decoration_inner_name: 'App\Mailer.original'
187+
arguments: ['@App\Mailer.original']
182188
183-
.. code-block:: yaml
189+
.. code-block:: php
184190
185-
# config/services.yaml
186-
services:
187-
App\DecoratingMailer:
188-
# ...
189-
decoration_inner_name: App\DecoratingMailer.wooz
190-
arguments: ['@App\DecoratingMailer.wooz']
191+
// config/services.php
192+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
191193
192-
.. code-block:: php
194+
use App\DecoratingMailer;
195+
use App\Mailer;
196+
197+
return function(ContainerConfigurator $container): void {
198+
$services = $container->services();
193199
194-
// config/services.php
195-
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
200+
$services->set(Mailer::class);
201+
202+
$services->set(DecoratingMailer::class)
203+
->decorate(Mailer::class, 'original_mailer')
204+
->args([service('original_mailer')]);
205+
206+
// if you decorate a lot of services, consider adding the full
207+
// original service ID as part of the new ID
208+
$services->set(DecoratingMailer::class)
209+
->decorate(Mailer::class, DecoratingMailer::class.'.original')
210+
->args([service(DecoratingMailer::class.'.original')]);
211+
};
196212
197-
use App\DecoratingMailer;
198-
use App\Mailer;
213+
.. note::
199214

200-
return function(ContainerConfigurator $container): void {
201-
$services = $container->services();
215+
The visibility of the decorated ``App\Mailer`` service (which is an alias
216+
for the new service) will still be the same as the original ``App\Mailer``
217+
visibility.
202218

203-
$services->set(Mailer::class);
219+
.. note::
204220

205-
$services->set(DecoratingMailer::class)
206-
->decorate(Mailer::class, DecoratingMailer::class.'.wooz')
207-
->args([service(DecoratingMailer::class.'.wooz')]);
208-
};
221+
All custom :doc:`service tags </service_container/tags>` from the decorated
222+
service are removed in the new service. Only certain built-in service tags
223+
defined by Symfony are retained: ``container.service_locator``, ``container.service_subscriber``,
224+
``kernel.event_subscriber``, ``kernel.event_listener``, ``kernel.locale_aware``,
225+
and ``kernel.reset``.
209226

210227
Decoration Priority
211228
-------------------

0 commit comments

Comments
 (0)