Example #1
0
/**
 * The main Drush function.
 *
 * - Runs "early" option code, if set (see global options).
 * - Parses the command line arguments, configuration files and environment.
 * - Prepares and executes a Drupal bootstrap, if possible,
 * - Dispatches the given command.
 *
 * function_exists('drush_main') may be used by modules to detect whether
 * they are being called from Drush.  See http://drupal.org/node/1181308
 * and http://drupal.org/node/827478
 *
 * @return mixed
 *   Whatever the given command returns.
 */
function drush_main()
{
    // Load Drush core include files, and parse command line arguments.
    require dirname(__FILE__) . '/includes/preflight.inc';
    if (drush_preflight_prepare() === FALSE) {
        return 1;
    }
    // Start code coverage collection.
    if ($coverage_file = drush_get_option('drush-coverage', FALSE)) {
        drush_set_context('DRUSH_CODE_COVERAGE', $coverage_file);
        xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
        register_shutdown_function('drush_coverage_shutdown');
    }
    // Load the global Drush configuration files, and global Drush commands.
    // Find the selected site based on --root, --uri or cwd
    // Preflight the selected site, and load any configuration and commandfiles associated with it.
    // Select and return the bootstrap class.
    $bootstrap = drush_preflight();
    // Reset our bootstrap phase to the beginning
    drush_set_context('DRUSH_BOOTSTRAP_PHASE', DRUSH_BOOTSTRAP_NONE);
    $return = '';
    if (!drush_get_error()) {
        if ($file = drush_get_option('early', FALSE)) {
            require_once $file;
            $function = 'drush_early_' . basename($file, '.inc');
            if (function_exists($function)) {
                if ($return = $function()) {
                    // If the function returns FALSE, we continue and attempt to bootstrap
                    // as normal. Otherwise, we exit early with the returned output.
                    if ($return === TRUE) {
                        $return = '';
                    }
                }
            }
        } else {
            // Do any necessary preprocessing operations on the command,
            // perhaps handling immediately.
            $command_handled = drush_preflight_command_dispatch();
            if (!$command_handled) {
                $return = $bootstrap->bootstrap_and_dispatch();
            }
        }
    }
    // TODO: Get rid of global variable access here, and just trust
    // the bootstrap object returned from drush_preflight().  This will
    // require some adjustments to Drush bootstrapping.
    // See: https://github.com/drush-ops/drush/pull/1303
    if ($bootstrap = drush_get_bootstrap_object()) {
        $bootstrap->terminate();
    }
    drush_postflight();
    // How strict are we?  If we are very strict, turn 'ok' into 'error'
    // if there are any warnings in the log.
    if ($return == 0 && drush_get_option('strict') > 1 && drush_log_has_errors()) {
        $return = 1;
    }
    // After this point the drush_shutdown function will run,
    // exiting with the correct exit code.
    return $return;
}
Example #2
0
/**
 * The main Drush function.
 *
 * - Runs "early" option code, if set (see global options).
 * - Parses the command line arguments, configuration files and environment.
 * - Prepares and executes a Drupal bootstrap, if possible,
 * - Dispatches the given command.
 *
 * function_exists('drush_main') may be used by modules to detect whether
 * they are being called from drush.  See http://drupal.org/node/1181308
 * and http://drupal.org/node/827478
 *
 * @return
 *   Whatever the given command returns.
 */
function drush_main()
{
    $return = '';
    if ($file = drush_get_option('early', FALSE)) {
        require_once $file;
        $function = 'drush_early_' . basename($file, '.inc');
        if (function_exists($function)) {
            if ($return = $function()) {
                // If the function returns FALSE, we continue and attempt to bootstrap
                // as normal. Otherwise, we exit early with the returned output.
                if ($return === TRUE) {
                    $return = '';
                }
                drush_bootstrap_finish();
                return $return;
            }
        }
    }
    // Process initial global options such as --debug.
    _drush_bootstrap_global_options();
    $return = '';
    drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUSH);
    if (!drush_get_error()) {
        // Do any necessary preprocessing operations on the command,
        // perhaps handling immediately.
        $command_handled = drush_preflight_command_dispatch();
        if (!$command_handled) {
            $return = _drush_bootstrap_and_dispatch();
        }
    }
    drush_bootstrap_finish();
    // After this point the drush_shutdown function will run,
    // exiting with the correct exit code.
    return $return;
}
 /**
  * Calls a Drush command.
  *
  * This is an exact copy of drush_main from drush.php, but that file
  * cannot be loaded because it produces side effects.
  *
  * @see drush_main
  *
  * @return int|string
  */
 protected static function drushMain()
 {
     $return = '';
     // Start code coverage collection.
     if ($coverage_file = drush_get_option('drush-coverage', false)) {
         drush_set_context('DRUSH_CODE_COVERAGE', $coverage_file);
         xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
         register_shutdown_function('drush_coverage_shutdown');
     }
     /* Set up bootstrap object, so that
      * - 'early' files can bootstrap when needed.
      * - bootstrap constants are available.
      */
     $bootstrap_class = drush_get_option('bootstrap_class', 'Drush\\Boot\\DrupalBoot');
     $bootstrap = new $bootstrap_class();
     drush_set_context('DRUSH_BOOTSTRAP_OBJECT', $bootstrap);
     $bootstrap->preflight();
     // Process initial global options such as --debug.
     _drush_preflight_global_options();
     $return = '';
     drush_preflight();
     if (!drush_get_error()) {
         if ($file = drush_get_option('early', false)) {
             require_once $file;
             $function = 'drush_early_' . basename($file, '.inc');
             if (function_exists($function)) {
                 if ($return = $function()) {
                     // If the function returns FALSE, we continue and attempt to bootstrap
                     // as normal. Otherwise, we exit early with the returned output.
                     if ($return === TRUE) {
                         $return = '';
                     }
                 }
             }
         } else {
             // Do any necessary preprocessing operations on the command,
             // perhaps handling immediately.
             $command_handled = drush_preflight_command_dispatch();
             if (!$command_handled) {
                 $bootstrap = drush_get_context('DRUSH_BOOTSTRAP_OBJECT');
                 $return = $bootstrap->bootstrap_and_dispatch();
             }
         }
     }
     drush_postflight();
     // How strict are we?  If we are very strict, turn 'ok' into 'error'
     // if there are any warnings in the log.
     if ($return == 0 && drush_get_option('strict') > 1 && drush_log_has_errors()) {
         $return = 1;
     }
     // After this point the drush_shutdown function will run,
     // exiting with the correct exit code.
     return $return;
 }
Example #4
0
function drush_return_status()
{
    exit(drush_get_error() ? DRUSH_FRAMEWORK_ERROR : DRUSH_SUCCESS);
}
Example #5
0
/**
 * Shutdown function for use while Drupal is bootstrapping and to return any
 * registered errors.
 *
 * The shutdown command checks whether certain options are set to reliably
 * detect and log some common Drupal initialization errors.
 *
 * If the command is being executed with the --backend option, the script
 * will return a json string containing the options and log information
 * used by the script.
 * 
 * The command will exit with '1' if it was succesfully executed, and the 
 * result of drush_get_error() if it wasn't.
 */
function drush_shutdown()
{
    $phase = drush_get_context('DRUSH_BOOTSTRAP_PHASE');
    if (drush_get_context('DRUSH_BOOTSTRAPPING')) {
        switch ($phase) {
            case DRUSH_BOOTSTRAP_DRUPAL_DATABASE:
                ob_end_clean();
                drush_set_error('DRUSH_DRUPAL_DB_ERROR');
                break;
            case DRUSH_BOOTSTRAP_DRUPAL_FULL:
                ob_end_clean();
                drush_set_error('DRUSH_DRUPAL_BOOTSTRAP_ERROR');
                break;
        }
    }
    if (drush_get_context('DRUSH_BACKEND')) {
        drush_backend_output();
    }
    exit(drush_get_error() ? DRUSH_FRAMEWORK_ERROR : DRUSH_SUCCESS);
}
 /**
  * Apply the settings.
  *
  * @param string $groups
  *   The groups option as passed in from drush_get_option().
  */
 public function exec($groups = '')
 {
     $_groups = !empty($groups) ? explode(',', $groups) : array();
     foreach ($this->exec as $type => $options) {
         if (!empty($options) && (empty($_groups) || in_array($type, $_groups))) {
             // Print a nice heading.
             $heading = $this->formatHeading($type);
             drush_print();
             drush_print("{$heading}");
             switch ($type) {
                 case 'modules':
                     $this->execModules($options);
                     break;
                 case 'variables':
                     $this->execVariables($options);
                     break;
                 case 'permissions':
                     $this->execPermissions($options);
                     break;
                 case 'commands':
                     $this->execCommands($options);
                     break;
                 default:
                     drush_set_error('INVALID_ENV_GROUP', dt("I'm not sure what to do with '!group'.", ['!group' => $type]));
                     break;
             }
         }
     }
     drush_print();
     if (drush_get_error()) {
         drush_set_error('DENVER_ENV_SEUP_FAILEED', dt("The environment may not have been configured the way you wanted.  Check the logs for more details."));
         drush_print(dt("Use the --groups option to run only certain sections of an environment definition."));
     } else {
         return drush_log(dt("Environment setup complete!"), 'success');
     }
 }
Example #7
0
/**
 * Shutdown function for use while Drupal is bootstrapping and to return any
 * registered errors.
 *
 * The shutdown command checks whether certain options are set to reliably
 * detect and log some common Drupal initialization errors.
 *
 * If the command is being executed with the --backend option, the script
 * will return a json string containing the options and log information
 * used by the script.
 * 
 * The command will exit with '1' if it was succesfully executed, and the 
 * result of drush_get_error() if it wasn't.
 */
function drush_shutdown()
{
    // Mysteriously make $user available during sess_write(). Avoids a NOTICE.
    global $user;
    if (!drush_get_context('DRUSH_EXECUTION_COMPLETED', FALSE)) {
        // We did not reach the end of the drush_main function,
        // this generally means somewhere in the code a call to exit(),
        // was made. We catch this, so that we can trigger an error in
        // those cases.
        drush_set_error("DRUSH_NOT_COMPLETED", dt("Drush command could not be completed."));
    }
    $phase = drush_get_context('DRUSH_BOOTSTRAP_PHASE');
    if (drush_get_context('DRUSH_BOOTSTRAPPING')) {
        switch ($phase) {
            case DRUSH_BOOTSTRAP_DRUPAL_FULL:
                ob_end_clean();
                _drush_log_drupal_messages();
                drush_set_error('DRUSH_DRUPAL_BOOTSTRAP_ERROR');
                break;
        }
    }
    if (drush_get_context('DRUSH_BACKEND')) {
        drush_backend_output();
    } elseif (drush_get_context('DRUSH_QUIET')) {
        ob_end_clean();
    }
    // If we are in pipe mode, emit the compact representation of the command, if available.
    if (drush_get_context('DRUSH_PIPE')) {
        drush_pipe_output();
    }
    exit(drush_get_error() ? DRUSH_FRAMEWORK_ERROR : DRUSH_SUCCESS);
}