src/App/Contracts/Service/CustomServiceSubscriberTrait.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Contracts\Service;
  3. use Symfony\Contracts\Service\Attribute\SubscribedService;
  4. /**
  5.  * TODO: this is a copy of another file as the signatures are not compatible; see if this is fixed in later updates...
  6.  */
  7. trait CustomServiceSubscriberTrait
  8. {
  9.     /**
  10.      * {@inheritdoc}
  11.      */
  12.     public static function getSubscribedServices(): array
  13.     {
  14.         $parent get_parent_class(static::class);
  15.         $services = ($parent && method_exists($parent__FUNCTION__)) ? call_user_func([$parent'getSubscribedServices']) : [];
  16.         $attributeOptIn false;
  17.         if (\PHP_VERSION_ID >= 80000) {
  18.             foreach ((new \ReflectionClass(static::class))->getMethods() as $method) {
  19.                 if (static::class !== $method->getDeclaringClass()->name) {
  20.                     continue;
  21.                 }
  22.                 if (!$attribute $method->getAttributes(SubscribedService::class)[0] ?? null) {
  23.                     continue;
  24.                 }
  25.                 if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
  26.                     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));
  27.                 }
  28.                 if (!$returnType $method->getReturnType()) {
  29.                     throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".'SubscribedService::class, $method->nameself::class));
  30.                 }
  31.                 $serviceId $returnType instanceof \ReflectionNamedType $returnType->getName() : (string) $returnType;
  32.                 if ($returnType->allowsNull()) {
  33.                     $serviceId '?'.$serviceId;
  34.                 }
  35.                 $services[$attribute->newInstance()->key ?? static::class.'::'.$method->name] = $serviceId;
  36.                 $attributeOptIn true;
  37.             }
  38.         }
  39.         if (!$attributeOptIn) {
  40.             foreach ((new \ReflectionClass(static::class))->getMethods() as $method) {
  41.                 if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
  42.                     continue;
  43.                 }
  44.                 if (static::class !== $method->getDeclaringClass()->name) {
  45.                     continue;
  46.                 }
  47.                 if (!($returnType $method->getReturnType()) instanceof \ReflectionNamedType) {
  48.                     continue;
  49.                 }
  50.                 if ($returnType->isBuiltin()) {
  51.                     continue;
  52.                 }
  53.                 if (\PHP_VERSION_ID >= 80000) {
  54.                     trigger_deprecation('symfony/service-contracts''2.5''Using "%s" in "%s" without using the "%s" attribute on any method is deprecated.'CustomServiceSubscriberTrait::class, self::class, SubscribedService::class);
  55.                 }
  56.                 $services[static::class.'::'.$method->name] = '?'.($returnType instanceof \ReflectionNamedType $returnType->getName() : $returnType);
  57.             }
  58.         }
  59.         return $services;
  60.     }
  61. }