ServiceSubscriberTrait.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. 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. }
  52. return $services;
  53. }
  54. /**
  55. * @required
  56. */
  57. public function setContainer(ContainerInterface $container): ?ContainerInterface
  58. {
  59. $this->container = $container;
  60. if (\is_callable(['parent', __FUNCTION__])) {
  61. return parent::setContainer($container);
  62. }
  63. return null;
  64. }
  65. }