You are here

public function Container::leaveScope in Service Container 7

Same name in this branch
  1. 7 lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::leaveScope()
  2. 7 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Container.php \Symfony\Component\DependencyInjection\Container::leaveScope()
Same name and namespace in other branches
  1. 7.2 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Container.php \Symfony\Component\DependencyInjection\Container::leaveScope()

This is called to leave the current scope, and move back to the parent scope.

@api

Parameters

string $name The name of the scope to leave:

Throws

InvalidArgumentException if the scope is not active

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Container.php, line 449

Class

Container
Container is a dependency injection container.

Namespace

Symfony\Component\DependencyInjection

Code

public function leaveScope($name) {
  if (!isset($this->scopedServices[$name])) {
    throw new InvalidArgumentException(sprintf('The scope "%s" is not active.', $name));
  }

  // remove all services of this scope, or any of its child scopes from
  // the global service map
  $services = array(
    $this->services,
    $this->scopedServices[$name],
  );
  unset($this->scopedServices[$name]);
  foreach ($this->scopeChildren[$name] as $child) {
    if (isset($this->scopedServices[$child])) {
      $services[] = $this->scopedServices[$child];
      unset($this->scopedServices[$child]);
    }
  }

  // update global map
  $this->services = call_user_func_array('array_diff_key', $services);

  // check if we need to restore services of a previous scope of this type
  if (isset($this->scopeStacks[$name]) && count($this->scopeStacks[$name]) > 0) {
    $services = $this->scopeStacks[$name]
      ->pop();
    $this->scopedServices += $services;
    if ($this->scopeStacks[$name]
      ->isEmpty()) {
      unset($this->scopeStacks[$name]);
    }
    foreach ($services as $array) {
      foreach ($array as $id => $service) {
        $this
          ->set($id, $service, $name);
      }
    }
  }
}