You are here

protected function DrupalKernel::initializeContainer in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/DrupalKernel.php \Drupal\Core\DrupalKernel::initializeContainer()
  2. 10 core/lib/Drupal/Core/DrupalKernel.php \Drupal\Core\DrupalKernel::initializeContainer()

Initializes the service container.

Return value

\Symfony\Component\DependencyInjection\ContainerInterface

5 calls to DrupalKernel::initializeContainer()
DrupalKernel::boot in core/lib/Drupal/Core/DrupalKernel.php
Boots the current kernel.
DrupalKernel::rebuildContainer in core/lib/Drupal/Core/DrupalKernel.php
Force a container rebuild.
DrupalKernel::updateModules in core/lib/Drupal/Core/DrupalKernel.php
Implements Drupal\Core\DrupalKernelInterface::updateModules().
InstallerKernel::initializeContainer in core/lib/Drupal/Core/Installer/InstallerKernel.php
Initializes the service container.
UpdateKernel::initializeContainer in core/lib/Drupal/Core/Update/UpdateKernel.php
Initializes the service container.
2 methods override DrupalKernel::initializeContainer()
InstallerKernel::initializeContainer in core/lib/Drupal/Core/Installer/InstallerKernel.php
Initializes the service container.
UpdateKernel::initializeContainer in core/lib/Drupal/Core/Update/UpdateKernel.php
Initializes the service container.

File

core/lib/Drupal/Core/DrupalKernel.php, line 880

Class

DrupalKernel
The DrupalKernel class is the core of Drupal itself.

Namespace

Drupal\Core

Code

protected function initializeContainer() {
  $this->containerNeedsDumping = FALSE;
  $session_started = FALSE;
  if (isset($this->container)) {

    // Save the id of the currently logged in user.
    if ($this->container
      ->initialized('current_user')) {
      $current_user_id = $this->container
        ->get('current_user')
        ->id();
    }

    // If there is a session, close and save it.
    if ($this->container
      ->initialized('session')) {
      $session = $this->container
        ->get('session');
      if ($session
        ->isStarted()) {
        $session_started = TRUE;
        $session
          ->save();
      }
      unset($session);
    }
  }

  // If we haven't booted yet but there is a container, then we're asked to
  // boot the container injected via setContainer().
  // @see \Drupal\KernelTests\KernelTestBase::setUp()
  if (isset($this->container) && !$this->booted) {
    $container = $this->container;
  }

  // If the module list hasn't already been set in updateModules and we are
  // not forcing a rebuild, then try and load the container from the cache.
  if (empty($this->moduleList) && !$this->containerNeedsRebuild) {
    $container_definition = $this
      ->getCachedContainerDefinition();
  }

  // If there is no container and no cached container definition, build a new
  // one from scratch.
  if (!isset($container) && !isset($container_definition)) {
    $container = $this
      ->compileContainer();

    // Only dump the container if dumping is allowed. This is useful for
    // KernelTestBase, which never wants to use the real container, but always
    // the container builder.
    if ($this->allowDumping) {
      $dumper = new $this->phpArrayDumperClass($container);
      $container_definition = $dumper
        ->getArray();
    }
  }

  // The container was rebuilt successfully.
  $this->containerNeedsRebuild = FALSE;

  // Only create a new class if we have a container definition.
  if (isset($container_definition)) {
    $class = Settings::get('container_base_class', '\\Drupal\\Core\\DependencyInjection\\Container');
    $container = new $class($container_definition);
  }
  $this
    ->attachSynthetic($container);
  $this->container = $container;
  if ($session_started) {
    $this->container
      ->get('session')
      ->start();
  }

  // The request stack is preserved across container rebuilds. Reinject the
  // new session into the master request if one was present before.
  if ($request_stack = $this->container
    ->get('request_stack', ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
    if ($request = $request_stack
      ->getMasterRequest()) {
      $subrequest = TRUE;
      if ($request
        ->hasSession()) {
        $request
          ->setSession($this->container
          ->get('session'));
      }
    }
  }
  if (!empty($current_user_id)) {
    $this->container
      ->get('current_user')
      ->setInitialAccountId($current_user_id);
  }
  \Drupal::setContainer($this->container);

  // Allow other parts of the codebase to react on container initialization in
  // subrequest.
  if (!empty($subrequest)) {
    $this->container
      ->get('event_dispatcher')
      ->dispatch(self::CONTAINER_INITIALIZE_SUBREQUEST_FINISHED);
  }

  // If needs dumping flag was set, dump the container.
  if ($this->containerNeedsDumping && !$this
    ->cacheDrupalContainer($container_definition)) {
    $this->container
      ->get('logger.factory')
      ->get('DrupalKernel')
      ->error('Container cannot be saved to cache.');
  }
  return $this->container;
}