Skip to content

[DependencyInjection] Add documentation for service locator changes #10397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions service_container/service_subscribers_locators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ Defining a Service Locator
--------------------------

To manually define a service locator, create a new service definition and add
the ``container.service_locator`` tag to it. Use its ``arguments`` option to
include as many services as needed in it.
the ``container.service_locator`` tag to it. Use the first argument of the
service definition to pass a collection of services to the service locator:

.. configuration-block::

Expand All @@ -258,6 +258,9 @@ include as many services as needed in it.
-
App\FooCommand: '@app.command_handler.foo'
App\BarCommand: '@app.command_handler.bar'
# if the element has no key, the ID of the original service is used
'@app.command_handler.baz'

# if you are not using the default service autoconfiguration,
# add the following tag to the service definition:
# tags: ['container.service_locator']
Expand All @@ -274,8 +277,10 @@ include as many services as needed in it.

<service id="app.command_handler_locator" class="Symfony\Component\DependencyInjection\ServiceLocator">
<argument type="collection">
<argument key="App\FooCommand" type="service" id="app.command_handler.foo"/>
<argument key="App\BarCommand" type="service" id="app.command_handler.bar"/>
<argument key="App\FooCommand" type="service" id="app.command_handler.foo" />
<argument key="App\BarCommand" type="service" id="app.command_handler.bar" />
<!-- if the element has no key, the ID of the original service is used -->
<argument type="service" id="app.command_handler.baz" />
</argument>
<!--
if you are not using the default service autoconfiguration,
Expand All @@ -300,12 +305,24 @@ include as many services as needed in it.
->setArguments([[
'App\FooCommand' => new Reference('app.command_handler.foo'),
'App\BarCommand' => new Reference('app.command_handler.bar'),
]])
// if the element has no key, the ID of the original service is used
new Reference('app.command_handler.baz'),
)))
// if you are not using the default service autoconfiguration,
// add the following tag to the service definition:
// ->addTag('container.service_locator')
;

.. versionadded:: 4.1
The service locator autoconfiguration was introduced in Symfony 4.1. In
previous Symfony versions you always needed to add the
``container.service_locator`` tag explicitly.

.. versionadded:: 4.2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a blank line after the directive


The ability to add services without specifying their id was introduced in
Symfony 4.2.

.. note::

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