Skip to content

Commit 2ddcf3e

Browse files
kbondLiinkiing
authored andcommitted
document ServiceSubscriberTrait
1 parent 40c9c0c commit 2ddcf3e

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

service_container/service_subscribers_locators.rst

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,99 @@ will share identical locators amongst all the services referencing them::
379379
}
380380

381381
.. _`Command pattern`: https://en.wikipedia.org/wiki/Command_pattern
382+
383+
Service Subscriber Trait
384+
------------------------
385+
386+
.. versionadded:: 4.2
387+
The :class:`Symfony\\Component\\DependencyInjection\\ServiceSubscriberTrait`
388+
was introduced in Symfony 4.2.
389+
390+
The :class:`Symfony\\Component\\DependencyInjection\\ServiceSubscriberTrait`
391+
provides an implementation for
392+
:class:`Symfony\\Component\\DependencyInjection\\ServiceSubscriberInterface`
393+
that looks through all methods in your class that have no arguments and a return
394+
type. It provides a ``ServiceLocator`` for the services of those return types.
395+
The service id is ``__METHOD__``. This allows you to easily add dependencies
396+
to your services based on type-hinted helper methods::
397+
398+
// src/Service/MyService.php
399+
namespace App\Service;
400+
401+
use Psr\Log\LoggerInterface;
402+
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
403+
use Symfony\Component\DependencyInjection\ServiceSubscriberTrait;
404+
use Symfony\Component\Routing\RouterInterface;
405+
406+
class MyService implements ServiceSubscriberInterface
407+
{
408+
use ServiceSubscriberTrait;
409+
410+
public function doSomething()
411+
{
412+
// $this->router() ...
413+
// $this->logger() ...
414+
}
415+
416+
private function router(): RouterInterface
417+
{
418+
return $this->container->get(__METHOD__);
419+
}
420+
421+
private function logger(): LoggerInterface
422+
{
423+
return $this->container->get(__METHOD__);
424+
}
425+
}
426+
427+
This allows you to create helper traits like RouterAware, LoggerAware, etc...
428+
and compose your services with them::
429+
430+
// src/Service/LoggerAware.php
431+
namespace App\Service;
432+
433+
use Psr\Log\LoggerInterface;
434+
435+
trait LoggerAware
436+
{
437+
private function logger(): LoggerInterface
438+
{
439+
return $this->container->get(__CLASS__.'::'.__FUNCTION__);
440+
}
441+
}
442+
443+
// src/Service/RouterAware.php
444+
namespace App\Service;
445+
446+
use Symfony\Component\Routing\RouterInterface;
447+
448+
trait RouterAware
449+
{
450+
private function router(): RouterInterface
451+
{
452+
return $this->container->get(__CLASS__.'::'.__FUNCTION__);
453+
}
454+
}
455+
456+
// src/Service/MyService.php
457+
namespace App\Service;
458+
459+
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
460+
use Symfony\Component\DependencyInjection\ServiceSubscriberTrait;
461+
462+
class MyService implements ServiceSubscriberInterface
463+
{
464+
use ServiceSubscriberTrait, LoggerAware, RouterAware;
465+
466+
public function doSomething()
467+
{
468+
// $this->router() ...
469+
// $this->logger() ...
470+
}
471+
}
472+
473+
.. caution::
474+
475+
When creating these helper traits, the service id cannot be ``__METHOD__``
476+
as this will include the trait name, not the class name. Instead, use
477+
``__CLASS__.'::'.__FUNCTION__`` as the service id.

0 commit comments

Comments
 (0)