file bootstrap.php

  1. 8.x-3.x src/Bootstrap.php
  2. 8.x-3.x scripts/bootstrap.php

Locates the Drupal root directory and bootstraps the kernel.

  1. <?php
  2. /**
  3. * @file
  4. * Locates the Drupal root directory and bootstraps the kernel.
  5. */
  6. use Drupal\Core\DrupalKernel;
  7. use Symfony\Component\HttpFoundation\Request;
  8. // Immediately return if classes are discoverable (already booted).
  9. if (class_exists('\Drupal\Core\DrupalKernel') && class_exists('\Drupal')) {
  10. return \Drupal::service('kernel');
  11. }
  12. function _find_autoloader($dir) {
  13. if (file_exists($autoloadFile = $dir . '/autoload.php') || file_exists($autoloadFile = $dir . '/vendor/autoload.php')) {
  14. return include_once($autoloadFile);
  15. }
  16. else if (empty($dir) || $dir === DIRECTORY_SEPARATOR) {
  17. return FALSE;
  18. }
  19. return _find_autoloader(dirname($dir));
  20. }
  21. $autoloader = _find_autoloader(empty($_SERVER['PWD']) ? getcwd() : $_SERVER['PWD']);
  22. if (!$autoloader || !class_exists('\Drupal\Core\DrupalKernel')) {
  23. print "This script must be invoked inside a Drupal 8 environment. Unable to continue.\n";
  24. exit();
  25. }
  26. // Create a DrupalKernel instance.
  27. DrupalKernel::bootEnvironment();
  28. $kernel = new DrupalKernel('prod', $autoloader);
  29. // Need to change the current working directory to the actual root path.
  30. // This is needed in case the script is initiated inside a sub-directory.
  31. chdir($kernel->getAppRoot());
  32. // Initialize settings, this requires reflection since its a protected method.
  33. $request = Request::createFromGlobals();
  34. $initializeSettings = new \ReflectionMethod($kernel, 'initializeSettings');
  35. $initializeSettings->setAccessible(TRUE);
  36. $initializeSettings->invokeArgs($kernel, [$request]);
  37. // Boot the kernel.
  38. $kernel->boot();
  39. $kernel->preHandle($request);
  40. // Due to a core bug, the theme handler has to be invoked to register theme
  41. // namespaces with the autoloader.
  42. // @todo Remove once installed_extensions makes its way into core.
  43. // @see https://www.drupal.org/project/drupal/issues/2941757
  44. $container = $kernel->getContainer();
  45. if (!$container->has('installed_extensions')) {
  46. $container->get('theme_handler')->listInfo();
  47. }
  48. return $kernel;

Functions

Name Description
_find_autoloader