Skip to content

[DependencyInjection] add tagged argument locator #15821

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

Open
wants to merge 1 commit into
base: 5.4
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions service_container/service_subscribers_locators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,52 @@ other services. To do so, create a new service definition using the
The services defined in the service locator argument must include keys,
which later become their unique identifiers inside the locator.

.. versionadded:: 5.4

Since Symfony 5.4 you can create a reusable service locator from tagged services too:

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:
app.command_handler_locator:
class: Symfony\Component\DependencyInjection\ServiceLocator
arguments: [!tagged_iterator { tag: 'app.handler', default_index_method: 'getCommandName' }]

.. code-block:: xml

<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>

<service id="app.command_handler_locator" class="Symfony\Component\DependencyInjection\ServiceLocator">
<argument type="tagged_iterator" tag="app.handler" default-index-method="getCommandName"/>
</service>

</services>
</container>

.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\DependencyInjection\ServiceLocator;

return function(ContainerConfigurator $configurator) {
$services = $configurator->services();

$services->set('app.command_handler_locator', ServiceLocator::class)
->args([tagged_iterator('app.handler', null, 'getCommandName')])
;
};

Now you can inject the service locator in any other services:

.. configuration-block::
Expand Down