Attribute
9 months ago
Test
9 months ago
ResetInterface.php
4 years ago
ServiceLocatorTrait.php
9 months ago
ServiceProviderInterface.php
4 years ago
ServiceSubscriberInterface.php
4 years ago
ServiceSubscriberTrait.php
9 months ago
index.php
3 years ago
ServiceSubscriberTrait.php
67 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Contracts\Service; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Psr\Container\ContainerInterface; |
| 5 | use MailPoetVendor\Symfony\Contracts\Service\Attribute\SubscribedService; |
| 6 | trait ServiceSubscriberTrait |
| 7 | { |
| 8 | protected $container; |
| 9 | public static function getSubscribedServices() : array |
| 10 | { |
| 11 | $services = \method_exists(\get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : []; |
| 12 | $attributeOptIn = \false; |
| 13 | if (\PHP_VERSION_ID >= 80000) { |
| 14 | foreach ((new \ReflectionClass(self::class))->getMethods() as $method) { |
| 15 | if (self::class !== $method->getDeclaringClass()->name) { |
| 16 | continue; |
| 17 | } |
| 18 | if (!($attribute = $method->getAttributes(SubscribedService::class)[0] ?? null)) { |
| 19 | continue; |
| 20 | } |
| 21 | if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) { |
| 22 | 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)); |
| 23 | } |
| 24 | if (!($returnType = $method->getReturnType())) { |
| 25 | throw new \LogicException(\sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class)); |
| 26 | } |
| 27 | $serviceId = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType; |
| 28 | if ($returnType->allowsNull()) { |
| 29 | $serviceId = '?' . $serviceId; |
| 30 | } |
| 31 | $services[$attribute->newInstance()->key ?? self::class . '::' . $method->name] = $serviceId; |
| 32 | $attributeOptIn = \true; |
| 33 | } |
| 34 | } |
| 35 | if (!$attributeOptIn) { |
| 36 | foreach ((new \ReflectionClass(self::class))->getMethods() as $method) { |
| 37 | if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) { |
| 38 | continue; |
| 39 | } |
| 40 | if (self::class !== $method->getDeclaringClass()->name) { |
| 41 | continue; |
| 42 | } |
| 43 | if (!($returnType = $method->getReturnType()) instanceof \ReflectionNamedType) { |
| 44 | continue; |
| 45 | } |
| 46 | if ($returnType->isBuiltin()) { |
| 47 | continue; |
| 48 | } |
| 49 | if (\PHP_VERSION_ID >= 80000) { |
| 50 | trigger_deprecation('symfony/service-contracts', '2.5', 'Using "%s" in "%s" without using the "%s" attribute on any method is deprecated.', ServiceSubscriberTrait::class, self::class, SubscribedService::class); |
| 51 | } |
| 52 | $services[self::class . '::' . $method->name] = '?' . ($returnType instanceof \ReflectionNamedType ? $returnType->getName() : $returnType); |
| 53 | } |
| 54 | } |
| 55 | return $services; |
| 56 | } |
| 57 | public function setContainer(ContainerInterface $container) |
| 58 | { |
| 59 | $ret = null; |
| 60 | if (\method_exists(\get_parent_class(self::class) ?: '', __FUNCTION__)) { |
| 61 | $ret = parent::setContainer($container); |
| 62 | } |
| 63 | $this->container = $container; |
| 64 | return $ret; |
| 65 | } |
| 66 | } |
| 67 |