diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index 9cf7325a93..9e06f431e7 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -101,3 +101,30 @@ define('DRUSH_BOOTSTRAP_DRUPAL_FULL', 5); * be different based on who is logged in. */ define('DRUSH_BOOTSTRAP_DRUPAL_LOGIN', 6); + +/** + * Used by a Drush extension to request that its Composer autoload + * files be loaded by Drush, if they have not already been. + * + * Usage: + * + * function myextension_init() { + * drush_autoload(__FILE__) + * } + */ +function drush_autoload($commandfile) { + $already_added = commandfiles_cache()->add($commandfile); + + if (!$already_added) { + $dir = dirname($commandfile); + $candidates = array("vendor/autoload.php", "../../../vendor/autoload.php"); + $drush_autoload_file = drush_get_context('DRUSH_VENDOR_PATH', ''); + + foreach ($candidates as $candidate) { + $autoload = $dir . '/' . $candidate; + if (file_exists($autoload) && (realpath($autoload) != $drush_autoload_file)) { + include $autoload; + } + } + } +} diff --git a/includes/command.inc b/includes/command.inc index b05219c847..dd49a69f2a 100644 --- a/includes/command.inc +++ b/includes/command.inc @@ -1365,7 +1365,7 @@ function drush_command_normalize_name($command_name) { * command files. */ function drush_commandfile_list() { - return Drush\Drush::commandfiles_cache()->get(); + return commandfiles_cache()->get(); } function _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE) { @@ -1392,7 +1392,7 @@ function _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE) { // Build a list of all of the modules to attempt to load. // Start with any modules deferred from a previous phase. - $list = Drush\Drush::commandfiles_cache()->deferred(); + $list = commandfiles_cache()->deferred(); if (isset($cached_list)) { $list = array_merge($list, $cached_list); } @@ -1425,7 +1425,7 @@ function _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE) { // command file cache may not be available anymore, in which case // we rebuild the cache for this phase. if ($filepath = realpath($filename)) { - $load_command = Drush\Drush::commandfiles_cache()->add($filepath); + $load_command = commandfiles_cache()->add($filepath); if ($load_command) { $needs_sort = TRUE; } @@ -1437,7 +1437,7 @@ function _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE) { } if ($needs_sort) { - Drush\Drush::commandfiles_cache()->sort(); + commandfiles_cache()->sort(); } } } @@ -1772,3 +1772,12 @@ function drush_shell_alias_replace() { _drush_preflight_global_options(); } } + +function commandfiles_cache() { + static $commandfiles_cache = NULL; + + if (!isset($commandfiles_cache)) { + $commandfiles_cache = new Drush\Command\Commandfiles(); + } + return $commandfiles_cache; +} diff --git a/lib/Drush/Boot/command.inc b/lib/Drush/Boot/command.inc index fc7b5a63ab..c36a9cf6ae 100644 --- a/lib/Drush/Boot/command.inc +++ b/lib/Drush/Boot/command.inc @@ -135,7 +135,7 @@ function drush_command_belongs_to_disabled_module() { } else { // The command does not define Drupal dependencies. Derive them. - $command_files = Drush\Drush::commandfiles_cache()->get(); + $command_files = commandfiles_cache()->get(); $command_path = $commands[$command_name]['path'] . DIRECTORY_SEPARATOR . $commands[$command_name]['commandfile'] . '.drush.inc'; $modules = array_search($command_path, $command_files); } diff --git a/lib/Drush/Drush.php b/lib/Drush/Drush.php deleted file mode 100644 index 130da33d85..0000000000 --- a/lib/Drush/Drush.php +++ /dev/null @@ -1,63 +0,0 @@ -add($commandfile); - - if (!$already_added) { - $dir = dirname($commandfile); - $candidates = array("vendor/autoload.php", "../../../vendor/autoload.php"); - $drush_autoload_file = drush_get_context('DRUSH_VENDOR_PATH', ''); - - foreach ($candidates as $candidate) { - $autoload = $dir . '/' . $candidate; - if (file_exists($autoload) && (realpath($autoload) != $drush_autoload_file)) { - include $autoload; - } - } - } - } - - // ====== Things used internally by Drush ====== - - static function set_commandfiles_cache($commandfiles_cache) { - self::$commandfiles_cache = $commandfiles_cache; - } - - static function commandfiles_cache() { - if (!isset(self::$commandfiles_cache)) { - self::$commandfiles_cache = new Command\Commandfiles(); - } - return self::$commandfiles_cache; - } -}