@@ -151,7 +151,10 @@ Anonymous Services
151
151
152
152
.. note ::
153
153
154
- Anonymous services are only supported by the XML configuration format.
154
+ Anonymous services are only supported by the XML and YAML configuration formats.
155
+
156
+ .. versionadded :: 3.3
157
+ The feature to configure anonymous services in YAML was introduced in Symfony 3.3.
155
158
156
159
In some cases, you may want to prevent a service being used as a dependency of
157
160
other services. This can be achieved by creating an anonymous service. These
@@ -160,23 +163,63 @@ created where they are used.
160
163
161
164
The following example shows how to inject an anonymous service into another service:
162
165
163
- .. code-block :: xml
164
-
165
- <!-- app/config/services.xml -->
166
- <?xml version =" 1.0" encoding =" UTF-8" ?>
167
- <container xmlns =" http://symfony.com/schema/dic/services"
168
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
169
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
170
- http://symfony.com/schema/dic/services/services-1.0.xsd" >
171
-
172
- <services >
173
- <service id =" foo" class =" App\Foo" >
174
- <argument type =" service" >
175
- <service class =" App\AnonymousBar" />
176
- </argument >
177
- </service >
178
- </services >
179
- </container >
166
+ .. configuration-block ::
167
+
168
+ .. code-block :: yaml
169
+
170
+ # app/config/services.yaml
171
+ services :
172
+ App\Foo :
173
+ arguments :
174
+ - !service
175
+ class : App\AnonymousBar
176
+
177
+ .. code-block :: xml
178
+
179
+ <!-- app/config/services.xml -->
180
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
181
+ <container xmlns =" http://symfony.com/schema/dic/services"
182
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
183
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
184
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
185
+
186
+ <services >
187
+ <service id =" foo" class =" App\Foo" >
188
+ <argument type =" service" >
189
+ <service class =" App\AnonymousBar" />
190
+ </argument >
191
+ </service >
192
+ </services >
193
+ </container >
194
+
195
+ Using an anonymous service as a factory looks like this:
196
+
197
+ .. configuration-block ::
198
+
199
+ .. code-block :: yaml
200
+
201
+ # app/config/services.yaml
202
+ services :
203
+ App\Foo :
204
+ factory : [ !service { class: App\FooFactory }, 'constructFoo' ]
205
+
206
+ .. code-block :: xml
207
+
208
+ <!-- app/config/services.xml -->
209
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
210
+ <container xmlns =" http://symfony.com/schema/dic/services"
211
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
212
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
213
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
214
+
215
+ <services >
216
+ <service id =" foo" class =" App\Foo" >
217
+ <factory method =" constructFoo" >
218
+ <service class =" App\FooFactory" />
219
+ </factory >
220
+ </service >
221
+ </services >
222
+ </container >
180
223
181
224
Deprecating Services
182
225
--------------------
0 commit comments