Example #1
0
/**
 * The main Drush function.
 *
 * - Parses the command line arguments, configuration files and environment.
 * - Prepares and executes a Drupal bootstrap, if possible,
 * - Dispatches the given command.
 *
 * @return
 *   Whatever the given command returns.
 */
function drush_main()
{
    $phases = _drush_bootstrap_phases();
    $completed_phases = array();
    $return = '';
    $command_found = FALSE;
    foreach ($phases as $phase) {
        if (drush_bootstrap($phase)) {
            $completed_phases[$phase] = TRUE;
            $command = drush_parse_command();
            // Process a remote command if 'remote-host' option is set.
            if (drush_remote_command()) {
                $command_found = TRUE;
                break;
            }
            if (is_array($command)) {
                if (array_key_exists($command['bootstrap'], $completed_phases) && empty($command['bootstrap_errors'])) {
                    drush_log(dt("Found command: !command (commandfile=!commandfile)", array('!command' => $command['command'], '!commandfile' => $command['commandfile'])), 'bootstrap');
                    $command_found = TRUE;
                    // Dispatch the command(s).
                    $return = drush_dispatch($command);
                    if (drush_get_context('DRUSH_DEBUG')) {
                        drush_print_timers();
                    }
                    drush_log(dt('Peak memory usage was !peak', array('!peak' => drush_format_size(memory_get_peak_usage()))), 'memory');
                    break;
                }
            }
        } else {
            break;
        }
    }
    if (!$command_found) {
        // If we reach this point, we have not found either a valid or matching command.
        $args = implode(' ', drush_get_arguments());
        if (isset($command) && is_array($command)) {
            foreach ($command['bootstrap_errors'] as $key => $error) {
                drush_set_error($key, $error);
            }
            drush_set_error('DRUSH_COMMAND_NOT_EXECUTABLE', dt("The drush command '!args' could not be executed.", array('!args' => $args)));
        } elseif (!empty($args)) {
            drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt("The drush command '!args' could not be found.", array('!args' => $args)));
        } else {
            // This can occur if we get an error during _drush_bootstrap_drush_validate();
            drush_set_error('DRUSH_COULD_NOT_EXECUTE', dt("Drush could not execute."));
        }
    }
    // We set this context to let the shutdown function know we reached the end of drush_main();
    drush_set_context("DRUSH_EXECUTION_COMPLETED", TRUE);
    // After this point the drush_shutdown function will run,
    // exiting with the correct exit code.
    return $return;
}
Example #2
0
function _drush_bootstrap_and_dispatch()
{
    $phases = _drush_bootstrap_phases(FALSE, TRUE);
    $return = '';
    $command_found = FALSE;
    _drush_bootstrap_output_prepare();
    foreach ($phases as $phase) {
        if (drush_bootstrap_to_phase($phase)) {
            $command = drush_parse_command();
            if (is_array($command)) {
                $bootstrap_result = drush_bootstrap_to_phase($command['bootstrap']);
                drush_enforce_requirement_bootstrap_phase($command);
                drush_enforce_requirement_core($command);
                drush_enforce_requirement_drupal_dependencies($command);
                drush_enforce_requirement_drush_dependencies($command);
                if ($bootstrap_result && empty($command['bootstrap_errors'])) {
                    drush_log(dt("Found command: !command (commandfile=!commandfile)", array('!command' => $command['command'], '!commandfile' => $command['commandfile'])), 'bootstrap');
                    $command_found = TRUE;
                    // Dispatch the command(s).
                    $return = drush_dispatch($command);
                    // prevent a '1' at the end of the output
                    if ($return === TRUE) {
                        $return = '';
                    }
                    if (drush_get_context('DRUSH_DEBUG') && !drush_get_context('DRUSH_QUIET')) {
                        drush_print_timers();
                    }
                    drush_log(dt('Peak memory usage was !peak', array('!peak' => drush_format_size(memory_get_peak_usage()))), 'memory');
                    break;
                }
            }
        } else {
            break;
        }
    }
    if (!$command_found) {
        // If we reach this point, command doesn't fit requirements or we have not
        // found either a valid or matching command.
        // If no command was found check if it belongs to a disabled module.
        if (!$command) {
            $command = drush_command_belongs_to_disabled_module();
        }
        // Set errors related to this command.
        $args = implode(' ', drush_get_arguments());
        if (isset($command) && is_array($command)) {
            foreach ($command['bootstrap_errors'] as $key => $error) {
                drush_set_error($key, $error);
            }
            drush_set_error('DRUSH_COMMAND_NOT_EXECUTABLE', dt("The drush command '!args' could not be executed.", array('!args' => $args)));
        } elseif (!empty($args)) {
            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)));
        }
        // Set errors that occurred in the bootstrap phases.
        $errors = drush_get_context('DRUSH_BOOTSTRAP_ERRORS', array());
        foreach ($errors as $code => $message) {
            drush_set_error($code, $message);
        }
    }
    return $return;
}
Example #3
0
 public function log($level, $message, array $context = array())
 {
     // Convert to old $entry array for b/c calls
     $entry = $context;
     $entry['type'] = $level;
     $entry['message'] = $message;
     // Drush\Log\Logger should take over all of the responsibilities
     // of drush_log, including caching the log messages and sending
     // log messages along to backend invoke.
     // TODO: move these implementations inside this class.
     $log =& drush_get_context('DRUSH_LOG', array());
     $log[] = $entry;
     drush_backend_packet('log', $entry);
     if (drush_get_context('DRUSH_NOCOLOR')) {
         $red = "[%s]";
         $yellow = "[%s]";
         $green = "[%s]";
     } else {
         $red = "[%s]";
         $yellow = "[%s]";
         $green = "[%s]";
     }
     $verbose = drush_get_context('DRUSH_VERBOSE');
     $debug = drush_get_context('DRUSH_DEBUG');
     switch ($level) {
         case LogLevel::WARNING:
         case 'cancel':
             $type_msg = sprintf($yellow, $level);
             break;
         case 'failed':
             // Obsolete; only here in case contrib is using it.
         // Obsolete; only here in case contrib is using it.
         case 'error':
             $type_msg = sprintf($red, $level);
             break;
         case 'ok':
         case 'completed':
             // Obsolete; only here in case contrib is using it.
         // Obsolete; only here in case contrib is using it.
         case 'success':
         case 'status':
             // Obsolete; only here in case contrib is using it.
             // In quiet mode, suppress progress messages
             if (drush_get_context('DRUSH_QUIET')) {
                 return TRUE;
             }
             $type_msg = sprintf($green, $level);
             break;
         case 'notice':
         case 'message':
             // Obsolete; only here in case contrib is using it.
         // Obsolete; only here in case contrib is using it.
         case 'info':
             if (!$verbose) {
                 // print nothing. exit cleanly.
                 return TRUE;
             }
             $type_msg = sprintf("[%s]", $level);
             break;
         default:
             if (!$debug) {
                 // print nothing. exit cleanly.
                 return TRUE;
             }
             $type_msg = sprintf("[%s]", $level);
             break;
     }
     // When running in backend mode, log messages are not displayed, as they will
     // be returned in the JSON encoded associative array.
     if (drush_get_context('DRUSH_BACKEND')) {
         return;
     }
     $columns = drush_get_context('DRUSH_COLUMNS', 80);
     $width[1] = 11;
     // Append timer and memory values.
     if ($debug) {
         $timer = sprintf('[%s sec, %s]', round($entry['timestamp'] - DRUSH_REQUEST_TIME, 2), drush_format_size($entry['memory']));
         $entry['message'] = $entry['message'] . ' ' . $timer;
     }
     $width[0] = $columns - 11;
     $format = sprintf("%%-%ds%%%ds", $width[0], $width[1]);
     // Place the status message right aligned with the top line of the error message.
     $message = wordwrap($entry['message'], $width[0]);
     $lines = explode("\n", $message);
     $lines[0] = sprintf($format, $lines[0], $type_msg);
     $message = implode("\n", $lines);
     drush_print($message, 0, STDERR);
 }
Example #4
0
/**
 * The main Drush function.
 *
 * - Parses the command line arguments, configuration files and environment.
 * - Prepares and executes a Drupal bootstrap, if possible,
 * - Dispatches the given command.
 *
 * @return
 *   Whatever the given command returns.
 */
function drush_main()
{
    $phases = _drush_bootstrap_phases(FALSE, TRUE);
    drush_set_context('DRUSH_BOOTSTRAP_PHASE', DRUSH_BOOTSTRAP_NONE);
    // We need some global options processed at this early stage. Namely --debug.
    drush_parse_args();
    _drush_bootstrap_global_options();
    $return = '';
    $command_found = FALSE;
    foreach ($phases as $phase) {
        if (drush_bootstrap_to_phase($phase)) {
            $command = drush_parse_command();
            // Process a remote command if 'remote-host' option is set.
            if (drush_remote_command()) {
                $command_found = TRUE;
                break;
            }
            if (is_array($command)) {
                $bootstrap_result = drush_bootstrap_to_phase($command['bootstrap']);
                drush_enforce_requirement_bootstrap_phase($command);
                drush_enforce_requirement_core($command);
                drush_enforce_requirement_drupal_dependencies($command);
                drush_enforce_requirement_drush_dependencies($command);
                if ($bootstrap_result && empty($command['bootstrap_errors'])) {
                    drush_log(dt("Found command: !command (commandfile=!commandfile)", array('!command' => $command['command'], '!commandfile' => $command['commandfile'])), 'bootstrap');
                    $command_found = TRUE;
                    // Dispatch the command(s).
                    $return = drush_dispatch($command);
                    // prevent a '1' at the end of the output
                    if ($return === TRUE) {
                        $return = '';
                    }
                    if (drush_get_context('DRUSH_DEBUG')) {
                        drush_print_timers();
                    }
                    drush_log(dt('Peak memory usage was !peak', array('!peak' => drush_format_size(memory_get_peak_usage()))), 'memory');
                    break;
                }
            }
        } else {
            break;
        }
    }
    if (!$command_found) {
        // If we reach this point, we have not found either a valid or matching command.
        $args = implode(' ', drush_get_arguments());
        if (isset($command) && is_array($command)) {
            foreach ($command['bootstrap_errors'] as $key => $error) {
                drush_set_error($key, $error);
            }
            drush_set_error('DRUSH_COMMAND_NOT_EXECUTABLE', dt("The drush command '!args' could not be executed.", array('!args' => $args)));
        } elseif (!empty($args)) {
            drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt("The drush command '!args' could not be found.", array('!args' => $args)));
        }
        // Set errors that ocurred in the bootstrap phases.
        $errors = drush_get_context('DRUSH_BOOTSTRAP_ERRORS', array());
        foreach ($errors as $code => $message) {
            drush_set_error($code, $message);
        }
    }
    // We set this context to let the shutdown function know we reached the end of drush_main();
    drush_set_context("DRUSH_EXECUTION_COMPLETED", TRUE);
    // After this point the drush_shutdown function will run,
    // exiting with the correct exit code.
    return $return;
}
Example #5
0
 public function log($level, $message, array $context = array())
 {
     // Convert to old $entry array for b/c calls
     $entry = $context + ['type' => $level, 'message' => $message, 'timestamp' => microtime(TRUE), 'memory' => memory_get_usage()];
     // Drush\Log\Logger should take over all of the responsibilities
     // of drush_log, including caching the log messages and sending
     // log messages along to backend invoke.
     // TODO: move these implementations inside this class.
     $log =& drush_get_context('DRUSH_LOG', array());
     $log[] = $entry;
     if ($level != LogLevel::DEBUG_NOTIFY) {
         drush_backend_packet('log', $entry);
     }
     if (drush_get_context('DRUSH_NOCOLOR')) {
         $red = "[%s]";
         $yellow = "[%s]";
         $green = "[%s]";
     } else {
         $red = "[%s]";
         $yellow = "[%s]";
         $green = "[%s]";
     }
     $verbose = drush_get_context('DRUSH_VERBOSE');
     $debug = drush_get_context('DRUSH_DEBUG');
     $debugnotify = drush_get_context('DRUSH_DEBUG_NOTIFY');
     switch ($level) {
         case LogLevel::WARNING:
         case LogLevel::CANCEL:
             $type_msg = sprintf($yellow, $level);
             break;
         case 'failed':
             // Obsolete; only here in case contrib is using it.
         // Obsolete; only here in case contrib is using it.
         case LogLevel::EMERGENCY:
             // Not used by Drush
         // Not used by Drush
         case LogLevel::ALERT:
             // Not used by Drush
         // Not used by Drush
         case LogLevel::ERROR:
             $type_msg = sprintf($red, $level);
             break;
         case LogLevel::OK:
         case 'completed':
             // Obsolete; only here in case contrib is using it.
         // Obsolete; only here in case contrib is using it.
         case LogLevel::SUCCESS:
         case 'status':
             // Obsolete; only here in case contrib is using it.
             // In quiet mode, suppress progress messages
             if (drush_get_context('DRUSH_QUIET')) {
                 return TRUE;
             }
             $type_msg = sprintf($green, $level);
             $level = LogLevel::NOTICE;
             break;
         case LogLevel::NOTICE:
             $type_msg = sprintf("[%s]", $level);
             break;
         case 'message':
             // Obsolete; only here in case contrib is using it.
         // Obsolete; only here in case contrib is using it.
         case LogLevel::INFO:
             if (!$verbose) {
                 // print nothing. exit cleanly.
                 return TRUE;
             }
             $type_msg = sprintf("[%s]", $level);
             $level = LogLevel::INFO;
             break;
         case LogLevel::DEBUG_NOTIFY:
             $level = LogLevel::DEBUG;
             // Report 'debug', handle like 'preflight'
         // Report 'debug', handle like 'preflight'
         case LogLevel::PREFLIGHT:
             if (!$debugnotify) {
                 // print nothing unless --debug AND --verbose. exit cleanly.
                 return TRUE;
             }
             $type_msg = sprintf("[%s]", $level);
             $level = LogLevel::DEBUG;
             break;
         case LogLevel::BOOTSTRAP:
         case LogLevel::DEBUG:
         default:
             if (!$debug) {
                 // print nothing. exit cleanly.
                 return TRUE;
             }
             $type_msg = sprintf("[%s]", $level);
             $level = LogLevel::DEBUG;
             break;
     }
     // When running in backend mode, log messages are not displayed, as they will
     // be returned in the JSON encoded associative array.
     if (drush_get_context('DRUSH_BACKEND')) {
         return;
     }
     $columns = drush_get_context('DRUSH_COLUMNS', 80);
     $width[1] = 11;
     // Append timer and memory values.
     if ($debug) {
         $timer = sprintf('[%s sec, %s]', round($entry['timestamp'] - DRUSH_REQUEST_TIME, 2), drush_format_size($entry['memory']));
         $entry['message'] = $entry['message'] . ' ' . $timer;
     }
     $message = $entry['message'];
     /*
           // Drush-styled output
     
           $message = $this->interpolate(
               $message,
               $this->getLogOutputStyler()->style($context)
           );
     
           $width[0] = ($columns - 11);
     
           $format = sprintf("%%-%ds%%%ds", $width[0], $width[1]);
     
           // Place the status message right aligned with the top line of the error message.
           $message = wordwrap($message, $width[0]);
           $lines = explode("\n", $message);
           $lines[0] = sprintf($format, $lines[0], $type_msg);
           $message = implode("\n", $lines);
           $this->getErrorStreamWrapper()->writeln($message);
     */
     // Robo-styled output
     parent::log($level, $message, $context);
 }