Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Document ServiceSubscriberTrait #9809

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
Jun 11, 2018
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
96 changes: 96 additions & 0 deletions service_container/service_subscribers_locators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,99 @@ Now you can use the service locator by injecting it in any other service:
better to create and inject it as an anonymous service.

.. _`Command pattern`: https://en.wikipedia.org/wiki/Command_pattern

Service Subscriber Trait
------------------------

.. versionadded:: 4.2
The :class:`Symfony\\Component\\DependencyInjection\\ServiceSubscriberTrait`
was introduced in Symfony 4.2.

The :class:`Symfony\\Component\\DependencyInjection\\ServiceSubscriberTrait`
provides an implementation for
:class:`Symfony\\Component\\DependencyInjection\\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.
The service id is ``__METHOD__``. This allows you to easily add dependencies
to your services based on type-hinted helper methods::

// src/Service/MyService.php
namespace App\Service;

use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberTrait;
use Symfony\Component\Routing\RouterInterface;

class MyService implements ServiceSubscriberInterface
{
use ServiceSubscriberTrait;

public function doSomething()
{
// $this->router() ...
// $this->logger() ...
}

private function router(): RouterInterface
{
return $this->container->get(__METHOD__);
}

private function logger(): LoggerInterface
{
return $this->container->get(__METHOD__);
}
}

This allows you to create helper traits like RouterAware, LoggerAware, etc...
and compose your services with them::

// src/Service/LoggerAware.php
namespace App\Service;

use Psr\Log\LoggerInterface;

trait LoggerAware
{
private function logger(): LoggerInterface
{
return $this->container->get(__CLASS__.'::'.__FUNCTION__);
}
}

// src/Service/RouterAware.php
namespace App\Service;

use Symfony\Component\Routing\RouterInterface;

trait RouterAware
{
private function router(): RouterInterface
{
return $this->container->get(__CLASS__.'::'.__FUNCTION__);
}
}

// src/Service/MyService.php
namespace App\Service;

use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberTrait;

class MyService implements ServiceSubscriberInterface
{
use ServiceSubscriberTrait, LoggerAware, RouterAware;

public function doSomething()
{
// $this->router() ...
// $this->logger() ...
}
}

.. caution::

When creating these helper traits, the service id cannot be ``__METHOD__``
as this will include the trait name, not the class name. Instead, use
``__CLASS__.'::'.__FUNCTION__`` as the service id.