@@ -149,7 +149,7 @@ Anonymous Services
149
149
150
150
.. note ::
151
151
152
- Anonymous services are only supported by the XML configuration format .
152
+ Anonymous services are only supported by the XML and YAML configuration formats .
153
153
154
154
In some cases, you may want to prevent a service being used as a dependency of
155
155
other services. This can be achieved by creating an anonymous service. These
@@ -158,23 +158,74 @@ created where they are used.
158
158
159
159
The following example shows how to inject an anonymous service into another service:
160
160
161
- .. code-block :: xml
161
+ .. configuration-block ::
162
+
163
+ .. code-block :: yaml
164
+
165
+ # app/config/services.yml
166
+ services :
167
+ _defaults :
168
+ autowire : true
169
+
170
+ AppBundle\Foo :
171
+ arguments :
172
+ - !service
173
+ class : AppBundle\AnonymousBar
174
+ autowire : true
175
+
176
+ .. code-block :: xml
177
+
178
+ <!-- app/config/services.xml -->
179
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
180
+ <container xmlns =" http://symfony.com/schema/dic/services"
181
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
182
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
183
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
184
+
185
+ <services >
186
+ <defaults autowire =" true" />
187
+
188
+ <service id =" foo" class =" AppBundle\Foo" >
189
+ <argument type =" service" >
190
+ <service class =" AppBundle\AnonymousBar" />
191
+ </argument >
192
+ </service >
193
+ </services >
194
+ </container >
162
195
163
- <!-- app/config/services.xml -->
164
- <?xml version =" 1.0" encoding =" UTF-8" ?>
165
- <container xmlns =" http://symfony.com/schema/dic/services"
166
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
167
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
168
- http://symfony.com/schema/dic/services/services-1.0.xsd" >
196
+ Using an anonymous service as a factory looks like this:
169
197
170
- <services >
171
- <service id =" foo" class =" AppBundle\Foo" >
172
- <argument type =" service" >
173
- <service class =" AppBundle\AnonymousBar" />
174
- </argument >
175
- </service >
176
- </services >
177
- </container >
198
+ .. configuration-block ::
199
+
200
+ .. code-block :: yaml
201
+
202
+ # app/config/services.yml
203
+ services :
204
+ _defaults :
205
+ autowire : true
206
+
207
+ AppBundle\Foo :
208
+ factory : [ !service { class: AppBundle\FooFactory }, 'constructFoo' ]
209
+
210
+ .. code-block :: xml
211
+
212
+ <!-- app/config/services.xml -->
213
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
214
+ <container xmlns =" http://symfony.com/schema/dic/services"
215
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
216
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
217
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
218
+
219
+ <services >
220
+ <defaults autowire =" true" />
221
+
222
+ <service id =" foo" class =" AppBundle\Foo" >
223
+ <factory method =" constructFoo" >
224
+ <service class =" App\FooFactory" />
225
+ </factory >
226
+ </service >
227
+ </services >
228
+ </container >
178
229
179
230
Deprecating Services
180
231
--------------------
@@ -186,8 +237,8 @@ or you decided not to maintain it anymore), you can deprecate its definition:
186
237
187
238
.. code-block :: yaml
188
239
189
- AppBundle\Service\OldService :
190
- deprecated : The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.
240
+ AppBundle\Service\OldService :
241
+ deprecated : The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.
191
242
192
243
.. code-block :: xml
193
244
0 commit comments