Skip to content

Commit 6d19d08

Browse files
committed
minor #9990 Tell about ServiceLocatorTagPass::register() (nicolas-grekas, javiereguiluz)
This PR was merged into the 3.4 branch. Discussion ---------- Tell about ServiceLocatorTagPass::register() Fixes #7784 Commits ------- b3a3905 Minor reword 7f1c584 Tell about ServiceLocatorTagPass::register()
2 parents c90f20a + b3a3905 commit 6d19d08

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

service_container/service_subscribers_locators.rst

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,24 @@ 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+
In :doc:`compiler passes </service_container/compiler_passes>` it's recommended
353+
to use the :method:`Symfony\\Component\\DependencyInjection\\Compiler\\ServiceLocatorTagPass::register`
354+
method to create the service locators. This will save you some boilerplate and
355+
will share identical locators amongst all the services referencing them::
356+
357+
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
358+
use Symfony\Component\DependencyInjection\ContainerBuilder;
359+
360+
public function process(ContainerBuilder $container)
361+
{
362+
//...
363+
364+
$locateableServices = array(
365+
//...
366+
'logger' => new Reference('logger'),
367+
);
353368

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.
369+
$myService->addArgument(ServiceLocatorTagPass::register($locateableServices));
370+
}
356371

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

0 commit comments

Comments
 (0)