Skip to content

Commit a3abf25

Browse files
committed
Documented the autowiring of the decorated services
1 parent cfed0b8 commit a3abf25

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

service_container/service_decoration.rst

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,66 @@ that you can reference it:
6262
App\Mailer: ~
6363
6464
App\DecoratingMailer:
65-
# overrides the App\Mailer service
66-
# but that service is still available as App\Mailer.inner
6765
decorates: App\Mailer
6866
67+
.. code-block:: xml
68+
69+
<!-- config/services.xml -->
70+
<?xml version="1.0" encoding="UTF-8" ?>
71+
<container xmlns="http://symfony.com/schema/dic/services"
72+
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
73+
xsd:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
74+
75+
<services>
76+
<service id="App\Mailer" />
77+
78+
<service id="App\DecoratingMailer"
79+
decorates="App\Mailer"
80+
/>
81+
82+
</services>
83+
</container>
84+
85+
.. code-block:: php
86+
87+
// config/services.php
88+
use App\DecoratingMailer;
89+
use App\Mailer;
90+
use Symfony\Component\DependencyInjection\Reference;
91+
92+
$container->register(Mailer::class);
93+
94+
$container->register(DecoratingMailer::class)
95+
->setDecoratedService(Mailer::class)
96+
;
97+
98+
The ``decorates`` option tells the container that the ``App\DecoratingMailer``
99+
service replaces the ``App\Mailer`` service. If you're using the
100+
:ref:`default services.yaml configuration <service-container-services-load-example>`,
101+
the decorated service is automatically injected into the decorating service
102+
constructor. The name of the argument is ``decorating_service_id + '.inner'``
103+
(in this example, ``App\DecoratingMailer.inner``).
104+
105+
.. versionadded:: 4.1
106+
The autowiring of the decorated service was introduced in Symfony 4.1.
107+
108+
If you are not using autowiring or the decorating service has more than one
109+
constructor argument of the type of the decorated service, you must inject the
110+
decorated service explicitly:
111+
112+
.. configuration-block::
113+
114+
.. code-block:: yaml
115+
116+
# config/services.yaml
117+
services:
118+
App\Mailer: ~
119+
120+
App\DecoratingMailer:
121+
decorates: App\Mailer
69122
# pass the old service as an argument
70123
arguments: ['@App\DecoratingMailer.inner']
71124
72-
# private, because usually you do not need to fetch App\DecoratingMailer directly
73-
public: false
74-
75125
.. code-block:: xml
76126
77127
<!-- config/services.xml -->
@@ -85,7 +135,6 @@ that you can reference it:
85135
86136
<service id="App\DecoratingMailer"
87137
decorates="App\Mailer"
88-
public="false"
89138
>
90139
<argument type="service" id="App\DecoratingMailer.inner" />
91140
</service>
@@ -105,16 +154,11 @@ that you can reference it:
105154
$container->register(DecoratingMailer::class)
106155
->setDecoratedService(Mailer::class)
107156
->addArgument(new Reference(DecoratingMailer::class.'.inner'))
108-
->setPublic(false)
109157
;
110158
111-
The ``decorates`` option tells the container that the ``App\DecoratingMailer`` service
112-
replaces the ``App\Mailer`` service. The old ``App\Mailer`` service is renamed to
113-
``App\DecoratingMailer.inner`` so you can inject it into your new service.
114-
115159
.. tip::
116160

117-
The visibility (public) of the decorated ``App\Mailer`` service (which is an alias
161+
The visibility of the decorated ``App\Mailer`` service (which is an alias
118162
for the new service) will still be the same as the original ``App\Mailer``
119163
visibility.
120164

0 commit comments

Comments
 (0)