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

Breadcrumb

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

class ReverseContainer

Same name in this branch
  1. 11.1.x core/lib/Drupal/Component/DependencyInjection/ReverseContainer.php \Drupal\Component\DependencyInjection\ReverseContainer

Turns public and "container.reversible" services back to their ids.

@author Nicolas Grekas <p@tchwork.com>

Hierarchy

  • class \Symfony\Component\DependencyInjection\ReverseContainer

Expanded class hierarchy of ReverseContainer

File

vendor/symfony/dependency-injection/ReverseContainer.php, line 22

Namespace

Symfony\Component\DependencyInjection
View source
final class ReverseContainer {
    private \Closure $getServiceId;
    public function __construct(Container $serviceContainer, ContainerInterface $reversibleLocator, string $tagName = 'container.reversible') {
        $this->getServiceId = \Closure::bind(fn(object $service): ?string => (array_search($service, $this->services, true) ?: array_search($service, $this->privates, true)) ?: null, $serviceContainer, Container::class);
    }
    
    /**
     * Returns the id of the passed object when it exists as a service.
     *
     * To be reversible, services need to be either public or be tagged with "container.reversible".
     */
    public function getId(object $service) : ?string {
        if ($this->serviceContainer === $service) {
            return 'service_container';
        }
        if (null === ($id = ($this->getServiceId)($service))) {
            return null;
        }
        if ($this->serviceContainer
            ->has($id) || $this->reversibleLocator
            ->has($id)) {
            return $id;
        }
        return null;
    }
    
    /**
     * @throws ServiceNotFoundException When the service is not reversible
     */
    public function getService(string $id) : object {
        if ($this->reversibleLocator
            ->has($id)) {
            return $this->reversibleLocator
                ->get($id);
        }
        if (isset($this->serviceContainer
            ->getRemovedIds()[$id])) {
            throw new ServiceNotFoundException($id, null, null, [], \sprintf('The "%s" service is private and cannot be accessed by reference. You should either make it public, or tag it as "%s".', $id, $this->tagName));
        }
        return $this->serviceContainer
            ->get($id);
    }

}

Members

Title Sort descending Modifiers Object type Summary
ReverseContainer::$getServiceId private property
ReverseContainer::getId public function Returns the id of the passed object when it exists as a service.
ReverseContainer::getService public function
ReverseContainer::__construct public function
RSS feed
Powered by Drupal