Skip to content

[DependencyInjection] ServiceSubscriberTrait 5.4 update #16015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions service_container/service_subscribers_locators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand All @@ -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;

Expand All @@ -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__);
Expand All @@ -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__);
Expand All @@ -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__);
Expand Down Expand Up @@ -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