From e2e0b44c946adb88eaa578761b831f4094dd5145 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sun, 1 Jul 2018 18:58:21 +0200 Subject: [PATCH 1/2] Tell about controllers in doc about service subscribers --- .../service_subscribers_locators.rst | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/service_container/service_subscribers_locators.rst b/service_container/service_subscribers_locators.rst index e8d31104ea3..bb12b33a52a 100644 --- a/service_container/service_subscribers_locators.rst +++ b/service_container/service_subscribers_locators.rst @@ -13,7 +13,11 @@ instantiation of the services to be lazy. However, that's not possible using the explicit dependency injection since services are not all meant to be ``lazy`` (see :doc:`/service_container/lazy_services`). -A real-world example are applications that implement the `Command pattern`_ +This can typically be the case in your controllers, where only one action is +usually called per request, and where services are used conditionnally in each +of them. + +Another example are applications that implement the `Command pattern`_ using a CommandBus to map command handlers by Command class names and use them to handle their respective command when it is asked for:: @@ -50,35 +54,12 @@ to handle their respective command when it is asked for:: Considering that only one command is handled at a time, instantiating all the other command handlers is unnecessary. A possible solution to lazy-load the -handlers could be to inject the whole dependency injection container:: - - // ... - use Symfony\Component\DependencyInjection\ContainerInterface; - - class CommandBus - { - private $container; - - public function __construct(ContainerInterface $container) - { - $this->container = $container; - } - - public function handle(Command $command) - { - $commandClass = get_class($command); - - if ($this->container->has($commandClass)) { - $handler = $this->container->get($commandClass); - - return $handler->handle($command); - } - } - } +handlers could be to inject the main dependency injection container. However, injecting the entire container is discouraged because it gives too broad access to existing services and it hides the actual dependencies of the -services. +services. Doing so also requires services to be made public, which isn't the +case by default since Symfony version 4.0. **Service Subscribers** are intended to solve this problem by giving access to a 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 which implements the PSR-11 ``ContainerInterface``, but it is also a callable:: // ... - $locateHandler = $this->locator; - $handler = $locateHandler($commandClass); + $handler = ($this->locator)($commandClass); return $handler->handle($command); @@ -173,6 +153,24 @@ Service types can also be keyed by a service name for internal use:: ]; } +When extending a class that also implements ``ServiceSubscriberInterface``, +it's your responsibility to call the parent when overriding the method. This +typically happens when extending ``AbstractController``:: + + use Psr\Log\LoggerInterface; + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; + + class MyController extends AbstractController + { + public static function getSubscribedServices() + { + return array_merge(parent::getSubscribedServices(), [ + // ... + 'logger' => LoggerInterface::class, + ]); + } + } + Optional Services ~~~~~~~~~~~~~~~~~ From 81738f0e0b2e6075631e21701358dd3c04c67968 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 4 Jul 2018 17:53:29 +0200 Subject: [PATCH 2/2] Minor rewords --- service_container/service_subscribers_locators.rst | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/service_container/service_subscribers_locators.rst b/service_container/service_subscribers_locators.rst index bb12b33a52a..e43adb666ea 100644 --- a/service_container/service_subscribers_locators.rst +++ b/service_container/service_subscribers_locators.rst @@ -13,10 +13,8 @@ instantiation of the services to be lazy. However, that's not possible using the explicit dependency injection since services are not all meant to be ``lazy`` (see :doc:`/service_container/lazy_services`). -This can typically be the case in your controllers, where only one action is -usually called per request, and where services are used conditionnally in each -of them. - +This can typically be the case in your controllers, where you may inject several +services in the constructor, but the action executed only uses some of them. Another example are applications that implement the `Command pattern`_ using a CommandBus to map command handlers by Command class names and use them to handle their respective command when it is asked for:: @@ -59,7 +57,7 @@ handlers could be to inject the main dependency injection container. However, injecting the entire container is discouraged because it gives too broad access to existing services and it hides the actual dependencies of the services. Doing so also requires services to be made public, which isn't the -case by default since Symfony version 4.0. +case by default in Symfony applications. **Service Subscribers** are intended to solve this problem by giving access to a set of predefined services while instantiating them only when actually needed