function BaseBoot::bootstrap_and_dispatch

8.0.x BaseBoot.php BaseBoot::bootstrap_and_dispatch()
7.x BaseBoot.php BaseBoot::bootstrap_and_dispatch()
master BaseBoot.php BaseBoot::bootstrap_and_dispatch()

Main entrypoint to bootstrap the selected CMS and execute the selected command.

The implementation provided in BaseBoot should be sufficient; this method usually will not need to be overridden.

Overrides Boot::bootstrap_and_dispatch

File

lib/Drush/Boot/BaseBoot.php, line 52

Namespace

Drush\Boot

Class

BaseBoot

Code

function bootstrap_and_dispatch() {
  $phases = $this->bootstrap_init_phases();

  $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)) {
        $command += $this->command_defaults();
        // Insure that we have bootstrapped to a high enough
        // phase for the command prior to enforcing requirements.
        $bootstrap_result = drush_bootstrap_to_phase($command['bootstrap']);
        $this->enforce_requirement($command);

        if ($bootstrap_result && empty($command['bootstrap_errors'])) {
          $this->logger->log(LogLevel::BOOTSTRAP, dt("Found command: !command (commandfile=!commandfile)", array('!command' => $command['command'], '!commandfile' => $command['commandfile'])));

          $command_found = TRUE;
          // Dispatch the command(s).
          $return = drush_dispatch($command);

          if (drush_get_context('DRUSH_DEBUG') && !drush_get_context('DRUSH_QUIET')) {
            // @todo Create version independant wrapper around Drupal timers. Use it.
            drush_print_timers();
          }
          break;
        }
      }
    }
    else {
      break;
    }
  }

  // TODO: If we could not find a legacy Drush command, try running a
  // command via the Symfony application. See also drush_main() in preflight.inc;
  // ultimately, the Symfony application should be called from there.
  if (!$command_found && isset($command)) {
    $container = \Drush::getContainer();
    $application = $container->get('application');
    $args = drush_get_arguments();
    if (count($args)) {
      $name = $args[0];
      if ($this->hasRegisteredSymfonyCommand($application, $name)) {
        $command_found = true;
        $input = drush_symfony_input();
        $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.)"));
        $application->run($input);
      }
    }
  }

  if (!$command_found) {
    // If we reach this point, command doesn't fit requirements or we have not
    // found either a valid or matching command.
    $this->report_command_error($command);
  }

  // Prevent a '1' at the end of the output.
  if ($return === TRUE) {
    $return = '';
  }

  return $return;
}