Attribute
1 year ago
Test
1 year ago
CHANGELOG.md
1 year ago
LICENSE
1 year ago
README.md
1 year ago
ResetInterface.php
1 year ago
ServiceCollectionInterface.php
1 year ago
ServiceLocatorTrait.php
1 year ago
ServiceMethodsSubscriberTrait.php
1 year ago
ServiceProviderInterface.php
1 year ago
ServiceSubscriberInterface.php
1 year ago
ServiceSubscriberTrait.php
1 year ago
ServiceMethodsSubscriberTrait.php
81 lines
| 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 = $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 | } |
| 81 |