ServiceSubscriberTrait.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. trigger_deprecation('symfony/contracts', 'v3.5', '"%s" is deprecated, use "ServiceMethodsSubscriberTrait" instead.', ServiceSubscriberTrait::class);
  15. /**
  16. * Implementation of ServiceSubscriberInterface that determines subscribed services
  17. * from methods that have the #[SubscribedService] attribute.
  18. *
  19. * Service ids are available as "ClassName::methodName" so that the implementation
  20. * of subscriber methods can be just `return $this->container->get(__METHOD__);`.
  21. *
  22. * @property ContainerInterface $container
  23. *
  24. * @author Kevin Bond <kevinbond@gmail.com>
  25. *
  26. * @deprecated since symfony/contracts v3.5, use ServiceMethodsSubscriberTrait instead
  27. */
  28. trait ServiceSubscriberTrait
  29. {
  30. public static function getSubscribedServices(): array
  31. {
  32. $services = method_exists(get_parent_class(self::class) ?: '', __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. /* @var SubscribedService $attribute */
  47. $attribute = $attribute->newInstance();
  48. $attribute->key ??= self::class.'::'.$method->name;
  49. $attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;
  50. $attribute->nullable = $returnType->allowsNull();
  51. if ($attribute->attributes) {
  52. $services[] = $attribute;
  53. } else {
  54. $services[$attribute->key] = ($attribute->nullable ? '?' : '').$attribute->type;
  55. }
  56. }
  57. return $services;
  58. }
  59. #[Required]
  60. public function setContainer(ContainerInterface $container): ?ContainerInterface
  61. {
  62. $ret = null;
  63. if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) {
  64. $ret = parent::setContainer($container);
  65. }
  66. $this->container = $container;
  67. return $ret;
  68. }
  69. }