Skip to content

Commit 7f1c584

Browse files
Tell about ServiceLocatorTagPass::register()
1 parent c90f20a commit 7f1c584

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

service_container/service_subscribers_locators.rst

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,25 @@ Now you can use the service locator by injecting it in any other service:
349349
->setArguments(array(new Reference('app.command_handler_locator')))
350350
;
351351
352-
.. tip::
352+
When inside :doc:`/service_container/compiler_passes`, using the
353+
:method:`Symfony\\Component\\DependencyInjection\\Compiler\\ServiceLocatorTagPass::register`
354+
method is the recommended way to create locators for your services. This will save
355+
you some boilerplate and will automatically share identical locators amongst all the
356+
services referencing them::
357+
358+
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
359+
use Symfony\Component\DependencyInjection\ContainerBuilder;
360+
361+
public function process(ContainerBuilder $container)
362+
{
363+
//...
364+
365+
$locateableServices = array(
366+
//...
367+
'logger' => new Reference('logger'),
368+
);
353369

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

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

0 commit comments

Comments
 (0)