diff options
Diffstat (limited to 'vendor/symfony/service-contracts/ServiceMethodsSubscriberTrait.php')
| -rw-r--r-- | vendor/symfony/service-contracts/ServiceMethodsSubscriberTrait.php | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/vendor/symfony/service-contracts/ServiceMethodsSubscriberTrait.php b/vendor/symfony/service-contracts/ServiceMethodsSubscriberTrait.php new file mode 100644 index 0000000..0d89d9f --- /dev/null +++ b/vendor/symfony/service-contracts/ServiceMethodsSubscriberTrait.php | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | /* | ||
| 4 | * This file is part of the Symfony package. | ||
| 5 | * | ||
| 6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
| 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 <kevinbond@gmail.com> | ||
| 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 | foreach ((new \ReflectionClass(self::class))->getMethods() as $method) { | ||
| 36 | if (self::class !== $method->getDeclaringClass()->name) { | ||
| 37 | continue; | ||
| 38 | } | ||
| 39 | |||
| 40 | if (!$attribute = $method->getAttributes(SubscribedService::class)[0] ?? null) { | ||
| 41 | continue; | ||
| 42 | } | ||
| 43 | |||
| 44 | if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) { | ||
| 45 | 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)); | ||
| 46 | } | ||
| 47 | |||
| 48 | if (!$returnType = $method->getReturnType()) { | ||
| 49 | throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class)); | ||
| 50 | } | ||
| 51 | |||
| 52 | /* @var SubscribedService $attribute */ | ||
| 53 | $attribute = $attribute->newInstance(); | ||
| 54 | $attribute->key ??= self::class.'::'.$method->name; | ||
| 55 | $attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType; | ||
| 56 | $attribute->nullable = $returnType->allowsNull(); | ||
| 57 | |||
| 58 | if ($attribute->attributes) { | ||
| 59 | $services[] = $attribute; | ||
| 60 | } else { | ||
| 61 | $services[$attribute->key] = ($attribute->nullable ? '?' : '').$attribute->type; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | return $services; | ||
| 66 | } | ||
| 67 | |||
| 68 | #[Required] | ||
| 69 | public function setContainer(ContainerInterface $container): ?ContainerInterface | ||
| 70 | { | ||
| 71 | $ret = null; | ||
| 72 | if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) { | ||
| 73 | $ret = parent::setContainer($container); | ||
| 74 | } | ||
| 75 | |||
| 76 | $this->container = $container; | ||
| 77 | |||
| 78 | return $ret; | ||
| 79 | } | ||
| 80 | } | ||
