function DrupalApplication::bootstrap

Same name and namespace in other branches
  1. main core/lib/Drupal/Core/Command/DrupalApplication.php \Drupal\Core\Command\DrupalApplication::bootstrap()

Bootstraps Drupal in a way that is appropriate for console commands.

1 call to DrupalApplication::bootstrap()
DrupalApplication::find in core/lib/Drupal/Core/Command/DrupalApplication.php

File

core/lib/Drupal/Core/Command/DrupalApplication.php, line 88

Class

DrupalApplication
Customize the Symfony Application for Drupal.

Namespace

Drupal\Core\Command

Code

protected function bootstrap() : bool {
  if ($this->booted) {
    return TRUE;
  }
  try {
    $request = Request::create($this->getUri());
    // Discovery can get out of whack if cleared caches and try to run this
    // command without a web request priming discovery.
    chdir(\DRUPAL_ROOT);
    // We need to not load a cached copy of the container from disk. For
    // example, inside Kernel tests, we need to fully build the container so
    // we discover and register commands, instead of reusing the container
    // from the Kernel test itself. Therefore, we pass `FALSE` for the
    // `$allow_dumping` parameter here.
    $kernel = new DrupalKernel('prod', $this->classloader, FALSE);
    // We tried calling `DrupalKernel::bootEnvironment()` right here to setup
    // some common environment and PHP initialization steps. However, that
    // method also calls `set_error_handler('_drupal_error_handler')` which
    // creates problems when running commands inside of tests. Since
    // `bootEnvironment()` has a static flag to only happen once, it's hard to
    // restore the previous error handler after running commands, and we don't
    // want to make tests responsible for restoring the error handler.
    // @todo Refactor `bootEnvironment()' so that we can use the parts we need
    // but leave the error handler alone.
    // @see https://www.drupal.org/node/2690035
    // Define the DRUPAL_TEST_IN_CHILD_SITE based on if we're inside a test.
    DrupalKernel::setupDrupalTestInChildSite($kernel->getAppRoot());
    $sitePath = DrupalKernel::findSitePath($request, TRUE);
    $kernel->setSitePath($sitePath);
    Settings::initialize($kernel->getAppRoot(), $kernel->getSitePath(), $this->classloader);
    $kernel->boot();
    $container = $kernel->getContainer();
    $container->get('request_stack')
      ->push($request);
    // Set base URL - needed when in a subdir e.g. http://example.com/subdir.
    $container->get('router.request_context')
      ->setBaseUrl($request->getPathInfo());
    // This sets things up, esp loadLegacyIncludes().
    $kernel->preHandle($request);
    // Adds commands as part of installed modules.
    $this->addDrupalCommands($container);
    // Dispatch standard console events.
    $this->setDispatcher($container->get('event_dispatcher'));
  } catch (\Throwable) {
    return FALSE;
  }
  $this->booted = TRUE;
  return TRUE;
}

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