BaseBoot.php

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

Namespace

Drush\Boot

Classes

Namesort descending Description
BaseBoot

File

lib/Drush/Boot/BaseBoot.php
View source
  1. <?php
  2. namespace Drush\Boot;
  3. use Drush\Log\LogLevel;
  4. use Psr\Log\LoggerInterface;
  5. use Psr\Log\LoggerAwareInterface;
  6. use Psr\Log\LoggerAwareTrait;
  7. use Symfony\Component\Console\Input\ArgvInput;
  8. abstract class BaseBoot implements Boot, LoggerAwareInterface {
  9. use LoggerAwareTrait;
  10. function __construct() {
  11. }
  12. function valid_root($path) {
  13. }
  14. function get_version($root) {
  15. }
  16. function command_defaults() {
  17. }
  18. function enforce_requirement(&$command) {
  19. drush_enforce_requirement_bootstrap_phase($command);
  20. drush_enforce_requirement_core($command);
  21. drush_enforce_requirement_drush_dependencies($command);
  22. }
  23. function report_command_error($command) {
  24. // Set errors related to this command.
  25. $args = implode(' ', drush_get_arguments());
  26. if (isset($command) && is_array($command)) {
  27. foreach ($command['bootstrap_errors'] as $key => $error) {
  28. drush_set_error($key, $error);
  29. }
  30. drush_set_error('DRUSH_COMMAND_NOT_EXECUTABLE', dt("The drush command '!args' could not be executed.", array('!args' => $args)));
  31. }
  32. elseif (!empty($args)) {
  33. drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt("The drush command '!args' could not be found. Run `drush cache-clear drush` to clear the commandfile cache if you have installed new extensions.", array('!args' => $args)));
  34. }
  35. // Set errors that occurred in the bootstrap phases.
  36. $errors = drush_get_context('DRUSH_BOOTSTRAP_ERRORS', array());
  37. foreach ($errors as $code => $message) {
  38. drush_set_error($code, $message);
  39. }
  40. }
  41. function bootstrap_and_dispatch() {
  42. $phases = $this->bootstrap_init_phases();
  43. $return = '';
  44. $command_found = FALSE;
  45. _drush_bootstrap_output_prepare();
  46. foreach ($phases as $phase) {
  47. if (drush_bootstrap_to_phase($phase)) {
  48. $command = drush_parse_command();
  49. if (is_array($command)) {
  50. $command += $this->command_defaults();
  51. // Insure that we have bootstrapped to a high enough
  52. // phase for the command prior to enforcing requirements.
  53. $bootstrap_result = drush_bootstrap_to_phase($command['bootstrap']);
  54. $this->enforce_requirement($command);
  55. if ($bootstrap_result && empty($command['bootstrap_errors'])) {
  56. $this->logger->log(LogLevel::BOOTSTRAP, dt("Found command: !command (commandfile=!commandfile)", array('!command' => $command['command'], '!commandfile' => $command['commandfile'])));
  57. $command_found = TRUE;
  58. // Dispatch the command(s).
  59. $return = drush_dispatch($command);
  60. if (drush_get_context('DRUSH_DEBUG') && !drush_get_context('DRUSH_QUIET')) {
  61. // @todo Create version independant wrapper around Drupal timers. Use it.
  62. drush_print_timers();
  63. }
  64. break;
  65. }
  66. }
  67. }
  68. else {
  69. break;
  70. }
  71. }
  72. // TODO: If we could not find a legacy Drush command, try running a
  73. // command via the Symfony application. See also drush_main() in preflight.inc;
  74. // ultimately, the Symfony application should be called from there.
  75. if (!$command_found && isset($command)) {
  76. $container = \Drush::getContainer();
  77. $application = $container->get('application');
  78. $args = drush_get_arguments();
  79. if (count($args)) {
  80. $name = $args[0];
  81. if ($this->hasRegisteredSymfonyCommand($application, $name)) {
  82. $command_found = true;
  83. $input = drush_symfony_input();
  84. $this->logger->log(LogLevel::BOOTSTRAP, dt("Dispatching with Symfony application as a fallback, since no native Drush command was found. (Set DRUSH_SYMFONY environment variable to skip Drush dispatch.)"));
  85. $application->run($input);
  86. }
  87. }
  88. }
  89. if (!$command_found) {
  90. // If we reach this point, command doesn't fit requirements or we have not
  91. // found either a valid or matching command.
  92. $this->report_command_error($command);
  93. }
  94. // Prevent a '1' at the end of the output.
  95. if ($return === TRUE) {
  96. $return = '';
  97. }
  98. return $return;
  99. }
  100. protected function hasRegisteredSymfonyCommand($application, $name) {
  101. try {
  102. $application->find($name);
  103. return true;
  104. }
  105. catch (\InvalidArgumentException $e) {
  106. return false;
  107. }
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function terminate() {
  113. }
  114. }