@@ -62,16 +62,66 @@ that you can reference it:
62
62
App\Mailer : ~
63
63
64
64
App\DecoratingMailer :
65
- # overrides the App\Mailer service
66
- # but that service is still available as App\Mailer.inner
67
65
decorates : App\Mailer
68
66
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
69
122
# pass the old service as an argument
70
123
arguments : ['@App\DecoratingMailer.inner']
71
124
72
- # private, because usually you do not need to fetch App\DecoratingMailer directly
73
- public : false
74
-
75
125
.. code-block :: xml
76
126
77
127
<!-- config/services.xml -->
@@ -85,7 +135,6 @@ that you can reference it:
85
135
86
136
<service id =" App\DecoratingMailer"
87
137
decorates =" App\Mailer"
88
- public =" false"
89
138
>
90
139
<argument type =" service" id =" App\DecoratingMailer.inner" />
91
140
</service >
@@ -105,16 +154,11 @@ that you can reference it:
105
154
$container->register(DecoratingMailer::class)
106
155
->setDecoratedService(Mailer::class)
107
156
->addArgument(new Reference(DecoratingMailer::class.'.inner'))
108
- ->setPublic(false)
109
157
;
110
158
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
-
115
159
.. tip ::
116
160
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
118
162
for the new service) will still be the same as the original ``App\Mailer ``
119
163
visibility.
120
164
0 commit comments