@@ -243,8 +243,8 @@ Defining a Service Locator
243
243
--------------------------
244
244
245
245
To manually define a service locator, create a new service definition and add
246
- the ``container.service_locator `` tag to it. Use its `` arguments `` option to
247
- include as many services as needed in it.
246
+ the ``container.service_locator `` tag to it. Use the first argument of the
247
+ service definition to pass a collection of services to the service locator:
248
248
249
249
.. configuration-block ::
250
250
@@ -262,6 +262,13 @@ include as many services as needed in it.
262
262
# add the following tag to the service definition:
263
263
# tags: ['container.service_locator']
264
264
265
+ # if the element has no key, the ID of the original service is used
266
+ app.another_command_handler_locator :
267
+ class : Symfony\Component\DependencyInjection\ServiceLocator
268
+ arguments :
269
+ -
270
+ - ' @app.command_handler.baz'
271
+
265
272
.. code-block :: xml
266
273
267
274
<!-- config/services.xml -->
@@ -274,8 +281,10 @@ include as many services as needed in it.
274
281
275
282
<service id =" app.command_handler_locator" class =" Symfony\Component\DependencyInjection\ServiceLocator" >
276
283
<argument type =" collection" >
277
- <argument key =" App\FooCommand" type =" service" id =" app.command_handler.foo" />
278
- <argument key =" App\BarCommand" type =" service" id =" app.command_handler.bar" />
284
+ <argument key =" App\FooCommand" type =" service" id =" app.command_handler.foo" />
285
+ <argument key =" App\BarCommand" type =" service" id =" app.command_handler.bar" />
286
+ <!-- if the element has no key, the ID of the original service is used -->
287
+ <argument type =" service" id =" app.command_handler.baz" />
279
288
</argument >
280
289
<!--
281
290
if you are not using the default service autoconfiguration,
@@ -300,12 +309,24 @@ include as many services as needed in it.
300
309
->setArguments([[
301
310
'App\FooCommand' => new Reference('app.command_handler.foo'),
302
311
'App\BarCommand' => new Reference('app.command_handler.bar'),
303
- ]])
312
+ // if the element has no key, the ID of the original service is used
313
+ new Reference('app.command_handler.baz'),
314
+ )))
304
315
// if you are not using the default service autoconfiguration,
305
316
// add the following tag to the service definition:
306
317
// ->addTag('container.service_locator')
307
318
;
308
319
320
+ .. versionadded :: 4.1
321
+ The service locator autoconfiguration was introduced in Symfony 4.1. In
322
+ previous Symfony versions you always needed to add the
323
+ ``container.service_locator `` tag explicitly.
324
+
325
+ .. versionadded :: 4.2
326
+
327
+ The ability to add services without specifying their id was introduced in
328
+ Symfony 4.2.
329
+
309
330
.. note ::
310
331
311
332
The services defined in the service locator argument must include keys,
0 commit comments