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 vendor/symfony/dependency-injection/Container.php \Symfony\Component\DependencyInjection\Container::get()

Gets a service.

If a service is defined both through a set() method and with a get{$id}Service() method, the former has always precedence.

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

\Exception if an exception has been thrown when the service has been resolved

Overrides ContainerInterface::get

See also

Reference

File

vendor/symfony/dependency-injection/Container.php, line 253

Class

Container
Container is a dependency injection container.

Namespace

Symfony\Component\DependencyInjection

Code

public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) {

  // Attempt to retrieve the service by checking first aliases then
  // available services. Service IDs are case insensitive, however since
  // this method can be called thousands of times during a request, avoid
  // calling strtolower() unless necessary.
  for ($i = 2;;) {
    if ('service_container' === $id) {
      return $this;
    }
    if (isset($this->aliases[$id])) {
      $id = $this->aliases[$id];
    }

    // Re-use shared service instance if it exists.
    if (isset($this->services[$id]) || array_key_exists($id, $this->services)) {
      return $this->services[$id];
    }
    if (isset($this->loading[$id])) {
      throw new ServiceCircularReferenceException($id, array_keys($this->loading));
    }
    if (isset($this->methodMap[$id])) {
      $method = $this->methodMap[$id];
    }
    elseif (--$i && $id !== ($lcId = strtolower($id))) {
      $id = $lcId;
      continue;
    }
    elseif (method_exists($this, $method = 'get' . strtr($id, $this->underscoreMap) . 'Service')) {

      // $method is set to the right value, proceed
    }
    else {
      if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
        if (!$id) {
          throw new ServiceNotFoundException($id);
        }
        $alternatives = array();
        foreach ($this->services as $key => $associatedService) {
          $lev = levenshtein($id, $key);
          if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) {
            $alternatives[] = $key;
          }
        }
        throw new ServiceNotFoundException($id, null, null, $alternatives);
      }
      return;
    }
    $this->loading[$id] = true;
    try {
      $service = $this
        ->{$method}();
    } catch (\Exception $e) {
      unset($this->loading[$id]);
      if (array_key_exists($id, $this->services)) {
        unset($this->services[$id]);
      }
      if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
        return;
      }
      throw $e;
    }
    unset($this->loading[$id]);
    return $service;
  }
}