Skip to content

Commit e112649

Browse files
committed
[DependencyInjection] ServiceSubscriberTrait 5.4 update
1 parent b430be8 commit e112649

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

service_container/service_subscribers_locators.rst

+18-2
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,9 @@ Service Subscriber Trait
627627

628628
The :class:`Symfony\\Contracts\\Service\\ServiceSubscriberTrait` provides an
629629
implementation for :class:`Symfony\\Contracts\\Service\\ServiceSubscriberInterface`
630-
that looks through all methods in your class that have no arguments and a return
631-
type. It provides a ``ServiceLocator`` for the services of those return types.
630+
that looks through all methods in your class that are marked with the
631+
:class:`Symfony\\Contracts\\Service\\Attribute\\SubscribedService` attribute. It
632+
provides a ``ServiceLocator`` for the services of each method's return type.
632633
The service id is ``__METHOD__``. This allows you to add dependencies to your
633634
services based on type-hinted helper methods::
634635

@@ -637,6 +638,7 @@ services based on type-hinted helper methods::
637638

638639
use Psr\Log\LoggerInterface;
639640
use Symfony\Component\Routing\RouterInterface;
641+
use Symfony\Contracts\Service\Attribute\SubscribedService;
640642
use Symfony\Contracts\Service\ServiceSubscriberInterface;
641643
use Symfony\Contracts\Service\ServiceSubscriberTrait;
642644

@@ -650,11 +652,13 @@ services based on type-hinted helper methods::
650652
// $this->logger() ...
651653
}
652654

655+
#[SubscribedService]
653656
private function router(): RouterInterface
654657
{
655658
return $this->container->get(__METHOD__);
656659
}
657660

661+
#[SubscribedService]
658662
private function logger(): LoggerInterface
659663
{
660664
return $this->container->get(__METHOD__);
@@ -668,9 +672,11 @@ and compose your services with them::
668672
namespace App\Service;
669673

670674
use Psr\Log\LoggerInterface;
675+
use Symfony\Contracts\Service\Attribute\SubscribedService;
671676

672677
trait LoggerAware
673678
{
679+
#[SubscribedService]
674680
private function logger(): LoggerInterface
675681
{
676682
return $this->container->get(__CLASS__.'::'.__FUNCTION__);
@@ -681,9 +687,11 @@ and compose your services with them::
681687
namespace App\Service;
682688

683689
use Symfony\Component\Routing\RouterInterface;
690+
use Symfony\Contracts\Service\Attribute\SubscribedService;
684691

685692
trait RouterAware
686693
{
694+
#[SubscribedService]
687695
private function router(): RouterInterface
688696
{
689697
return $this->container->get(__CLASS__.'::'.__FUNCTION__);
@@ -713,4 +721,12 @@ and compose your services with them::
713721
as this will include the trait name, not the class name. Instead, use
714722
``__CLASS__.'::'.__FUNCTION__`` as the service id.
715723

724+
.. deprecated:: 5.4
725+
726+
Defining your *subscribed service* methods with the
727+
:class:`Symfony\\Contracts\\Service\\Attribute\\SubscribedService` attribute
728+
was added in Symfony 5.4. Previously, any methods with no arguments and a
729+
return type were *subscribed*. This still works in 5.4 but is deprecated (only
730+
when using PHP 8) and will be removed in 6.0.
731+
716732
.. _`Command pattern`: https://en.wikipedia.org/wiki/Command_pattern

0 commit comments

Comments
 (0)