@@ -627,8 +627,9 @@ Service Subscriber Trait
627
627
628
628
The :class: `Symfony\\ Contracts\\ Service\\ ServiceSubscriberTrait ` provides an
629
629
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.
632
633
The service id is ``__METHOD__ ``. This allows you to add dependencies to your
633
634
services based on type-hinted helper methods::
634
635
@@ -637,6 +638,7 @@ services based on type-hinted helper methods::
637
638
638
639
use Psr\Log\LoggerInterface;
639
640
use Symfony\Component\Routing\RouterInterface;
641
+ use Symfony\Contracts\Service\Attribute\SubscribedService;
640
642
use Symfony\Contracts\Service\ServiceSubscriberInterface;
641
643
use Symfony\Contracts\Service\ServiceSubscriberTrait;
642
644
@@ -650,11 +652,13 @@ services based on type-hinted helper methods::
650
652
// $this->logger() ...
651
653
}
652
654
655
+ #[SubscribedService]
653
656
private function router(): RouterInterface
654
657
{
655
658
return $this->container->get(__METHOD__);
656
659
}
657
660
661
+ #[SubscribedService]
658
662
private function logger(): LoggerInterface
659
663
{
660
664
return $this->container->get(__METHOD__);
@@ -668,9 +672,11 @@ and compose your services with them::
668
672
namespace App\Service;
669
673
670
674
use Psr\Log\LoggerInterface;
675
+ use Symfony\Contracts\Service\Attribute\SubscribedService;
671
676
672
677
trait LoggerAware
673
678
{
679
+ #[SubscribedService]
674
680
private function logger(): LoggerInterface
675
681
{
676
682
return $this->container->get(__CLASS__.'::'.__FUNCTION__);
@@ -681,9 +687,11 @@ and compose your services with them::
681
687
namespace App\Service;
682
688
683
689
use Symfony\Component\Routing\RouterInterface;
690
+ use Symfony\Contracts\Service\Attribute\SubscribedService;
684
691
685
692
trait RouterAware
686
693
{
694
+ #[SubscribedService]
687
695
private function router(): RouterInterface
688
696
{
689
697
return $this->container->get(__CLASS__.'::'.__FUNCTION__);
@@ -713,4 +721,12 @@ and compose your services with them::
713
721
as this will include the trait name, not the class name. Instead, use
714
722
``__CLASS__.'::'.__FUNCTION__ `` as the service id.
715
723
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
+
716
732
.. _`Command pattern` : https://en.wikipedia.org/wiki/Command_pattern
0 commit comments