function ReverseContainer::getId

Same name in other branches
  1. 9 core/lib/Drupal/Component/DependencyInjection/ReverseContainer.php \Drupal\Component\DependencyInjection\ReverseContainer::getId()
  2. 11.x core/lib/Drupal/Component/DependencyInjection/ReverseContainer.php \Drupal\Component\DependencyInjection\ReverseContainer::getId()

Returns the ID of the passed object when it exists as a service.

To be reversible, services need to be public.

Parameters

object $service: The service to find the ID for.

File

core/lib/Drupal/Component/DependencyInjection/ReverseContainer.php, line 49

Class

ReverseContainer
Retrieves service IDs from the container for public services.

Namespace

Drupal\Component\DependencyInjection

Code

public function getId(object $service) : ?string {
    if ($this->serviceContainer === $service || $service instanceof SymfonyContainerInterface) {
        return 'service_container';
    }
    $hash = $this->generateServiceIdHash($service);
    $id = self::$recordedServices[$hash] ?? ($this->getServiceId)($service);
    if ($id !== NULL && $this->serviceContainer
        ->has($id)) {
        self::$recordedServices[$hash] = $id;
        return $id;
    }
    return NULL;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.