DrupalBoot8.php

  1. 8.0.x lib/Drush/Boot/DrupalBoot8.php
  2. 7.x lib/Drush/Boot/DrupalBoot8.php
  3. master lib/Drush/Boot/DrupalBoot8.php

Namespace

Drush\Boot

Classes

Namesort descending Description
DrupalBoot8

File

lib/Drush/Boot/DrupalBoot8.php
View source
  1. <?php
  2. namespace Drush\Boot;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Psr\Log\LoggerInterface;
  6. use Drupal\Core\DrupalKernel;
  7. use Drush\Drupal\DrupalKernel as DrushDrupalKernel;
  8. use Drush\Drupal\DrushServiceModfier;
  9. use Symfony\Component\DependencyInjection\Reference;
  10. use Drush\Log\LogLevel;
  11. class DrupalBoot8 extends DrupalBoot {
  12. /**
  13. * @var \Drupal\Core\DrupalKernelInterface
  14. */
  15. protected $kernel;
  16. /**
  17. * @var \Symfony\Component\HttpFoundation\Request
  18. */
  19. protected $request;
  20. function valid_root($path) {
  21. if (!empty($path) && is_dir($path) && file_exists($path . '/autoload.php')) {
  22. // Additional check for the presence of core/composer.json to
  23. // grant it is not a Drupal 7 site with a base folder named "core".
  24. $candidate = 'core/includes/common.inc';
  25. if (file_exists($path . '/' . $candidate) && file_exists($path . '/core/core.services.yml')) {
  26. if (file_exists($path . '/core/misc/drupal.js') || file_exists($path . '/core/assets/js/drupal.js')) {
  27. return $candidate;
  28. }
  29. }
  30. }
  31. }
  32. function get_version($drupal_root) {
  33. // Load the autoloader so we can access the class constants.
  34. drush_drupal_load_autoloader($drupal_root);
  35. // Drush depends on bootstrap being loaded at this point.
  36. require_once $drupal_root .'/core/includes/bootstrap.inc';
  37. if (defined('\Drupal::VERSION')) {
  38. return \Drupal::VERSION;
  39. }
  40. }
  41. function get_profile() {
  42. return drupal_get_profile();
  43. }
  44. function conf_path($require_settings = TRUE, $reset = FALSE, Request $request = NULL) {
  45. if (!isset($request)) {
  46. if (\Drupal::hasRequest()) {
  47. $request = \Drupal::request();
  48. }
  49. // @todo Remove once external CLI scripts (Drush) are updated.
  50. else {
  51. $request = Request::createFromGlobals();
  52. }
  53. }
  54. if (\Drupal::hasService('kernel')) {
  55. $site_path = \Drupal::service('kernel')->getSitePath();
  56. }
  57. if (!isset($site_path) || empty($site_path)) {
  58. $site_path = DrupalKernel::findSitePath($request, $require_settings);
  59. }
  60. return $site_path;
  61. }
  62. function add_logger() {
  63. // If we're running on Drupal 8 or later, we provide a logger which will send
  64. // output to drush_log(). This should catch every message logged through every
  65. // channel.
  66. $container = \Drupal::getContainer();
  67. $parser = $container->get('logger.log_message_parser');
  68. $drushLogger = \Drush::logger();
  69. $logger = new \Drush\Log\DrushLog($parser, $drushLogger);
  70. $container->get('logger.factory')->addLogger($logger);
  71. }
  72. function contrib_modules_paths() {
  73. return array(
  74. $this->conf_path() . '/modules',
  75. 'sites/all/modules',
  76. 'modules',
  77. );
  78. }
  79. /**
  80. * @return array of strings - paths to directories where contrib
  81. * themes can be found
  82. */
  83. function contrib_themes_paths() {
  84. return array(
  85. $this->conf_path() . '/themes',
  86. 'sites/all/themes',
  87. 'themes',
  88. );
  89. }
  90. function bootstrap_drupal_core($drupal_root) {
  91. $core = DRUPAL_ROOT . '/core';
  92. return $core;
  93. }
  94. function bootstrap_drupal_database_validate() {
  95. return parent::bootstrap_drupal_database_validate() && $this->bootstrap_drupal_database_has_table('key_value');
  96. }
  97. function bootstrap_drupal_database() {
  98. // D8 omits this bootstrap level as nothing special needs to be done.
  99. parent::bootstrap_drupal_database();
  100. }
  101. function bootstrap_drupal_configuration() {
  102. $this->request = Request::createFromGlobals();
  103. $classloader = drush_drupal_load_autoloader(DRUPAL_ROOT);
  104. // @todo - use Request::create() and then no need to set PHP superglobals
  105. $kernelClass = new \ReflectionClass('\Drupal\Core\DrupalKernel');
  106. if ($kernelClass->hasMethod('addServiceModifier')) {
  107. $this->kernel = DrupalKernel::createFromRequest($this->request, $classloader, 'prod', DRUPAL_ROOT);
  108. }
  109. else {
  110. $this->kernel = DrushDrupalKernel::createFromRequest($this->request, $classloader, 'prod', DRUPAL_ROOT);
  111. }
  112. // @see Drush\Drupal\DrupalKernel::addServiceModifier()
  113. $this->kernel->addServiceModifier(new DrushServiceModfier());
  114. // Unset drupal error handler and restore Drush's one.
  115. restore_error_handler();
  116. // Disable automated cron if the module is enabled.
  117. $GLOBALS['config']['automated_cron.settings']['interval'] = 0;
  118. parent::bootstrap_drupal_configuration();
  119. }
  120. function bootstrap_drupal_full() {
  121. drush_log(dt('About to bootstrap the Drupal 8 Kernel.'), LogLevel::DEBUG);
  122. // TODO: do we need to do ob_start any longer?
  123. if (!drush_get_context('DRUSH_QUIET', FALSE)) {
  124. ob_start();
  125. }
  126. $this->kernel->invalidateContainer();
  127. $this->kernel->boot();
  128. $this->kernel->prepareLegacyRequest($this->request);
  129. if (!drush_get_context('DRUSH_QUIET', FALSE)) {
  130. ob_end_clean();
  131. }
  132. drush_log(dt('Finished bootstraping the Drupal 8 Kernel.'), LogLevel::DEBUG);
  133. parent::bootstrap_drupal_full();
  134. // Get a list of the modules to ignore
  135. $ignored_modules = drush_get_option_list('ignored-modules', array());
  136. // We have to get the service command list from the container, because
  137. // it is constructed in an indirect way during the container initialization.
  138. // The upshot is that the list of console commands is not available
  139. // until after $kernel->boot() is called.
  140. $container = \Drupal::getContainer();
  141. $serviceCommandlist = $container->get('drush.service.consolecommands');
  142. foreach ($serviceCommandlist->getCommandList() as $command) {
  143. if (!$this->commandIgnored($command, $ignored_modules)) {
  144. drush_log(dt('Add a command: !name', ['!name' => $command->getName()]), LogLevel::DEBUG);
  145. annotationcommand_adapter_cache_module_console_commands($command);
  146. }
  147. }
  148. // Do the same thing with the annotation commands.
  149. $serviceCommandlist = $container->get('drush.service.consolidationcommands');
  150. foreach ($serviceCommandlist->getCommandList() as $commandhandler) {
  151. if (!$this->commandIgnored($commandhandler, $ignored_modules)) {
  152. drush_log(dt('Add a commandhandler: !name', ['!name' => get_class($commandhandler)]), LogLevel::DEBUG);
  153. annotationcommand_adapter_cache_module_service_commands($commandhandler);
  154. }
  155. }
  156. }
  157. public function commandIgnored($command, $ignored_modules) {
  158. if (empty($ignored_modules)) {
  159. return false;
  160. }
  161. $ignored_regex = '#\\\\(' . implode('|', $ignored_modules) . ')\\\\#';
  162. $class = new \ReflectionClass($command);
  163. $commandNamespace = $class->getNamespaceName();
  164. return preg_match($ignored_regex, $commandNamespace);
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function terminate() {
  170. parent::terminate();
  171. if ($this->kernel) {
  172. $response = Response::create('');
  173. $this->kernel->terminate($this->request, $response);
  174. }
  175. }
  176. }