2021-08-27 06:46:27 -04:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Contracts\Service ;
use Psr\Container\ContainerInterface ;
2022-03-14 16:22:30 -04:00
use Symfony\Contracts\Service\Attribute\SubscribedService ;
2021-08-27 06:46:27 -04:00
/**
* Implementation of ServiceSubscriberInterface that determines subscribed services from
2022-03-14 16:22:30 -04:00
* method return types . Service ids are available as " ClassName::methodName " .
2021-08-27 06:46:27 -04:00
*
* @ author Kevin Bond < kevinbond @ gmail . com >
*/
trait ServiceSubscriberTrait
{
/** @var ContainerInterface */
protected $container ;
/**
* { @ inheritdoc }
*/
public static function getSubscribedServices () : array
{
static $services ;
if ( null !== $services ) {
return $services ;
}
$services = \is_callable ([ 'parent' , __FUNCTION__ ]) ? parent :: getSubscribedServices () : [];
foreach (( new \ReflectionClass ( self :: class )) -> getMethods () as $method ) {
2022-03-14 16:22:30 -04:00
if ( self :: class !== $method -> getDeclaringClass () -> name ) {
continue ;
}
if ( ! $attribute = $method -> getAttributes ( SubscribedService :: class )[ 0 ] ? ? null ) {
2021-08-27 06:46:27 -04:00
continue ;
}
2022-03-14 16:22:30 -04:00
if ( $method -> isStatic () || $method -> isAbstract () || $method -> isGenerator () || $method -> isInternal () || $method -> getNumberOfRequiredParameters ()) {
throw new \LogicException ( sprintf ( 'Cannot use "%s" on method "%s::%s()" (can only be used on non-static, non-abstract methods with no parameters).' , SubscribedService :: class , self :: class , $method -> name ));
2021-08-27 06:46:27 -04:00
}
2022-03-14 16:22:30 -04:00
if ( ! $returnType = $method -> getReturnType ()) {
throw new \LogicException ( sprintf ( 'Cannot use "%s" on methods without a return type in "%s::%s()".' , SubscribedService :: class , $method -> name , self :: class ));
}
$serviceId = $returnType instanceof \ReflectionNamedType ? $returnType -> getName () : ( string ) $returnType ;
if ( $returnType -> allowsNull ()) {
$serviceId = '?' . $serviceId ;
}
$services [ $attribute -> newInstance () -> key ? ? self :: class . '::' . $method -> name ] = $serviceId ;
2021-08-27 06:46:27 -04:00
}
return $services ;
}
/**
* @ required
*/
2022-03-14 16:22:30 -04:00
public function setContainer ( ContainerInterface $container ) : ? ContainerInterface
2021-08-27 06:46:27 -04:00
{
$this -> container = $container ;
if ( \is_callable ([ 'parent' , __FUNCTION__ ])) {
return parent :: setContainer ( $container );
}
return null ;
}
}