@@ -13,7 +13,11 @@ instantiation of the services to be lazy. However, that's not possible using
13
13
the explicit dependency injection since services are not all meant to
14
14
be ``lazy `` (see :doc: `/service_container/lazy_services `).
15
15
16
- A real-world example are applications that implement the `Command pattern `_
16
+ This can typically be the case in your controllers, where only one action is
17
+ usually called per request, and where services are used conditionnaly in each
18
+ of them.
19
+
20
+ Another example are applications that implement the `Command pattern `_
17
21
using a CommandBus to map command handlers by Command class names and use them
18
22
to handle their respective command when it is asked for::
19
23
@@ -50,35 +54,12 @@ to handle their respective command when it is asked for::
50
54
51
55
Considering that only one command is handled at a time, instantiating all the
52
56
other command handlers is unnecessary. A possible solution to lazy-load the
53
- handlers could be to inject the whole dependency injection container::
54
-
55
- // ...
56
- use Symfony\Component\DependencyInjection\ContainerInterface;
57
-
58
- class CommandBus
59
- {
60
- private $container;
61
-
62
- public function __construct(ContainerInterface $container)
63
- {
64
- $this->container = $container;
65
- }
66
-
67
- public function handle(Command $command)
68
- {
69
- $commandClass = get_class($command);
70
-
71
- if ($this->container->has($commandClass)) {
72
- $handler = $this->container->get($commandClass);
73
-
74
- return $handler->handle($command);
75
- }
76
- }
77
- }
57
+ handlers could be to inject the main dependency injection container.
78
58
79
59
However, injecting the entire container is discouraged because it gives too
80
60
broad access to existing services and it hides the actual dependencies of the
81
- services.
61
+ services. Doing so also requires services to be made public, which isn't the
62
+ case by default since Symfony version 4.0.
82
63
83
64
**Service Subscribers ** are intended to solve this problem by giving access to a
84
65
set of predefined services while instantiating them only when actually needed
@@ -139,8 +120,7 @@ The injected service is an instance of :class:`Symfony\\Component\\DependencyInj
139
120
which implements the PSR-11 ``ContainerInterface ``, but it is also a callable::
140
121
141
122
// ...
142
- $locateHandler = $this->locator;
143
- $handler = $locateHandler($commandClass);
123
+ $handler = ($this->locator)($commandClass);
144
124
145
125
return $handler->handle($command);
146
126
@@ -173,6 +153,24 @@ Service types can also be keyed by a service name for internal use::
173
153
];
174
154
}
175
155
156
+ When extending a class that also implements ``ServiceSubscriberInterface ``,
157
+ it's your responsibility to call the parent when overriding the method. This
158
+ typically happens when extending ``AbstractController ``::
159
+
160
+ use Psr\Log\LoggerInterface;
161
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
162
+
163
+ class MyController extends AbstractController
164
+ {
165
+ public static function getSubscribedServices()
166
+ {
167
+ return array_merge(parent::getSubscribedServices(), [
168
+ // ...
169
+ 'logger' => LoggerInterface::class,
170
+ ]);
171
+ }
172
+ }
173
+
176
174
Optional Services
177
175
~~~~~~~~~~~~~~~~~
178
176
0 commit comments