function Container::get

Same name in other branches
  1. 9 core/lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::get()
  2. 10 core/lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::get()
  3. 11.x core/lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::get()
4 calls to Container::get()
Container::resolveServicesAndParameters in core/lib/Drupal/Component/DependencyInjection/Container.php
Resolves arguments that represent services or variables to the real values.
ErrorContainer::get in core/tests/Drupal/FunctionalTests/Bootstrap/ErrorContainer.php
ExceptionContainer::get in core/tests/Drupal/FunctionalTests/Bootstrap/ExceptionContainer.php
PhpArrayContainer::resolveServicesAndParameters in core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php
Resolves arguments that represent services or variables to the real values.
2 methods override Container::get()
ErrorContainer::get in core/tests/Drupal/FunctionalTests/Bootstrap/ErrorContainer.php
ExceptionContainer::get in core/tests/Drupal/FunctionalTests/Bootstrap/ExceptionContainer.php

File

core/lib/Drupal/Component/DependencyInjection/Container.php, line 132

Class

Container
Provides a container optimized for Drupal's needs.

Namespace

Drupal\Component\DependencyInjection

Code

public function get($id, $invalid_behavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
    if (isset($this->aliases[$id])) {
        $id = $this->aliases[$id];
    }
    // Re-use shared service instance if it exists.
    if (isset($this->services[$id]) || $invalid_behavior === ContainerInterface::NULL_ON_INVALID_REFERENCE && array_key_exists($id, $this->services)) {
        return $this->services[$id];
    }
    if (isset($this->loading[$id])) {
        throw new ServiceCircularReferenceException($id, array_keys($this->loading));
    }
    $definition = isset($this->serviceDefinitions[$id]) ? $this->serviceDefinitions[$id] : NULL;
    if (!$definition && $invalid_behavior === ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
        if (!$id) {
            throw new ServiceNotFoundException('');
        }
        throw new ServiceNotFoundException($id, NULL, NULL, $this->getServiceAlternatives($id));
    }
    // In case something else than ContainerInterface::NULL_ON_INVALID_REFERENCE
    // is used, the actual wanted behavior is to re-try getting the service at a
    // later point.
    if (!$definition) {
        return;
    }
    // Definition is a keyed array, so [0] is only defined when it is a
    // serialized string.
    if (isset($definition[0])) {
        $definition = unserialize($definition);
    }
    // Now create the service.
    $this->loading[$id] = TRUE;
    try {
        $service = $this->createService($definition, $id);
    } catch (\Exception $e) {
        unset($this->loading[$id]);
        unset($this->services[$id]);
        if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalid_behavior) {
            return;
        }
        throw $e;
    }
    unset($this->loading[$id]);
    return $service;
}

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