Skip to content

Commit de28f4c

Browse files
committed
minor #10397 [DependencyInjection] Add documentation for service locator changes (codedmonkey, javiereguiluz)
This PR was submitted for the master branch but it was merged into the 4.3 branch instead (closes #10397). Discussion ---------- [DependencyInjection] Add documentation for service locator changes Adds documentation for PR symfony/symfony#28571 Commits ------- 8d8c65e Reword bc5ef85 [DependencyInjection] Add documentation for service locator changes
2 parents 61f4ecc + 8d8c65e commit de28f4c

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

service_container/service_subscribers_locators.rst

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ Defining a Service Locator
243243
--------------------------
244244

245245
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:
248248

249249
.. configuration-block::
250250

@@ -258,6 +258,9 @@ include as many services as needed in it.
258258
-
259259
App\FooCommand: '@app.command_handler.foo'
260260
App\BarCommand: '@app.command_handler.bar'
261+
# if the element has no key, the ID of the original service is used
262+
'@app.command_handler.baz'
263+
261264
# if you are not using the default service autoconfiguration,
262265
# add the following tag to the service definition:
263266
# tags: ['container.service_locator']
@@ -274,8 +277,10 @@ include as many services as needed in it.
274277
275278
<service id="app.command_handler_locator" class="Symfony\Component\DependencyInjection\ServiceLocator">
276279
<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"/>
280+
<argument key="App\FooCommand" type="service" id="app.command_handler.foo" />
281+
<argument key="App\BarCommand" type="service" id="app.command_handler.bar" />
282+
<!-- if the element has no key, the ID of the original service is used -->
283+
<argument type="service" id="app.command_handler.baz" />
279284
</argument>
280285
<!--
281286
if you are not using the default service autoconfiguration,
@@ -300,12 +305,24 @@ include as many services as needed in it.
300305
->setArguments([[
301306
'App\FooCommand' => new Reference('app.command_handler.foo'),
302307
'App\BarCommand' => new Reference('app.command_handler.bar'),
303-
]])
308+
// if the element has no key, the ID of the original service is used
309+
new Reference('app.command_handler.baz'),
310+
)))
304311
// if you are not using the default service autoconfiguration,
305312
// add the following tag to the service definition:
306313
// ->addTag('container.service_locator')
307314
;
308315
316+
.. versionadded:: 4.1
317+
The service locator autoconfiguration was introduced in Symfony 4.1. In
318+
previous Symfony versions you always needed to add the
319+
``container.service_locator`` tag explicitly.
320+
321+
.. versionadded:: 4.2
322+
323+
The ability to add services without specifying their id was introduced in
324+
Symfony 4.2.
325+
309326
.. note::
310327

311328
The services defined in the service locator argument must include keys,

0 commit comments

Comments
 (0)