protected function DrupalKernel::initializeContainer

Initializes the service container.

Overrides Kernel::initializeContainer

1 call to DrupalKernel::initializeContainer()
DrupalKernel::boot in drupal/core/lib/Drupal/Core/DrupalKernel.php
Overrides Kernel::boot().

File

drupal/core/lib/Drupal/Core/DrupalKernel.php, line 300
Definition of Drupal\Core\DrupalKernel.

Class

DrupalKernel
The DrupalKernel class is the core of Drupal itself.

Namespace

Drupal\Core

Code

protected function initializeContainer() {
  $persist = $this
    ->getServicesToPersist();

  // If we are rebuilding the kernel and we are in a request scope, store
  // request info so we can add them back after the rebuild.
  if (isset($this->container) && $this->container
    ->hasScope('request')) {
    $request = $this->container
      ->get('request');
  }
  $this->container = NULL;
  $class = $this
    ->getClassName();
  $cache_file = $class . '.php';
  if ($this->allowDumping) {

    // First, try to load.
    if (!class_exists($class, FALSE)) {
      $this
        ->storage()
        ->load($cache_file);
    }

    // If the load succeeded or the class already existed, use it.
    if (class_exists($class, FALSE)) {
      $fully_qualified_class_name = '\\' . $class;
      $this->container = new $fully_qualified_class_name();
      $this
        ->persistServices($persist);
    }
  }

  // First check whether the list of modules changed in this request.
  if (isset($this->newModuleList)) {
    if (isset($this->container) && isset($this->moduleList) && array_keys($this->moduleList) !== array_keys($this->newModuleList)) {
      unset($this->container);
    }
    $this->moduleList = $this->newModuleList;
    unset($this->newModuleList);
  }

  // Second, check if some other request -- for example on another web
  // frontend or during the installer -- changed the list of enabled modules.
  if (isset($this->container)) {

    // All namespaces must be registered before we attempt to use any service
    // from the container.
    $container_modules = $this->container
      ->getParameter('container.modules');
    $namespaces_before = $this->classLoader
      ->getPrefixes();
    $this
      ->registerNamespaces($this
      ->getModuleNamespaces($container_modules));

    // If 'container.modules' is wrong, the container must be rebuilt.
    if (!isset($this->moduleList)) {
      $this->moduleList = $this->container
        ->get('config.factory')
        ->get('system.module')
        ->load()
        ->get('enabled');
    }
    if (array_keys($this->moduleList) !== array_keys($container_modules)) {
      $persist = $this
        ->getServicesToPersist();
      unset($this->container);

      // Revert the class loader to its prior state. However,
      // registerNamespaces() performs a merge rather than replace, so to
      // effectively remove erroneous registrations, we must replace them with
      // empty arrays.
      $namespaces_after = $this->classLoader
        ->getPrefixes();
      $namespaces_before += array_fill_keys(array_diff(array_keys($namespaces_after), array_keys($namespaces_before)), array());
      $this
        ->registerNamespaces($namespaces_before);
    }
  }
  if (!isset($this->container)) {
    $this->container = $this
      ->buildContainer();
    $this
      ->persistServices($persist);

    // The namespaces are marked as persistent, so objects like the annotated
    // class discovery still has the right object. We may have updated the
    // list of modules, so set it.
    if ($this->container
      ->initialized('container.namespaces')) {
      $this->container
        ->get('container.namespaces')
        ->exchangeArray($this->container
        ->getParameter('container.namespaces'));
    }
    if ($this->allowDumping) {
      $this->containerNeedsDumping = TRUE;
    }
  }
  $this->container
    ->set('kernel', $this);

  // Set the class loader which was registered as a synthetic service.
  $this->container
    ->set('class_loader', $this->classLoader);

  // If we have a request set it back to the new container.
  if (isset($request)) {
    $this->container
      ->enterScope('request');
    $this->container
      ->set('request', $request);
  }
  \Drupal::setContainer($this->container);
}