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.

Source
  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(realpath("$dir/.."));
  20. }
  21. $autoloader = _find_autoloader(empty($_SERVER['PWD']) ? getcwd() : $_SERVER['PWD']);
  22. if (!$autoloader || !class_exists('\Drupal\Core\DrupalKernel')) {
  23. throw new \Exception("This script must be invoked inside a Drupal 8 environment. Unable to continue.");
  24. }
  25. // Create a DrupalKernel instance.
  26. DrupalKernel::bootEnvironment();
  27. $kernel = new DrupalKernel('prod', $autoloader);
  28. // Need to change the current working directory to the actual root path.
  29. // This is needed in case the script is initiated inside a sub-directory.
  30. chdir($kernel->getAppRoot());
  31. // Initialize settings, this requires reflection since its a protected method.
  32. $request = Request::createFromGlobals();
  33. $initializeSettings = new \ReflectionMethod($kernel, 'initializeSettings');
  34. $initializeSettings->setAccessible(TRUE);
  35. $initializeSettings->invokeArgs($kernel, [$request]);
  36. // Boot the kernel.
  37. $kernel->boot();
  38. $kernel->preHandle($request);
  39. // Due to a core bug, the theme handler has to be invoked to register theme
  40. // namespaces with the autoloader.
  41. // @todo Remove once installed_extensions makes its way into core.
  42. // @see https://www.drupal.org/project/drupal/issues/2941757
  43. $container = $kernel->getContainer();
  44. if (!$container->has('installed_extensions')) {
  45. $container->get('theme_handler')->listInfo();
  46. }
  47. return $kernel;

Functions

Name Description
_find_autoloader