function drush_preflight

8.0.x preflight.inc drush_preflight()
7.x preflight.inc drush_preflight()
master preflight.inc drush_preflight()

During the initialization of Drush, this is the first step where we load our configuration and commandfiles, and select the site we are going to operate on; however, we take no irreversible actions (e.g. site bootstrapping). This allows commands that are declared with no bootstrap to select a new site root and bootstrap it.

In this step we will register the shutdown function, parse the command line arguments and store them in their related contexts.

Configuration files (drushrc.php) that are a) Specified on the command line b) Stored in the root directory of drush.php c) Stored in the home directory of the system user.

Additionally the DRUSH_QUIET and DRUSH_BACKEND contexts, will be evaluated now, as they need to be set very early in the execution flow to be able to take affect.

Return value

\Drush\Boot\Boot;

5 calls to drush_preflight()
commandUnitCase::testCommandVersionSpecific in tests/commandUnitTest.php
Assure that matching version-specific command files are loaded and others are ignored.
commandUnitCase::testGetCommands in tests/commandUnitTest.php
Assert that $command has interesting properties. Reference command by it's alias (dl) to assure that those aliases are built as expected.
drush_main in ./drush.php
The main Drush function.
outputFormatUnitCase::testOutputFormat in tests/outputFormatUnitTest.php
Test various output formats using php-eval with no Drupal site.
tablesUnitTest::setUp in tests/tablesUnitTest.php

File

includes/preflight.inc, line 128
Preflight, postflight and shutdown code.

Code

function drush_preflight() {
  // Create an alias '@none' to represent no Drupal site
  _drush_sitealias_cache_alias('@none', array('root' => '', 'uri' => ''));

  // Discover terminal width for pretty output.
  _drush_preflight_columns();

  // Display is tidy now that column width has been handled.
  drush_log(dt('Starting Drush preflight.'), LogLevel::PREFLIGHT);

  // Statically define a way to call drush again.
  define('DRUSH_COMMAND', drush_find_drush());

  // prime the CWD cache
  drush_cwd();

  // Set up base environment for system-wide file locations.
  _drush_preflight_base_environment();

  // Setup global alias_paths[] in context system.
  if (!drush_get_option('local')) {
    _drush_preflight_alias_path();
  }
  if (!drush_get_option('local')) {
    // Load a drushrc.php file in the drush.php's directory.
    drush_load_config('drush');

    // Load a drushrc.php file in the $ETC_PREFIX/etc/drush directory.
    drush_load_config('system');

    // Load a drushrc.php file at ~/.drushrc.php.
    drush_load_config('user');

    // Load a drushrc.php file in the ~/.drush directory.
    drush_load_config('home.drush');
  }

  // Load a custom config specified with the --config option.
  drush_load_config('custom');

  _drush_preflight_global_options();
  // Load all the commandfiles findable from any of the
  // scopes listed above.
  _drush_find_commandfiles_drush();

  // Look up the alias identifier that the user wants to use,
  // either via an arguement or via 'site-set'.
  $target_alias = drush_sitealias_check_arg_and_site_set();

  // Process the site alias that specifies which instance
  // of Drush (local or remote) this command will operate on.
  // We must do this after we load our config files (so that
  // site aliases are available), but before the rest of
  // Drush preflight and Drupal root bootstrap phase are
  // done, since site aliases may set option values that
  // affect these phases.
  $alias_record = _drush_sitealias_set_context_by_name($target_alias);

  // Find the selected site based on --root, --uri or cwd
  drush_preflight_root();

  // Preflight the selected site, and load any configuration and commandfiles associated with it.
  drush_preflight_site();

  // Check to see if anything changed during the 'site' preflight
  // that might allow us to find our alias record now
  if (empty($alias_record)) {
    $alias_record = _drush_sitealias_set_context_by_name($target_alias);

    // If the site alias settings changed late in the preflight,
    // then run the preflight for the root and site contexts again.
    if (!empty($alias_record)) {
      $remote_host = drush_get_option('remote-host');
      if (!isset($remote_host)) {
        drush_preflight_root();
        drush_preflight_site();
      }
    }
  }

  // Fail if we could not find the selected site alias.
  if ($target_alias && empty($alias_record)) {
    // We will automatically un-set the site-set alias if it could not be found.
    // Otherwise, we'd be stuck -- the user would only be able to execute Drush
    // commands again after `drush @none site-set @none`, and most folks would
    // have a hard time figuring that out.
    $site_env = drush_sitealias_site_get();
    if ($site_env == $target_alias) {
      drush_sitealias_site_clear();
    }
    return drush_set_error('DRUSH_BOOTSTRAP_NO_ALIAS', dt("Could not find the alias !alias", array('!alias' => $target_alias)));
  }

  // If applicable swaps in shell alias values.
  drush_shell_alias_replace($target_alias);

  // Copy global options to their respective contexts
  _drush_preflight_global_options();

  // Set environment variables based on #env-vars.
  drush_set_environment_vars($alias_record);

  // Select the bootstrap object and return it.
  return drush_select_bootstrap_class();
}