ServiceSubscriberTrait.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. /**
  13. * Implementation of ServiceSubscriberInterface that determines subscribed services from
  14. * private method return types. Service ids are available as "ClassName::methodName".
  15. *
  16. * @author Kevin Bond <kevinbond@gmail.com>
  17. */
  18. trait ServiceSubscriberTrait
  19. {
  20. /** @var ContainerInterface */
  21. protected $container;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function getSubscribedServices(): array
  26. {
  27. static $services;
  28. if (null !== $services) {
  29. return $services;
  30. }
  31. $services = \is_callable(['parent', __FUNCTION__]) ? parent::getSubscribedServices() : [];
  32. foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
  33. if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
  34. continue;
  35. }
  36. if (self::class !== $method->getDeclaringClass()->name) {
  37. continue;
  38. }
  39. if (!($returnType = $method->getReturnType()) instanceof \ReflectionNamedType) {
  40. continue;
  41. }
  42. if ($returnType->isBuiltin()) {
  43. continue;
  44. }
  45. $services[self::class.'::'.$method->name] = '?'.$returnType->getName();
  46. }
  47. return $services;
  48. }
  49. /**
  50. * @required
  51. *
  52. * @return ContainerInterface|null
  53. */
  54. public function setContainer(ContainerInterface $container)
  55. {
  56. $this->container = $container;
  57. if (\is_callable(['parent', __FUNCTION__])) {
  58. return parent::setContainer($container);
  59. }
  60. return null;
  61. }
  62. }