Attribute
2 years ago
Test
1 month ago
ResetInterface.php
2 years ago
ServiceLocatorTrait.php
1 month ago
ServiceProviderInterface.php
2 years ago
ServiceSubscriberInterface.php
1 month ago
ServiceSubscriberTrait.php
1 month ago
ServiceSubscriberTrait.php
65 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 | namespace IAWPSCOPED\Symfony\Contracts\Service; |
| 12 | |
| 13 | use IAWPSCOPED\Psr\Container\ContainerInterface; |
| 14 | use IAWPSCOPED\Symfony\Contracts\Service\Attribute\SubscribedService; |
| 15 | /** |
| 16 | * Implementation of ServiceSubscriberInterface that determines subscribed services from |
| 17 | * method return types. Service ids are available as "ClassName::methodName". |
| 18 | * |
| 19 | * @author Kevin Bond <kevinbond@gmail.com> |
| 20 | * @internal |
| 21 | */ |
| 22 | trait ServiceSubscriberTrait |
| 23 | { |
| 24 | /** @var ContainerInterface */ |
| 25 | protected $container; |
| 26 | /** |
| 27 | * {@inheritdoc} |
| 28 | */ |
| 29 | public static function getSubscribedServices() : array |
| 30 | { |
| 31 | $services = \method_exists(\get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : []; |
| 32 | foreach ((new \ReflectionClass(self::class))->getMethods() as $method) { |
| 33 | if (self::class !== $method->getDeclaringClass()->name) { |
| 34 | continue; |
| 35 | } |
| 36 | if (!($attribute = $method->getAttributes(SubscribedService::class)[0] ?? null)) { |
| 37 | continue; |
| 38 | } |
| 39 | if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) { |
| 40 | 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)); |
| 41 | } |
| 42 | if (!($returnType = $method->getReturnType())) { |
| 43 | throw new \LogicException(\sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class)); |
| 44 | } |
| 45 | $serviceId = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType; |
| 46 | if ($returnType->allowsNull()) { |
| 47 | $serviceId = '?' . $serviceId; |
| 48 | } |
| 49 | $services[$attribute->newInstance()->key ?? self::class . '::' . $method->name] = $serviceId; |
| 50 | } |
| 51 | return $services; |
| 52 | } |
| 53 | /** |
| 54 | * @required |
| 55 | */ |
| 56 | public function setContainer(ContainerInterface $container) : ?ContainerInterface |
| 57 | { |
| 58 | $this->container = $container; |
| 59 | if (\method_exists(\get_parent_class(self::class) ?: '', __FUNCTION__)) { |
| 60 | return parent::setContainer($container); |
| 61 | } |
| 62 | return null; |
| 63 | } |
| 64 | } |
| 65 |