Açıklama Yok

ServiceSubscriberTrait.php 3.9KB

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