drush.php

  1. 8.0.x drush.php
  2. 6.x drush.php
  3. 7.x drush.php
  4. 3.x drush.php
  5. 4.x drush.php
  6. 5.x drush.php
  7. master lib/Drush.php
  8. master drush.php

drush is a PHP script implementing a command line shell for Drupal.

@requires PHP CLI 5.2.0, or newer.

Functions

Namesort descending Description
drush_command_belongs_to_disabled_module Check if the given command belongs to a disabled module
drush_main The main Drush function.
_drush_bootstrap_and_dispatch

File

drush.php
View source
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * @file
  5. * drush is a PHP script implementing a command line shell for Drupal.
  6. *
  7. * @requires PHP CLI 5.2.0, or newer.
  8. */
  9. require(dirname(__FILE__) . '/includes/bootstrap.inc');
  10. if (drush_bootstrap_prepare() === FALSE) {
  11. exit(1);
  12. }
  13. exit(drush_main());
  14. /**
  15. * The main Drush function.
  16. *
  17. * - Runs "early" option code, if set (see global options).
  18. * - Parses the command line arguments, configuration files and environment.
  19. * - Prepares and executes a Drupal bootstrap, if possible,
  20. * - Dispatches the given command.
  21. *
  22. * function_exists('drush_main') may be used by modules to detect whether
  23. * they are being called from drush. See http://drupal.org/node/1181308
  24. * and http://drupal.org/node/827478
  25. *
  26. * @return
  27. * Whatever the given command returns.
  28. */
  29. function drush_main() {
  30. $return = '';
  31. if ($file = drush_get_option('early', FALSE)) {
  32. require_once($file);
  33. $function = 'drush_early_' . basename($file, '.inc');
  34. if (function_exists($function)) {
  35. if ($return = $function()) {
  36. // If the function returns FALSE, we continue and attempt to bootstrap
  37. // as normal. Otherwise, we exit early with the returned output.
  38. if ($return === TRUE) {
  39. $return = '';
  40. }
  41. drush_bootstrap_finish();
  42. return $return;
  43. }
  44. }
  45. }
  46. // Process initial global options such as --debug.
  47. _drush_bootstrap_global_options();
  48. $return = '';
  49. drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUSH);
  50. if (!drush_get_error()) {
  51. // Do any necessary preprocessing operations on the command,
  52. // perhaps handling immediately.
  53. $command_handled = drush_preflight_command_dispatch();
  54. if (!$command_handled) {
  55. $return = _drush_bootstrap_and_dispatch();
  56. }
  57. }
  58. drush_bootstrap_finish();
  59. // After this point the drush_shutdown function will run,
  60. // exiting with the correct exit code.
  61. return $return;
  62. }
  63. function _drush_bootstrap_and_dispatch() {
  64. $phases = _drush_bootstrap_phases(FALSE, TRUE);
  65. $return = '';
  66. $command_found = FALSE;
  67. _drush_bootstrap_output_prepare();
  68. foreach ($phases as $phase) {
  69. if (drush_bootstrap_to_phase($phase)) {
  70. $command = drush_parse_command();
  71. if (is_array($command)) {
  72. $bootstrap_result = drush_bootstrap_to_phase($command['bootstrap']);
  73. drush_enforce_requirement_bootstrap_phase($command);
  74. drush_enforce_requirement_core($command);
  75. drush_enforce_requirement_drupal_dependencies($command);
  76. drush_enforce_requirement_drush_dependencies($command);
  77. if ($bootstrap_result && empty($command['bootstrap_errors'])) {
  78. drush_log(dt("Found command: !command (commandfile=!commandfile)", array('!command' => $command['command'], '!commandfile' => $command['commandfile'])), 'bootstrap');
  79. $command_found = TRUE;
  80. // Dispatch the command(s).
  81. $return = drush_dispatch($command);
  82. // prevent a '1' at the end of the output
  83. if ($return === TRUE) {
  84. $return = '';
  85. }
  86. if (drush_get_context('DRUSH_DEBUG') && !drush_get_context('DRUSH_QUIET')) {
  87. drush_print_timers();
  88. }
  89. drush_log(dt('Peak memory usage was !peak', array('!peak' => drush_format_size(memory_get_peak_usage()))), 'memory');
  90. break;
  91. }
  92. }
  93. }
  94. else {
  95. break;
  96. }
  97. }
  98. if (!$command_found) {
  99. // If we reach this point, command doesn't fit requirements or we have not
  100. // found either a valid or matching command.
  101. // If no command was found check if it belongs to a disabled module.
  102. if (!$command) {
  103. $command = drush_command_belongs_to_disabled_module();
  104. }
  105. // Set errors related to this command.
  106. $args = implode(' ', drush_get_arguments());
  107. if (isset($command) && is_array($command)) {
  108. foreach ($command['bootstrap_errors'] as $key => $error) {
  109. drush_set_error($key, $error);
  110. }
  111. drush_set_error('DRUSH_COMMAND_NOT_EXECUTABLE', dt("The drush command '!args' could not be executed.", array('!args' => $args)));
  112. }
  113. elseif (!empty($args)) {
  114. 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)));
  115. }
  116. // Set errors that occurred in the bootstrap phases.
  117. $errors = drush_get_context('DRUSH_BOOTSTRAP_ERRORS', array());
  118. foreach ($errors as $code => $message) {
  119. drush_set_error($code, $message);
  120. }
  121. }
  122. return $return;
  123. }
  124. /**
  125. * Check if the given command belongs to a disabled module
  126. *
  127. * @return
  128. * Array with a command-like bootstrap error or FALSE if Drupal was not
  129. * bootstrapped fully or the command does not belong to a diabled module.
  130. */
  131. function drush_command_belongs_to_disabled_module() {
  132. if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  133. _drush_find_commandfiles(DRUSH_BOOTSTRAP_DRUPAL_SITE, DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION);
  134. $commands = drush_get_commands();
  135. $arguments = drush_get_arguments();
  136. $command_name = array_shift($arguments);
  137. if (isset($commands[$command_name])) {
  138. // We found it. Load its module name and set an error.
  139. if (is_array($commands[$command_name]['drupal dependencies']) && count($commands[$command_name]['drupal dependencies'])) {
  140. $modules = implode(', ', $commands[$command_name]['drupal dependencies']);
  141. } else {
  142. // The command does not define Drupal dependencies. Derive them.
  143. $command_files = drush_get_context('DRUSH_COMMAND_FILES', array());
  144. $command_path = $commands[$command_name]['path'] . DIRECTORY_SEPARATOR . $commands[$command_name]['commandfile'] . '.drush.inc';
  145. $modules = array_search($command_path, $command_files);
  146. }
  147. return array(
  148. 'bootstrap_errors' => array(
  149. 'DRUSH_COMMAND_DEPENDENCY_ERROR' =>
  150. dt('Command !command needs the following module(s) enabled to run: !dependencies.', array('!command' => $command_name, '!dependencies' => $modules)),
  151. )
  152. );
  153. }
  154. }
  155. return FALSE;
  156. }