Skip to content

Commit e2e0b44

Browse files
Tell about controllers in doc about service subscribers
1 parent c90f20a commit e2e0b44

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

service_container/service_subscribers_locators.rst

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ instantiation of the services to be lazy. However, that's not possible using
1313
the explicit dependency injection since services are not all meant to
1414
be ``lazy`` (see :doc:`/service_container/lazy_services`).
1515

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 conditionnally in each
18+
of them.
19+
20+
Another example are applications that implement the `Command pattern`_
1721
using a CommandBus to map command handlers by Command class names and use them
1822
to handle their respective command when it is asked for::
1923

@@ -50,35 +54,12 @@ to handle their respective command when it is asked for::
5054

5155
Considering that only one command is handled at a time, instantiating all the
5256
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.
7858

7959
However, injecting the entire container is discouraged because it gives too
8060
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.
8263

8364
**Service Subscribers** are intended to solve this problem by giving access to a
8465
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
139120
which implements the PSR-11 ``ContainerInterface``, but it is also a callable::
140121

141122
// ...
142-
$locateHandler = $this->locator;
143-
$handler = $locateHandler($commandClass);
123+
$handler = ($this->locator)($commandClass);
144124

145125
return $handler->handle($command);
146126

@@ -173,6 +153,24 @@ Service types can also be keyed by a service name for internal use::
173153
];
174154
}
175155

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+
176174
Optional Services
177175
~~~~~~~~~~~~~~~~~
178176

0 commit comments

Comments
 (0)