[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/service-contracts/ -> ServiceSubscriberTrait.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <[email protected]>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Contracts\Service;
  13  
  14  use Psr\Container\ContainerInterface;
  15  use Symfony\Contracts\Service\Attribute\SubscribedService;
  16  
  17  /**
  18   * Implementation of ServiceSubscriberInterface that determines subscribed services from
  19   * method return types. Service ids are available as "ClassName::methodName".
  20   *
  21   * @author Kevin Bond <[email protected]>
  22   */
  23  trait ServiceSubscriberTrait
  24  {
  25      /** @var ContainerInterface */
  26      protected $container;
  27  
  28      /**
  29       * {@inheritdoc}
  30       */
  31      public static function getSubscribedServices(): array
  32      {
  33          static $services;
  34  
  35          if (null !== $services) {
  36              return $services;
  37          }
  38  
  39          $services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : [];
  40          $attributeOptIn = false;
  41  
  42          if (\PHP_VERSION_ID >= 80000) {
  43              foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
  44                  if (self::class !== $method->getDeclaringClass()->name) {
  45                      continue;
  46                  }
  47  
  48                  if (!$attribute = $method->getAttributes(SubscribedService::class)[0] ?? null) {
  49                      continue;
  50                  }
  51  
  52                  if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
  53                      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));
  54                  }
  55  
  56                  if (!$returnType = $method->getReturnType()) {
  57                      throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class));
  58                  }
  59  
  60                  $serviceId = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;
  61  
  62                  if ($returnType->allowsNull()) {
  63                      $serviceId = '?'.$serviceId;
  64                  }
  65  
  66                  $services[$attribute->newInstance()->key ?? self::class.'::'.$method->name] = $serviceId;
  67                  $attributeOptIn = true;
  68              }
  69          }
  70  
  71          if (!$attributeOptIn) {
  72              foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
  73                  if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
  74                      continue;
  75                  }
  76  
  77                  if (self::class !== $method->getDeclaringClass()->name) {
  78                      continue;
  79                  }
  80  
  81                  if (!($returnType = $method->getReturnType()) instanceof \ReflectionNamedType) {
  82                      continue;
  83                  }
  84  
  85                  if ($returnType->isBuiltin()) {
  86                      continue;
  87                  }
  88  
  89                  if (\PHP_VERSION_ID >= 80000) {
  90                      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);
  91                  }
  92  
  93                  $services[self::class.'::'.$method->name] = '?'.($returnType instanceof \ReflectionNamedType ? $returnType->getName() : $returnType);
  94              }
  95          }
  96  
  97          return $services;
  98      }
  99  
 100      /**
 101       * @required
 102       *
 103       * @return ContainerInterface|null
 104       */
 105      public function setContainer(ContainerInterface $container)
 106      {
 107          $this->container = $container;
 108  
 109          if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) {
 110              return parent::setContainer($container);
 111          }
 112  
 113          return null;
 114      }
 115  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer