| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Contracts\Service; |
| 13 |
|
| 14 |
use Psr\Container\ContainerInterface; |
| 15 |
use Symfony\Contracts\Service\Attribute\Required; |
| 16 |
use Symfony\Contracts\Service\Attribute\SubscribedService; |
| 17 |
|
| 18 |
/** |
| 19 |
* Implementation of ServiceSubscriberInterface that determines subscribed services |
| 20 |
* from methods that have the #[SubscribedService] attribute. |
| 21 |
* |
| 22 |
* Service ids are available as "ClassName::methodName" so that the implementation |
| 23 |
* of subscriber methods can be just `return $this->container->get(__METHOD__);`. |
| 24 |
* |
| 25 |
* @author Kevin Bond <[email protected]> |
| 26 |
*/ |
| 27 |
trait ServiceMethodsSubscriberTrait |
| 28 |
{ |
| 29 |
protected ContainerInterface $container; |
| 30 |
|
| 31 |
public static function getSubscribedServices(): array |
| 32 |
{ |
| 33 |
$services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : []; |
| 34 |
|
| 35 |
$reflectors = [ |
| 36 |
...(new \ReflectionClass(self::class))->getMethods(), |
| 37 |
...(new \ReflectionClass(self::class))->getProperties(), |
| 38 |
]; |
| 39 |
|
| 40 |
foreach ($reflectors as $reflector) { |
| 41 |
if (self::class !== $reflector->class) { |
| 42 |
continue; |
| 43 |
} |
| 44 |
|
| 45 |
if (!$reflectionAttribute = $reflector->getAttributes(SubscribedService::class)[0] ?? null) { |
| 46 |
continue; |
| 47 |
} |
| 48 |
|
| 49 |
if ($reflector instanceof \ReflectionMethod) { |
| 50 |
if ($reflector->isStatic() || $reflector->isAbstract() || $reflector->isGenerator() || $reflector->isInternal() || $reflector->getNumberOfRequiredParameters()) { |
| 51 |
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, $reflector->name)); |
| 52 |
} |
| 53 |
|
| 54 |
if (!$autowireType = $reflector->getReturnType()) { |
| 55 |
throw new \LogicException(\sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $reflector->name, self::class)); |
| 56 |
} |
| 57 |
|
| 58 |
$defaultKey = self::class.'::'.$reflector->name; |
| 59 |
} elseif ($reflector instanceof \ReflectionProperty) { |
| 60 |
if (\PHP_VERSION_ID < 80400 || !$reflector->hasHook(\PropertyHookType::Get)) { |
| 61 |
throw new \LogicException(\sprintf('Cannot use "%s" on property "%s::$%s" (can only be used on properties with a get hook).', SubscribedService::class, self::class, $reflector->name)); |
| 62 |
} |
| 63 |
|
| 64 |
if (!$autowireType = $reflector->getType()) { |
| 65 |
throw new \LogicException(\sprintf('Cannot use "%s" on properties without a type in "%s::$%s".', SubscribedService::class, $reflector->name, self::class)); |
| 66 |
} |
| 67 |
|
| 68 |
$defaultKey = self::class.'::$'.$reflector->name.'::get'; |
| 69 |
} else { |
| 70 |
throw new \LogicException('Unexpected reflector: '.get_debug_type($reflector)); |
| 71 |
} |
| 72 |
|
| 73 |
/* @var SubscribedService $attribute */ |
| 74 |
$attribute = $reflectionAttribute->newInstance(); |
| 75 |
$attribute->key ??= $defaultKey; |
| 76 |
$attribute->type ??= $autowireType instanceof \ReflectionNamedType ? $autowireType->getName() : (string) $autowireType; |
| 77 |
$attribute->nullable = $attribute->nullable ?: $autowireType->allowsNull(); |
| 78 |
|
| 79 |
if ($attribute->attributes) { |
| 80 |
$services[] = $attribute; |
| 81 |
} else { |
| 82 |
$services[$attribute->key] = ($attribute->nullable ? '?' : '').$attribute->type; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $services; |
| 87 |
} |
| 88 |
|
| 89 |
#[Required] |
| 90 |
public function setContainer(ContainerInterface $container): ?ContainerInterface |
| 91 |
{ |
| 92 |
$ret = null; |
| 93 |
if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) { |
| 94 |
$ret = parent::setContainer($container); |
| 95 |
} |
| 96 |
|
| 97 |
$this->container = $container; |
| 98 |
|
| 99 |
return $ret; |
| 100 |
} |
| 101 |
} |