Skip to content

Tell about ServiceLocatorTagPass::register() #9990

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
Jul 2, 2018
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
21 changes: 18 additions & 3 deletions service_container/service_subscribers_locators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,24 @@ Now you can use the service locator by injecting it in any other service:
->setArguments(array(new Reference('app.command_handler_locator')))
;

.. tip::
In :doc:`compiler passes </service_container/compiler_passes>` it's recommended
to use the :method:`Symfony\\Component\\DependencyInjection\\Compiler\\ServiceLocatorTagPass::register`
method to create the service locators. This will save you some boilerplate and
will share identical locators amongst all the services referencing them::

use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

public function process(ContainerBuilder $container)
{
//...

$locateableServices = array(
//...
'logger' => new Reference('logger'),
);

If the service locator is not intended to be used by multiple services, it's
better to create and inject it as an anonymous service.
$myService->addArgument(ServiceLocatorTagPass::register($locateableServices));
}

.. _`Command pattern`: https://en.wikipedia.org/wiki/Command_pattern