ServiceMethodsSubscriberTrait.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
  30. if (self::class !== $method->getDeclaringClass()->name) {
  31. continue;
  32. }
  33. if (!$attribute = $method->getAttributes(SubscribedService::class)[0] ?? null) {
  34. continue;
  35. }
  36. if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
  37. 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));
  38. }
  39. if (!$returnType = $method->getReturnType()) {
  40. throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class));
  41. }
  42. /* @var SubscribedService $attribute */
  43. $attribute = $attribute->newInstance();
  44. $attribute->key ??= self::class.'::'.$method->name;
  45. $attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;
  46. $attribute->nullable = $returnType->allowsNull();
  47. if ($attribute->attributes) {
  48. $services[] = $attribute;
  49. } else {
  50. $services[$attribute->key] = ($attribute->nullable ? '?' : '').$attribute->type;
  51. }
  52. }
  53. return $services;
  54. }
  55. #[Required]
  56. public function setContainer(ContainerInterface $container): ?ContainerInterface
  57. {
  58. $ret = null;
  59. if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) {
  60. $ret = parent::setContainer($container);
  61. }
  62. $this->container = $container;
  63. return $ret;
  64. }
  65. }