| 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 WindPressDeps\Symfony\Contracts\Service; |
| 12 |
|
| 13 |
use WindPressDeps\Psr\Container\ContainerInterface; |
| 14 |
use WindPressDeps\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 |
*/ |
| 21 |
trait ServiceSubscriberTrait |
| 22 |
{ |
| 23 |
/** @var ContainerInterface */ |
| 24 |
protected $container; |
| 25 |
/** |
| 26 |
* {@inheritdoc} |
| 27 |
*/ |
| 28 |
public static function getSubscribedServices(): array |
| 29 |
{ |
| 30 |
$services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : []; |
| 31 |
$attributeOptIn = \false; |
| 32 |
if (\PHP_VERSION_ID >= 80000) { |
| 33 |
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) { |
| 34 |
if (self::class !== $method->getDeclaringClass()->name) { |
| 35 |
continue; |
| 36 |
} |
| 37 |
if (!$attribute = $method->getAttributes(SubscribedService::class)[0] ?? null) { |
| 38 |
continue; |
| 39 |
} |
| 40 |
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) { |
| 41 |
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)); |
| 42 |
} |
| 43 |
if (!$returnType = $method->getReturnType()) { |
| 44 |
throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class)); |
| 45 |
} |
| 46 |
$serviceId = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType; |
| 47 |
if ($returnType->allowsNull()) { |
| 48 |
$serviceId = '?' . $serviceId; |
| 49 |
} |
| 50 |
$services[$attribute->newInstance()->key ?? self::class . '::' . $method->name] = $serviceId; |
| 51 |
$attributeOptIn = \true; |
| 52 |
} |
| 53 |
} |
| 54 |
if (!$attributeOptIn) { |
| 55 |
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) { |
| 56 |
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
if (self::class !== $method->getDeclaringClass()->name) { |
| 60 |
continue; |
| 61 |
} |
| 62 |
if (!($returnType = $method->getReturnType()) instanceof \ReflectionNamedType) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
if ($returnType->isBuiltin()) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
if (\PHP_VERSION_ID >= 80000) { |
| 69 |
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); |
| 70 |
} |
| 71 |
$services[self::class . '::' . $method->name] = '?' . ($returnType instanceof \ReflectionNamedType ? $returnType->getName() : $returnType); |
| 72 |
} |
| 73 |
} |
| 74 |
return $services; |
| 75 |
} |
| 76 |
/** |
| 77 |
* @required |
| 78 |
* |
| 79 |
* @return ContainerInterface|null |
| 80 |
*/ |
| 81 |
public function setContainer(ContainerInterface $container) |
| 82 |
{ |
| 83 |
$ret = null; |
| 84 |
if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) { |
| 85 |
$ret = parent::setContainer($container); |
| 86 |
} |
| 87 |
$this->container = $container; |
| 88 |
return $ret; |
| 89 |
} |
| 90 |
} |
| 91 |
|