function DrupalKernel::resetContainer

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

Overrides DrupalKernelInterface::resetContainer

File

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

Class

DrupalKernel
The DrupalKernel class is the core of Drupal itself.

Namespace

Drupal\Core

Code

public function resetContainer() : ContainerInterface {
    $session_started = FALSE;
    $subrequest = FALSE;
    $reload_module_handler = FALSE;
    // 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 ($this->container
        ->initialized('module_handler') && $this->container
        ->get('module_handler')
        ->isLoaded()) {
        $reload_module_handler = TRUE;
    }
    // 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();
    $persist = $this->getServicesToPersist($this->container);
    $this->container
        ->reset();
    $this->persistServices($this->container, $persist);
    $this->container
        ->set('kernel', $this);
    // Set the class loader which was registered as a synthetic service.
    $this->container
        ->set('class_loader', $this->classLoader);
    if ($reload_module_handler) {
        $this->container
            ->get('module_handler')
            ->reload();
    }
    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);
        }
    }
    // Allow other parts of the codebase to react on container reset in
    // subrequest.
    if (!empty($subrequest)) {
        $this->container
            ->get('event_dispatcher')
            ->dispatch(new Event(), self::CONTAINER_INITIALIZE_SUBREQUEST_FINISHED);
    }
    return $this->container;
}

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