Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ServiceSubscriberTrait.php

trait ServiceSubscriberTrait

Implementation of ServiceSubscriberInterface that determines subscribed services from methods that have the #[SubscribedService] attribute.

Service ids are available as "ClassName::methodName" so that the implementation of subscriber methods can be just `return $this->container->get(__METHOD__);`.

@property ContainerInterface $container

@author Kevin Bond <kevinbond@gmail.com>

Hierarchy

  • trait \Symfony\Contracts\Service\ServiceSubscriberTrait

Deprecated

since symfony/contracts v3.5, use ServiceMethodsSubscriberTrait instead

File

vendor/symfony/service-contracts/ServiceSubscriberTrait.php, line 33

Namespace

Symfony\Contracts\Service
View source
trait ServiceSubscriberTrait {
    public static function getSubscribedServices() : array {
        $services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : [];
        foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
            if (self::class !== $method->getDeclaringClass()->name) {
                continue;
            }
            if (!($attribute = $method->getAttributes(SubscribedService::class)[0] ?? null)) {
                continue;
            }
            if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
                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));
            }
            if (!($returnType = $method->getReturnType())) {
                throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class));
            }
            
            /* @var SubscribedService $attribute */
            $attribute = $attribute->newInstance();
            $attribute->key ??= self::class . '::' . $method->name;
            $attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;
            $attribute->nullable = $returnType->allowsNull();
            if ($attribute->attributes) {
                $services[] = $attribute;
            }
            else {
                $services[$attribute->key] = ($attribute->nullable ? '?' : '') . $attribute->type;
            }
        }
        return $services;
    }
    public function setContainer(ContainerInterface $container) : ?ContainerInterface {
        $ret = null;
        if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) {
            $ret = parent::setContainer($container);
        }
        $this->container = $container;
        return $ret;
    }

}

Members

Title Sort descending Modifiers Object type Summary
ServiceSubscriberTrait::getSubscribedServices public static function
ServiceSubscriberTrait::setContainer public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal