You are here

public function Container::enterScope in Service Container 7

Same name in this branch
  1. 7 lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::enterScope()
  2. 7 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Container.php \Symfony\Component\DependencyInjection\Container::enterScope()
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::enterScope()

This is called when you enter a scope.

@api

Parameters

string $name:

Throws

RuntimeException When the parent scope is inactive

InvalidArgumentException When the scope does not exist

File

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

Class

Container
Container is a dependency injection container.

Namespace

Symfony\Component\DependencyInjection

Code

public function enterScope($name) {
  if (!isset($this->scopes[$name])) {
    throw new InvalidArgumentException(sprintf('The scope "%s" does not exist.', $name));
  }
  if (self::SCOPE_CONTAINER !== $this->scopes[$name] && !isset($this->scopedServices[$this->scopes[$name]])) {
    throw new RuntimeException(sprintf('The parent scope "%s" must be active when entering this scope.', $this->scopes[$name]));
  }

  // check if a scope of this name is already active, if so we need to
  // remove all services of this scope, and those of any of its child
  // scopes from the global services map
  if (isset($this->scopedServices[$name])) {
    $services = array(
      $this->services,
      $name => $this->scopedServices[$name],
    );
    unset($this->scopedServices[$name]);
    foreach ($this->scopeChildren[$name] as $child) {
      if (isset($this->scopedServices[$child])) {
        $services[$child] = $this->scopedServices[$child];
        unset($this->scopedServices[$child]);
      }
    }

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

    // add stack entry for this scope so we can restore the removed services later
    if (!isset($this->scopeStacks[$name])) {
      $this->scopeStacks[$name] = new \SplStack();
    }
    $this->scopeStacks[$name]
      ->push($services);
  }
  $this->scopedServices[$name] = array();
}