Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6e523f7

Browse filesBrowse files
committed
minor #8398 some tweaks for the service locator chapter (xabbuh)
This PR was merged into the 3.3 branch. Discussion ---------- some tweaks for the service locator chapter Commits ------- 47e28dd some tweaks for the service locator chapter
2 parents c17710f + 47e28dd commit 6e523f7
Copy full SHA for 6e523f7

File tree

Expand file treeCollapse file tree

1 file changed

+27
-21
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+27
-21
lines changed

‎service_container/service_locators.rst

Copy file name to clipboardExpand all lines: service_container/service_locators.rst
+27-21Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ A real-world example are applications that implement the `Command pattern`_
1414
using a CommandBus to map command handlers by Command class names and use them
1515
to handle their respective command when it is asked for::
1616

17+
// src/AppBundle/CommandBus.php
18+
namespace AppBundle;
19+
1720
// ...
1821
class CommandBus
1922
{
@@ -46,28 +49,29 @@ Considering that only one command is handled at a time, instantiating all the
4649
other command handlers is unnecessary. A possible solution to lazy-load the
4750
handlers could be to inject the whole dependency injection container::
4851

49-
use Symfony\Component\DependencyInjection\ContainerInterface;
52+
// ...
53+
use Symfony\Component\DependencyInjection\ContainerInterface;
5054

51-
class CommandBus
52-
{
53-
private $container;
55+
class CommandBus
56+
{
57+
private $container;
5458

55-
public function __construct(ContainerInterface $container)
56-
{
57-
$this->container = $container;
58-
}
59+
public function __construct(ContainerInterface $container)
60+
{
61+
$this->container = $container;
62+
}
5963

60-
public function handle(Command $command)
61-
{
62-
$commandClass = get_class($command);
64+
public function handle(Command $command)
65+
{
66+
$commandClass = get_class($command);
6367

64-
if ($this->container->has($commandClass)) {
65-
$handler = $this->container->get($commandClass);
68+
if ($this->container->has($commandClass)) {
69+
$handler = $this->container->get($commandClass);
6670

67-
return $handler->handle($command);
68-
}
71+
return $handler->handle($command);
6972
}
7073
}
74+
}
7175

7276
However, injecting the entire container is discouraged because it gives too
7377
broad access to existing services and it hides the actual dependencies of the
@@ -87,8 +91,8 @@ option to include as many services as needed to it and add the
8791

8892
.. code-block:: yaml
8993
94+
// app/config/services.yml
9095
services:
91-
9296
app.command_handler_locator:
9397
class: Symfony\Component\DependencyInjection\ServiceLocator
9498
tags: ['container.service_locator']
@@ -99,6 +103,7 @@ option to include as many services as needed to it and add the
99103
100104
.. code-block:: xml
101105
106+
<!-- app/config/services.xml -->
102107
<?xml version="1.0" encoding="UTF-8" ?>
103108
<container xmlns="http://symfony.com/schema/dic/services"
104109
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -119,6 +124,7 @@ option to include as many services as needed to it and add the
119124
120125
.. code-block:: php
121126
127+
// app/config/services.php
122128
use Symfony\Component\DependencyInjection\ServiceLocator;
123129
use Symfony\Component\DependencyInjection\Reference;
124130
@@ -144,13 +150,14 @@ Now you can use the service locator injecting it in any other service:
144150

145151
.. code-block:: yaml
146152
153+
// app/config/services.yml
147154
services:
148-
149155
AppBundle\CommandBus:
150156
arguments: ['@app.command_handler_locator']
151157
152158
.. code-block:: xml
153159
160+
<!-- app/config/services.xml -->
154161
<?xml version="1.0" encoding="UTF-8" ?>
155162
<container xmlns="http://symfony.com/schema/dic/services"
156163
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -159,19 +166,18 @@ Now you can use the service locator injecting it in any other service:
159166
<services>
160167
161168
<service id="AppBundle\CommandBus">
162-
<argument type="service" id="app.command_handler.locator" />
169+
<argument type="service" id="app.command_handler_locator" />
163170
</service>
164171
165172
</services>
166173
</container>
167174
168175
.. code-block:: php
169176
177+
// app/config/services.php
170178
use AppBundle\CommandBus;
171179
use Symfony\Component\DependencyInjection\Reference;
172180
173-
//...
174-
175181
$container
176182
->register(CommandBus::class)
177183
->setArguments(array(new Reference('app.command_handler_locator')))
@@ -185,7 +191,7 @@ Now you can use the service locator injecting it in any other service:
185191
Usage
186192
-----
187193

188-
Back to the previous CommandBus example, it looks like this when using the
194+
Back to the previous ``CommandBus`` example, it looks like this when using the
189195
service locator::
190196

191197
// ...

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.