From 59cf90536c8f3d0c7fbba069f8e2fad904542ce2 Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Thu, 5 Feb 2015 18:41:23 -0800 Subject: [PATCH] #572: Move commandfile cache to its own class, so that composer libraries can interact with Drush via Drush\Drush::autoload(). --- includes/command.inc | 43 ++----------- includes/environment.inc | 3 +- includes/preflight.inc | 1 + lib/Drush/Boot/command.inc | 2 +- lib/Drush/Command/Commandfiles.php | 70 +++++++++++++++++++++ lib/Drush/Command/CommandfilesInterface.php | 10 +++ lib/Drush/Drush.php | 63 +++++++++++++++++++ 7 files changed, 151 insertions(+), 41 deletions(-) create mode 100644 lib/Drush/Command/Commandfiles.php create mode 100644 lib/Drush/Command/CommandfilesInterface.php create mode 100644 lib/Drush/Drush.php diff --git a/includes/command.inc b/includes/command.inc index ad9216e03c..b05219c847 100644 --- a/includes/command.inc +++ b/includes/command.inc @@ -1365,48 +1365,13 @@ function drush_command_normalize_name($command_name) { * command files. */ function drush_commandfile_list() { - return drush_get_context('DRUSH_COMMAND_FILES', array()); -} - -/** - * Register a single commandfile with Drush. - * - * Called by `drush_autoload()` and `_drush_add_commandfiles()`. - */ -function drush_add_commandfile($commandfile) { - $load_command = FALSE; - $cache =& drush_get_context('DRUSH_COMMAND_FILES', array()); - $deferred =& drush_get_context('DRUSH_DEFERRED_COMMAND_FILES', array()); - $module = basename($commandfile); - $module = preg_replace('/\.*drush[0-9]*\.inc/', '', $module); - $module_versionless = preg_replace('/\.d([0-9]+)$/', '', $module); - if (!isset($cache[$module_versionless])) { - $drupal_version = ''; - if (preg_match('/\.d([0-9]+)$/', $module, $matches)) { - $drupal_version = $matches[1]; - } - if (empty($drupal_version) || ($drupal_version == drush_drupal_major_version())) { - $load_command = TRUE; - $cache[$module_versionless] = $commandfile; - require_once $commandfile; - unset($deferred[$module]); - } - elseif (!empty($drupal_version)) { - // Signal that we should try again on - // the next bootstrap phase. - $deferred[$module] = $commandfile; - } - } - return $load_command; + return Drush\Drush::commandfiles_cache()->get(); } function _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE) { static $evaluated = array(); $needs_sort = FALSE; - $cache =& drush_get_context('DRUSH_COMMAND_FILES', array()); - $deferred =& drush_get_context('DRUSH_DEFERRED_COMMAND_FILES', array()); - if (count($searchpath)) { if (!$reset) { // Assemble a cid specific to the bootstrap phase and searchpaths. @@ -1427,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 = $deferred; + $list = Drush\Drush::commandfiles_cache()->deferred(); if (isset($cached_list)) { $list = array_merge($list, $cached_list); } @@ -1460,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_add_commandfile($filepath); + $load_command = Drush\Drush::commandfiles_cache()->add($filepath); if ($load_command) { $needs_sort = TRUE; } @@ -1472,7 +1437,7 @@ function _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE) { } if ($needs_sort) { - ksort($cache); + Drush\Drush::commandfiles_cache()->sort(); } } } diff --git a/includes/environment.inc b/includes/environment.inc index 71c7c6d4a8..ca53188632 100644 --- a/includes/environment.inc +++ b/includes/environment.inc @@ -300,7 +300,7 @@ function drush_cwd() { * On the last line of any *.drush.inc commandfile, add: * * drush_autoload(__FILE__); - */ + * function drush_autoload($commandfile) { $already_added = drush_add_commandfile($commandfile); @@ -317,6 +317,7 @@ function drush_autoload($commandfile) { } } } + */ /** * Converts a Windows path (dir1\dir2\dir3) into a Unix path (dir1/dir2/dir3). diff --git a/includes/preflight.inc b/includes/preflight.inc index 88bc98ef67..025989f280 100644 --- a/includes/preflight.inc +++ b/includes/preflight.inc @@ -30,6 +30,7 @@ function drush_preflight_prepare() { fwrite(STDERR, $msg); return FALSE; } + // drush_drupal_load_autoloader() uses require_once. Is this going to be // a problem if $vendor_path == $drupal_root .'/core/vendor/autoload.php' require $vendor_path; diff --git a/lib/Drush/Boot/command.inc b/lib/Drush/Boot/command.inc index d864e66960..fc7b5a63ab 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_get_context('DRUSH_COMMAND_FILES', array()); + $command_files = Drush\Drush::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/Command/Commandfiles.php b/lib/Drush/Command/Commandfiles.php new file mode 100644 index 0000000000..0c944719a7 --- /dev/null +++ b/lib/Drush/Command/Commandfiles.php @@ -0,0 +1,70 @@ +cache = array(); + $this->deferred = array(); + } + + function get() { + return $this->cache; + } + + function deferred() { + return $this->deferred; + } + + function sort() { + ksort($this->cache); + } + + function add($commandfile) { + $load_command = FALSE; + + $module = basename($commandfile); + $module = preg_replace('/\.*drush[0-9]*\.inc/', '', $module); + $module_versionless = preg_replace('/\.d([0-9]+)$/', '', $module); + if (!isset($this->cache[$module_versionless])) { + $drupal_version = ''; + if (preg_match('/\.d([0-9]+)$/', $module, $matches)) { + $drupal_version = $matches[1]; + } + if (empty($drupal_version)) { + $load_command = TRUE; + } + else { + if (function_exists('drush_drupal_major_version') && ($drupal_version == drush_drupal_major_version())) { + $load_command = TRUE; + } + else { + // Signal that we should try again on + // the next bootstrap phase. + $this->deferred[$module] = $commandfile; + } + } + if ($load_command) { + $this->cache[$module_versionless] = $commandfile; + require_once $commandfile; + unset($this->deferred[$module]); + } + } + return $load_command; + } +} diff --git a/lib/Drush/Command/CommandfilesInterface.php b/lib/Drush/Command/CommandfilesInterface.php new file mode 100644 index 0000000000..acb7c2cdb6 --- /dev/null +++ b/lib/Drush/Command/CommandfilesInterface.php @@ -0,0 +1,10 @@ +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; + } +}