@@ -14,6 +14,9 @@ A real-world example are applications that implement the `Command pattern`_
14
14
using a CommandBus to map command handlers by Command class names and use them
15
15
to handle their respective command when it is asked for::
16
16
17
+ // src/AppBundle/CommandBus.php
18
+ namespace AppBundle;
19
+
17
20
// ...
18
21
class CommandBus
19
22
{
@@ -46,28 +49,29 @@ Considering that only one command is handled at a time, instantiating all the
46
49
other command handlers is unnecessary. A possible solution to lazy-load the
47
50
handlers could be to inject the whole dependency injection container::
48
51
49
- use Symfony\Component\DependencyInjection\ContainerInterface;
52
+ // ...
53
+ use Symfony\Component\DependencyInjection\ContainerInterface;
50
54
51
- class CommandBus
52
- {
53
- private $container;
55
+ class CommandBus
56
+ {
57
+ private $container;
54
58
55
- public function __construct(ContainerInterface $container)
56
- {
57
- $this->container = $container;
58
- }
59
+ public function __construct(ContainerInterface $container)
60
+ {
61
+ $this->container = $container;
62
+ }
59
63
60
- public function handle(Command $command)
61
- {
62
- $commandClass = get_class($command);
64
+ public function handle(Command $command)
65
+ {
66
+ $commandClass = get_class($command);
63
67
64
- if ($this->container->has($commandClass)) {
65
- $handler = $this->container->get($commandClass);
68
+ if ($this->container->has($commandClass)) {
69
+ $handler = $this->container->get($commandClass);
66
70
67
- return $handler->handle($command);
68
- }
71
+ return $handler->handle($command);
69
72
}
70
73
}
74
+ }
71
75
72
76
However, injecting the entire container is discouraged because it gives too
73
77
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
87
91
88
92
.. code-block :: yaml
89
93
94
+ // app/config/services.yml
90
95
services :
91
-
92
96
app.command_handler_locator :
93
97
class : Symfony\Component\DependencyInjection\ServiceLocator
94
98
tags : ['container.service_locator']
@@ -99,6 +103,7 @@ option to include as many services as needed to it and add the
99
103
100
104
.. code-block :: xml
101
105
106
+ <!-- app/config/services.xml -->
102
107
<?xml version =" 1.0" encoding =" UTF-8" ?>
103
108
<container xmlns =" http://symfony.com/schema/dic/services"
104
109
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
119
124
120
125
.. code-block :: php
121
126
127
+ // app/config/services.php
122
128
use Symfony\Component\DependencyInjection\ServiceLocator;
123
129
use Symfony\Component\DependencyInjection\Reference;
124
130
@@ -144,13 +150,14 @@ Now you can use the service locator injecting it in any other service:
144
150
145
151
.. code-block :: yaml
146
152
153
+ // app/config/services.yml
147
154
services :
148
-
149
155
AppBundle\CommandBus :
150
156
arguments : ['@app.command_handler_locator']
151
157
152
158
.. code-block :: xml
153
159
160
+ <!-- app/config/services.xml -->
154
161
<?xml version =" 1.0" encoding =" UTF-8" ?>
155
162
<container xmlns =" http://symfony.com/schema/dic/services"
156
163
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:
159
166
<services >
160
167
161
168
<service id =" AppBundle\CommandBus" >
162
- <argument type =" service" id =" app.command_handler.locator " />
169
+ <argument type =" service" id =" app.command_handler_locator " />
163
170
</service >
164
171
165
172
</services >
166
173
</container >
167
174
168
175
.. code-block :: php
169
176
177
+ // app/config/services.php
170
178
use AppBundle\CommandBus;
171
179
use Symfony\Component\DependencyInjection\Reference;
172
180
173
- //...
174
-
175
181
$container
176
182
->register(CommandBus::class)
177
183
->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:
185
191
Usage
186
192
-----
187
193
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
189
195
service locator::
190
196
191
197
// ...
0 commit comments