@@ -13,7 +13,9 @@ 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 you may inject several
17
+ services in the constructor, but the action executed only uses some of them.
18
+ Another example are applications that implement the `Command pattern `_
17
19
using a CommandBus to map command handlers by Command class names and use them
18
20
to handle their respective command when it is asked for::
19
21
@@ -50,35 +52,12 @@ to handle their respective command when it is asked for::
50
52
51
53
Considering that only one command is handled at a time, instantiating all the
52
54
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
- }
55
+ handlers could be to inject the main dependency injection container.
78
56
79
57
However, injecting the entire container is discouraged because it gives too
80
58
broad access to existing services and it hides the actual dependencies of the
81
- services.
59
+ services. Doing so also requires services to be made public, which isn't the
60
+ case by default in Symfony applications.
82
61
83
62
**Service Subscribers ** are intended to solve this problem by giving access to a
84
63
set of predefined services while instantiating them only when actually needed
@@ -139,8 +118,7 @@ The injected service is an instance of :class:`Symfony\\Component\\DependencyInj
139
118
which implements the PSR-11 ``ContainerInterface ``, but it is also a callable::
140
119
141
120
// ...
142
- $locateHandler = $this->locator;
143
- $handler = $locateHandler($commandClass);
121
+ $handler = ($this->locator)($commandClass);
144
122
145
123
return $handler->handle($command);
146
124
@@ -173,6 +151,24 @@ Service types can also be keyed by a service name for internal use::
173
151
];
174
152
}
175
153
154
+ When extending a class that also implements ``ServiceSubscriberInterface ``,
155
+ it's your responsibility to call the parent when overriding the method. This
156
+ typically happens when extending ``AbstractController ``::
157
+
158
+ use Psr\Log\LoggerInterface;
159
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
160
+
161
+ class MyController extends AbstractController
162
+ {
163
+ public static function getSubscribedServices()
164
+ {
165
+ return array_merge(parent::getSubscribedServices(), [
166
+ // ...
167
+ 'logger' => LoggerInterface::class,
168
+ ]);
169
+ }
170
+ }
171
+
176
172
Optional Services
177
173
~~~~~~~~~~~~~~~~~
178
174
0 commit comments