From e1126491c9443dcc8a4b52bc34dbaba0d4b759d7 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Wed, 27 Oct 2021 10:42:08 -0400 Subject: [PATCH] [DependencyInjection] ServiceSubscriberTrait 5.4 update --- .../service_subscribers_locators.rst | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/service_container/service_subscribers_locators.rst b/service_container/service_subscribers_locators.rst index db75b181446..2459139ed70 100644 --- a/service_container/service_subscribers_locators.rst +++ b/service_container/service_subscribers_locators.rst @@ -627,8 +627,9 @@ Service Subscriber Trait The :class:`Symfony\\Contracts\\Service\\ServiceSubscriberTrait` provides an implementation for :class:`Symfony\\Contracts\\Service\\ServiceSubscriberInterface` -that looks through all methods in your class that have no arguments and a return -type. It provides a ``ServiceLocator`` for the services of those return types. +that looks through all methods in your class that are marked with the +:class:`Symfony\\Contracts\\Service\\Attribute\\SubscribedService` attribute. It +provides a ``ServiceLocator`` for the services of each method's return type. The service id is ``__METHOD__``. This allows you to add dependencies to your services based on type-hinted helper methods:: @@ -637,6 +638,7 @@ services based on type-hinted helper methods:: use Psr\Log\LoggerInterface; use Symfony\Component\Routing\RouterInterface; + use Symfony\Contracts\Service\Attribute\SubscribedService; use Symfony\Contracts\Service\ServiceSubscriberInterface; use Symfony\Contracts\Service\ServiceSubscriberTrait; @@ -650,11 +652,13 @@ services based on type-hinted helper methods:: // $this->logger() ... } + #[SubscribedService] private function router(): RouterInterface { return $this->container->get(__METHOD__); } + #[SubscribedService] private function logger(): LoggerInterface { return $this->container->get(__METHOD__); @@ -668,9 +672,11 @@ and compose your services with them:: namespace App\Service; use Psr\Log\LoggerInterface; + use Symfony\Contracts\Service\Attribute\SubscribedService; trait LoggerAware { + #[SubscribedService] private function logger(): LoggerInterface { return $this->container->get(__CLASS__.'::'.__FUNCTION__); @@ -681,9 +687,11 @@ and compose your services with them:: namespace App\Service; use Symfony\Component\Routing\RouterInterface; + use Symfony\Contracts\Service\Attribute\SubscribedService; trait RouterAware { + #[SubscribedService] private function router(): RouterInterface { return $this->container->get(__CLASS__.'::'.__FUNCTION__); @@ -713,4 +721,12 @@ and compose your services with them:: as this will include the trait name, not the class name. Instead, use ``__CLASS__.'::'.__FUNCTION__`` as the service id. +.. deprecated:: 5.4 + + Defining your *subscribed service* methods with the + :class:`Symfony\\Contracts\\Service\\Attribute\\SubscribedService` attribute + was added in Symfony 5.4. Previously, any methods with no arguments and a + return type were *subscribed*. This still works in 5.4 but is deprecated (only + when using PHP 8) and will be removed in 6.0. + .. _`Command pattern`: https://en.wikipedia.org/wiki/Command_pattern