drush-ops/drush — pull request #624 by weitzman (2014-05-08 17:23:10+00:00)
Title: Refactor Drush's own bootstrap into drush_preflight() and move rest to new DrupalBoot class

Drupal 8 is about to [significantly change its bootstrap](https://drupal.org/node/2016629) (removes early phases), so I decided to refactor Drush in preparation. I will say that we can likely handle D8 within the current Drush bootstrap model so we shouldn't feel obligated to commit this PR. Feedback welcome.

Conceptually, this PR improves the separation of Drush from Drupal. Specifically, a new Drupal "Boot" class now owns the bootstrap process, as I've moved Drush's own bootstrap to a new preflight stage. This separation lets us think more clearly about two potential directions for Drush (not mutually exclusive):
- Break out Drupal support into a new repo and vendor that in via Composer.
- Review all the Drush bits with an eye to replacing them with Symfony components like Console, Process, etc. See #88. Once thats done, Drush is a wrapper around Symfony components. One raison-d'etre for this wrapper is handling redispatch to remote sites via site aliases. Symfony Console doesn't handle that. There are probably other big benefits.

Code changes
--------------
1. Splits the DRUSH_BOOTSTRAP_DRUSH code into a new set of functions called 'preflight' and adds an includes/preflight.inc. drush_main() runs this early on, as before. The DRUSH_BOOTSTRAP_DRUSH constant is deprecated in favor of DRUSH_BOOTSTRAP_NONE.
1. After preflight, Drush hands control to a configurable "Boot" class. The only such class right now is a new lib/Boot/DrupalBoot class. We could conceivably have other Boot classes for Wordpress, Symfony apps, whatever. Those could be vendored in via Composer. Soon, I think we should specialize DrupalBoot into Drupal7Boot, Drupal8Boot, etc.
1. The DrupalBoot class is responsible for all subsequent bootstrapping and dispatching. So, the old /includes/bootstrap.inc has been moved to /lib/Boot and "belongs" to Drupal.
1. After dispatch, Drush resumes control as usual with its postflight and shutdown code. No changes here.

Known issues
----------------
1. During preflight, we no longer find aliases/commandfiles/config that are located in [DRUPAL_ROOT]/drush, [DRUPAL_ROOT]/sites/all/drush, and [DRUPAL_ROOT]/sites/[URI]. That happens later during DRUSH_BOOTSTRAP_DRUPAL_ROOT and DRUSH_BOOTSTRAP_DRUPAL_SITE phases. A byproduct of this is that any remote aliases defined there are found too late for redispatch via Drush's backend. This is why testContextHierarchy is failing. We could mitigate this by:
1. Do nothing. Anyone who wants redispatch from these dirs can specify them with --alias-path (@see [Grayside trick](https://github.com/drush-ops/drush/blob/6.2.0/examples/example.drushrc.php#L305-L316))
1. Let DrupalBoot suggest directories to search during Drush preflight
1. Add redispatch ability during ROOT and SITE phases in Drupal bootstrap.
1. testComplete (i.e. bash completion test) is failing for some reason. Likely fixable.
1. There are still quite a few bootstrap concepts within the Drush code. We can move those into DrupalBoot over time
1. Drupalboot class should be refactured in future so that it isn't a horrid mix of OO and procedural code.