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