Skip to content

Commit 44f1f4d

Browse files
committed
minor #9499 Documented the autowiring of the decorated services (javiereguiluz)
This PR was merged into the master branch. Discussion ---------- Documented the autowiring of the decorated services This fixes #9486. Commits ------- a3abf25 Documented the autowiring of the decorated services
2 parents ab04c65 + a3abf25 commit 44f1f4d

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

service_container/service_decoration.rst

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,64 @@ that you can reference it:
6666
# but that service is still available as App\DecoratingMailer.inner
6767
decorates: App\Mailer
6868
69+
.. code-block:: xml
70+
71+
<!-- config/services.xml -->
72+
<?xml version="1.0" encoding="UTF-8" ?>
73+
<container xmlns="http://symfony.com/schema/dic/services"
74+
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
75+
xsd:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
76+
77+
<services>
78+
<service id="App\Mailer" />
79+
80+
<service id="App\DecoratingMailer"
81+
decorates="App\Mailer"
82+
/>
83+
84+
</services>
85+
</container>
86+
87+
.. code-block:: php
88+
89+
// config/services.php
90+
use App\DecoratingMailer;
91+
use App\Mailer;
92+
use Symfony\Component\DependencyInjection\Reference;
93+
94+
$container->register(Mailer::class);
95+
96+
$container->register(DecoratingMailer::class)
97+
->setDecoratedService(Mailer::class)
98+
;
99+
100+
The ``decorates`` option tells the container that the ``App\DecoratingMailer``
101+
service replaces the ``App\Mailer`` service. If you're using the
102+
:ref:`default services.yaml configuration <service-container-services-load-example>`,
103+
the decorated service is automatically injected when the constructor of the
104+
decorating service has one argument type-hinted with the decorated service class.
105+
106+
.. versionadded:: 4.1
107+
The autowiring of the decorated service was introduced in Symfony 4.1.
108+
109+
If you are not using autowiring or the decorating service has more than one
110+
constructor argument type-hinted with the decorated service class, you must
111+
inject the decorated service explicitly (the ID of the decorated service is
112+
automatically changed to ``decorating_service_id + '.inner'``):
113+
114+
.. configuration-block::
115+
116+
.. code-block:: yaml
117+
118+
# config/services.yaml
119+
services:
120+
App\Mailer: ~
121+
122+
App\DecoratingMailer:
123+
decorates: App\Mailer
69124
# pass the old service as an argument
70125
arguments: ['@App\DecoratingMailer.inner']
71126
72-
# private, because usually you do not need to fetch App\DecoratingMailer directly
73-
public: false
74-
75127
.. code-block:: xml
76128
77129
<!-- config/services.xml -->
@@ -85,7 +137,6 @@ that you can reference it:
85137
86138
<service id="App\DecoratingMailer"
87139
decorates="App\Mailer"
88-
public="false"
89140
>
90141
<argument type="service" id="App\DecoratingMailer.inner" />
91142
</service>
@@ -105,16 +156,11 @@ that you can reference it:
105156
$container->register(DecoratingMailer::class)
106157
->setDecoratedService(Mailer::class)
107158
->addArgument(new Reference(DecoratingMailer::class.'.inner'))
108-
->setPublic(false)
109159
;
110160
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-
115161
.. tip::
116162

117-
The visibility (public) of the decorated ``App\Mailer`` service (which is an alias
163+
The visibility of the decorated ``App\Mailer`` service (which is an alias
118164
for the new service) will still be the same as the original ``App\Mailer``
119165
visibility.
120166

0 commit comments

Comments
 (0)