-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[DI] Add section about service locators #7458
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
.. index:: | ||
single: DependencyInjection; Service Locators | ||
|
||
Service Locators | ||
================ | ||
|
||
Sometimes, a service needs access to several other services without being sure | ||
that all of them will actually be used. In those cases, you may want the | ||
instantiation of the services to be lazy. However, that's not possible using | ||
the explicit dependency injection since services are not all meant to | ||
be ``lazy`` (see :doc:`/service_container/lazy_services`). | ||
|
||
A real-world example are applications that implement the `Command pattern`_ | ||
using a CommandBus to map command handlers by Command class names and use them | ||
to handle their respective command when it is asked for:: | ||
|
||
// ... | ||
class CommandBus | ||
{ | ||
/** | ||
* @var CommandHandler[] | ||
*/ | ||
private $handlerMap; | ||
|
||
public function __construct(array $handlerMap) | ||
{ | ||
$this->handlerMap = $handlerMap; | ||
} | ||
|
||
public function handle(Command $command) | ||
{ | ||
$commandClass = get_class($command) | ||
|
||
if (!isset($this->handlerMap[$commandClass])) { | ||
return; | ||
} | ||
|
||
return $this->handlerMap[$commandClass]->handle($command); | ||
} | ||
} | ||
|
||
// ... | ||
$commandBus->handle(new FooCommand()); | ||
|
||
Considering that only one command is handled at a time, instantiating all the | ||
other command handlers is unnecessary. A possible solution to lazy-load the | ||
handlers could be to inject the whole dependency injection container:: | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
class CommandBus | ||
{ | ||
private $container; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
public function handle(Command $command) | ||
{ | ||
$commandClass = get_class($command) | ||
|
||
if ($this->container->has($commandClass)) { | ||
$handler = $this->container->get($commandClass); | ||
|
||
return $handler->handle($command); | ||
} | ||
} | ||
} | ||
|
||
However, injecting the entire container is discouraged because it gives too | ||
broad access to existing services and it hides the actual dependencies of the | ||
services. | ||
|
||
**Service Locators** are intended to solve this problem by giving access to a | ||
set of predefined services while instantiating them only when actually needed. | ||
|
||
Defining a Service Locator | ||
-------------------------- | ||
|
||
First, define a new service for the service locator. Use its ``arguments`` | ||
option to include as many services as needed to it and add the | ||
``container.service_locator`` tag to turn it into a service locator: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
services: | ||
|
||
app.command_handler_locator: | ||
class: Symfony\Component\DependencyInjection\ServiceLocator | ||
tags: ['container.service_locator'] | ||
arguments: | ||
- | ||
AppBundle\FooCommand: '@app.command_handler.foo' | ||
AppBundle\BarCommand: '@app.command_handler.bar' | ||
|
||
.. code-block:: 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 http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
|
||
<service id="app.command_handler_locator" class="Symfony\Component\DependencyInjection\ServiceLocator"> | ||
<argument type="collection"> | ||
<argument key="AppBundle\FooCommand" type="service" id="app.command_handler.foo" /> | ||
<argument key="AppBundle\BarCommand" type="service" id="app.command_handler.bar" /> | ||
</argument> | ||
<tag name="container.service_locator" /> | ||
</service> | ||
|
||
</services> | ||
</container> | ||
|
||
.. code-block:: php | ||
|
||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
//... | ||
|
||
$container | ||
->register('app.command_handler_locator', ServiceLocator::class) | ||
->addTag('container.service_locator') | ||
->setArguments(array(array( | ||
'AppBundle\FooCommand' => new Reference('app.command_handler.foo'), | ||
'AppBundle\BarCommand' => new Reference('app.command_handler.bar'), | ||
))) | ||
; | ||
|
||
.. note:: | ||
|
||
The services defined in the service locator argument must include keys, | ||
which later become their unique identifiers inside the locator. | ||
|
||
Now you can use the service locator injecting it in any other service: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
services: | ||
|
||
AppBundle\CommandBus: | ||
arguments: ['@app.command_handler_locator'] | ||
|
||
.. code-block:: 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 http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
|
||
<service id="AppBundle\CommandBus"> | ||
<argument type="service" id="app.command_handler.locator" /> | ||
</service> | ||
|
||
</services> | ||
</container> | ||
|
||
.. code-block:: php | ||
|
||
use AppBundle\CommandBus; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
//... | ||
|
||
$container | ||
->register(CommandBus::class) | ||
->setArguments(array(new Reference('app.command_handler_locator'))) | ||
; | ||
|
||
.. tip:: | ||
|
||
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. | ||
|
||
Usage | ||
----- | ||
|
||
Back to the previous CommandBus example, it looks like this when using the | ||
service locator:: | ||
|
||
// ... | ||
use Psr\Container\ContainerInterface; | ||
|
||
class CommandBus | ||
{ | ||
/** | ||
* @var ContainerInterface | ||
*/ | ||
private $handlerLocator; | ||
|
||
// ... | ||
|
||
public function handle(Command $command) | ||
{ | ||
$commandClass = get_class($command); | ||
|
||
if (!$this->handlerLocator->has($commandClass)) { | ||
return; | ||
} | ||
|
||
$handler = $this->handlerLocator->get($commandClass); | ||
|
||
return $handler->handle($command); | ||
} | ||
} | ||
|
||
The injected service is an instance of :class:`Symfony\\Component\\DependencyInjection\\ServiceLocator` | ||
which implements the PSR-11 ``ContainerInterface``, but it is also a callable:: | ||
|
||
// ... | ||
$locateHandler = $this->handlerLocator; | ||
$handler = $locateHandler($commandClass); | ||
|
||
return $handler->handle($command); | ||
|
||
.. _`Command pattern`: https://en.wikipedia.org/wiki/Command_pattern |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not only
$handler = $this->locateHandler($commandClass);
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you mean
$this->handlerLocator($commandClass)
:).Anyway, the intermediate variable is mandatory, calling
$this->handlerLocator()
would make the engine look for ahandlerLocator()
method instead of executing the property as a callable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intermediate variable assignment is necessary for php to understand it's a callable property, and not a method.
But as of PHP7, it could be
$handler = ($this->handlerLocator)($commandClass)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, good to know. Let's stay simple there though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(But this is documentation. I think the current code sample is great for this purpose 😄 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok guys, thanks!