W3cubDocs

/Drupal 8

protected function DrupalKernel::initializeContainer

protected DrupalKernel::initializeContainer()

Initializes the service container.

Return value

\Symfony\Component\DependencyInjection\ContainerInterface

File

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

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()) {
      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);

  // 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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!DrupalKernel.php/function/DrupalKernel::initializeContainer/8.1.x