You are here

public function Container::get in Service Container 7

Same name in this branch
  1. 7 lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::get()
  2. 7 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Container.php \Symfony\Component\DependencyInjection\Container::get()
Same name and namespace in other branches
  1. 7.2 lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::get()
3 calls to Container::get()
Container::createInstance in src/DependencyInjection/Container.php
Container::resolveServicesAndParameters in lib/Drupal/Component/DependencyInjection/Container.php
Resolves arguments that represent services or variables to the real values.
PhpArrayContainer::resolveServicesAndParameters in lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php
Resolves arguments that represent services or variables to the real values.

File

lib/Drupal/Component/DependencyInjection/Container.php, line 140
Contains \Drupal\Component\DependencyInjection\Container.

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($id);
    }
    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]);

    // Remove a potentially shared service that was constructed incompletely.
    if (array_key_exists($id, $this->services)) {
      unset($this->services[$id]);
    }
    if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalid_behavior) {
      return;
    }
    throw $e;
  }
  unset($this->loading[$id]);
  return $service;
}