diff --git a/includes/environment.inc b/includes/environment.inc index 8a1d363932..d13fbed3fc 100644 --- a/includes/environment.inc +++ b/includes/environment.inc @@ -317,33 +317,6 @@ function drush_conf_path($server_uri, $require_settings = TRUE) { return $conf; } -/** - * Exhaustive depth-first search to try and locate the Drupal root directory. - * This makes it possible to run Drush from a subdirectory of the drupal root. - * - * @param - * Search start path. Defaults to current working directory. - * @return - * A path to drupal root, or FALSE if not found. - */ -function drush_locate_root($start_path = NULL) { - $drupal_root = FALSE; - - $start_path = empty($start_path) ? drush_cwd() : $start_path; - - $drupalFinder = new DrupalFinder(); - if (!$drupalFinder->locateRoot($start_path)) { -// echo ' Drush must be executed within a Drupal site.'. PHP_EOL; -// exit(1); - } - - // $composerRoot = $drupalFinder->getComposerRoot(); - $drupalRoot = $drupalFinder->getDrupalRoot(); - // chdir($drupalRoot); - - return $drupalRoot; -} - /** * Checks whether given path qualifies as a Drupal root. * diff --git a/includes/preflight.inc b/includes/preflight.inc index 26ad573f0d..934d958b09 100644 --- a/includes/preflight.inc +++ b/includes/preflight.inc @@ -543,13 +543,7 @@ function drush_preflight() { */ function drush_preflight_root() { $root = drush_get_option('root'); - if (!isset($root)) { - $root = drush_locate_root(); - } - if ($root) { - $root = realpath($root); - } - Drush::bootstrapManager()->setRoot($root); + Drush::bootstrapManager()->locateRoot($root); // Load the config options from Drupal's /drush, ../drush, and sites/all/drush directories, // even prior to bootstrapping the root. diff --git a/includes/sitealias.inc b/includes/sitealias.inc index f2bc12cdcd..7aa5dd6560 100644 --- a/includes/sitealias.inc +++ b/includes/sitealias.inc @@ -1443,8 +1443,10 @@ function _drush_find_local_sites_in_sites_folder($a_drupal_root) { // First we'll resolve the realpath of the settings.php file, // so that we get the correct drupal root when symlinks are in use. $real_sitedir = dirname(realpath($filename)); - $real_root = drush_locate_root($filename); - if ($real_root !== FALSE) { + + $drupalFinder = new DrupalFinder(); + if ($drupalFinder->locateRoot($real_sitedir)) { + $real_root = $drupalFinder->getDrupalRoot(); $a_drupal_site = $real_root . '#' . basename($real_sitedir); } // If the symlink points to some folder outside of any drupal @@ -1556,7 +1558,10 @@ function drush_sitealias_build_record_from_settings_file($site_settings_file, $a if (file_exists($site_settings_file)) { if (!isset($drupal_root)) { - $drupal_root = drush_locate_root($site_settings_file); + $drupalFinder = new DrupalFinder(); + if ($drupalFinder->locateRoot(dirname($site_settings_file))) { + $drupal_root = $drupalFinder->getDrupalRoot(); + } } $alias_record['root'] = $drupal_root; diff --git a/src/Boot/BootstrapManager.php b/src/Boot/BootstrapManager.php index 17b1f1ca98..4baeb8b0e3 100644 --- a/src/Boot/BootstrapManager.php +++ b/src/Boot/BootstrapManager.php @@ -2,6 +2,7 @@ namespace Drush\Boot; +use DrupalFinder\DrupalFinder; use Drush\Drush; use Psr\Log\LoggerInterface; use Psr\Log\LoggerAwareInterface; @@ -11,6 +12,11 @@ class BootstrapManager implements LoggerAwareInterface { use LoggerAwareTrait; + /** + * @var DrupalFinder + */ + protected $drupalFinder; + /** * @var \Drush\Boot\Boot[] */ @@ -46,6 +52,7 @@ class BootstrapManager implements LoggerAwareInterface public function __construct(Boot $default) { $this->defaultBootstrapObject = $default; + $this->drupalFinder = new DrupalFinder(); } /** @@ -66,13 +73,28 @@ public function add($candidateList) */ public function getRoot() { - return $this->root; + return $this->drupalFinder->getDrupalRoot(); } - public function setRoot($root) + /** + * Return the composer root for the selected Drupal site. + */ + public function getComposerRoot() + { + return $this->drupalFinder->getComposerRoot(); + } + + public function locateRoot($root, $start_path = NULL) { // TODO: Throw if we already bootstrapped a framework? - $this->root = $root; + + if (!isset($root)) { + $root = drush_cwd(); + } + if (!$this->drupalFinder->locateRoot($root)) { + // echo ' Drush must be executed within a Drupal site.'. PHP_EOL; + // exit(1); + } } /** @@ -133,7 +155,7 @@ protected function selectBootstrapClass() { // Once we have selected a Drupal root, we will reduce our bootstrap // candidates down to just the one used to select this site root. - $bootstrap = $this->bootstrapObjectForRoot($this->root); + $bootstrap = $this->bootstrapObjectForRoot($this->getRoot()); // If we have not found a bootstrap class by this point, // then return our default bootstrap object. The default bootstrap object // should pass through all calls without doing anything that