ServiceMethodsSubscriberTrait.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Contracts\Service;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Contracts\Service\Attribute\Required;
  13. use Symfony\Contracts\Service\Attribute\SubscribedService;
  14. /**
  15. * Implementation of ServiceSubscriberInterface that determines subscribed services
  16. * from methods that have the #[SubscribedService] attribute.
  17. *
  18. * Service ids are available as "ClassName::methodName" so that the implementation
  19. * of subscriber methods can be just `return $this->container->get(__METHOD__);`.
  20. *
  21. * @author Kevin Bond <kevinbond@gmail.com>
  22. */
  23. trait ServiceMethodsSubscriberTrait
  24. {
  25. protected ContainerInterface $container;
  26. public static function getSubscribedServices(): array
  27. {
  28. $services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : [];
  29. $reflectors = [
  30. ...(new \ReflectionClass(self::class))->getMethods(),
  31. ...(new \ReflectionClass(self::class))->getProperties(),
  32. ];
  33. foreach ($reflectors as $reflector) {
  34. if (self::class !== $reflector->class) {
  35. continue;
  36. }
  37. if (!$reflectionAttribute = $reflector->getAttributes(SubscribedService::class)[0] ?? null) {
  38. continue;
  39. }
  40. if ($reflector instanceof \ReflectionMethod) {
  41. if ($reflector->isStatic() || $reflector->isAbstract() || $reflector->isGenerator() || $reflector->isInternal() || $reflector->getNumberOfRequiredParameters()) {
  42. 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, $reflector->name));
  43. }
  44. if (!$autowireType = $reflector->getReturnType()) {
  45. throw new \LogicException(\sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $reflector->name, self::class));
  46. }
  47. $defaultKey = self::class.'::'.$reflector->name;
  48. } elseif ($reflector instanceof \ReflectionProperty) {
  49. if (\PHP_VERSION_ID < 80400 || !$reflector->hasHook(\PropertyHookType::Get)) {
  50. throw new \LogicException(\sprintf('Cannot use "%s" on property "%s::$%s" (can only be used on properties with a get hook).', SubscribedService::class, self::class, $reflector->name));
  51. }
  52. if (!$autowireType = $reflector->getType()) {
  53. throw new \LogicException(\sprintf('Cannot use "%s" on properties without a type in "%s::$%s".', SubscribedService::class, $reflector->name, self::class));
  54. }
  55. $defaultKey = self::class.'::$'.$reflector->name.'::get';
  56. } else {
  57. throw new \LogicException('Unexpected reflector: '.get_debug_type($reflector));
  58. }
  59. /* @var SubscribedService $attribute */
  60. $attribute = $reflectionAttribute->newInstance();
  61. $attribute->key ??= $defaultKey;
  62. $attribute->type ??= $autowireType instanceof \ReflectionNamedType ? $autowireType->getName() : (string) $autowireType;
  63. $attribute->nullable = $attribute->nullable ?: $autowireType->allowsNull();
  64. if ($attribute->attributes) {
  65. $services[] = $attribute;
  66. } else {
  67. $services[$attribute->key] = ($attribute->nullable ? '?' : '').$attribute->type;
  68. }
  69. }
  70. return $services;
  71. }
  72. #[Required]
  73. public function setContainer(ContainerInterface $container): ?ContainerInterface
  74. {
  75. $ret = null;
  76. if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) {
  77. $ret = parent::setContainer($container);
  78. }
  79. $this->container = $container;
  80. return $ret;
  81. }
  82. }