ServiceSubscriberTrait.php 3.9 KB

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