function DrupalKernel::initializeContainer

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/DrupalKernel.php \Drupal\Core\DrupalKernel::initializeContainer()
  2. 8.9.x core/lib/Drupal/Core/DrupalKernel.php \Drupal\Core\DrupalKernel::initializeContainer()
  3. 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 861

Class

DrupalKernel
The DrupalKernel class is the core of Drupal itself.

Namespace

Drupal\Core

Code

protected function initializeContainer() {
    $this->containerNeedsDumping = FALSE;
    $session_started = FALSE;
    $all_messages = [];
    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();
        }
        // After rebuilding the container some objects will have stale services.
        // Record a map of objects to service IDs prior to rebuilding the
        // container in order to ensure
        // \Drupal\Core\DependencyInjection\DependencySerializationTrait works as
        // expected.
        $this->container
            ->get(ReverseContainer::class)
            ->recordContainer();
        // 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);
        }
        $all_messages = $this->container
            ->get('messenger')
            ->all();
    }
    // 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 cached container definition, build a new container from
    // scratch.
    if (!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)) {
        // Drupal provides two dynamic parameters to access specific paths that
        // are determined from the request.
        $container_definition['parameters']['app.root'] = $this->getAppRoot();
        $container_definition['parameters']['site.path'] = $this->getSitePath();
        $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. Re-inject the
    // new session into the main request if one was present before.
    if ($request_stack = $this->container
        ->get('request_stack', ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
        if ($request = $request_stack->getMainRequest()) {
            $subrequest = TRUE;
            $request->setSession($this->container
                ->get('session'));
        }
    }
    if (!empty($current_user_id)) {
        $this->container
            ->get('current_user')
            ->setInitialAccountId($current_user_id);
    }
    // Re-add messages.
    foreach ($all_messages as $type => $messages) {
        foreach ($messages as $message) {
            $this->container
                ->get('messenger')
                ->addMessage($message, $type);
        }
    }
    \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(new Event(), 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.