You are here

public function Container::get in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/dependency-injection/Container.php \Symfony\Component\DependencyInjection\Container::get()
  2. 8 core/lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::get()
Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::get()

Gets a service.

Parameters

string $id The service identifier:

int $invalidBehavior The behavior when the service does not exist:

Return value

object The associated service

Throws

ServiceCircularReferenceException When a circular reference is detected

ServiceNotFoundException When the service is not defined

Overrides ContainerInterface::get

See also

Reference

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/modules/system/src/Tests/Bootstrap/ErrorContainer.php
Gets a service.
ExceptionContainer::get in core/modules/system/src/Tests/Bootstrap/ExceptionContainer.php
Gets a service.
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/modules/system/src/Tests/Bootstrap/ErrorContainer.php
Gets a service.
ExceptionContainer::get in core/modules/system/src/Tests/Bootstrap/ExceptionContainer.php
Gets a service.

File

core/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;
}