Skip to content

Commit

Permalink
drush-ops#572: Move commandfile cache to its own class, so that compo…
Browse files Browse the repository at this point in the history
…ser libraries can interact with Drush via Drush\Drush::autoload().
  • Loading branch information
greg-1-anderson committed Feb 25, 2015
1 parent 4983394 commit 59cf905
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 41 deletions.
43 changes: 4 additions & 39 deletions includes/command.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -1472,7 +1437,7 @@ function _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE) {
}

if ($needs_sort) {
ksort($cache);
Drush\Drush::commandfiles_cache()->sort();
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion includes/environment.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -317,6 +317,7 @@ function drush_autoload($commandfile) {
}
}
}
*/

/**
* Converts a Windows path (dir1\dir2\dir3) into a Unix path (dir1/dir2/dir3).
Expand Down
1 change: 1 addition & 0 deletions includes/preflight.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/Drush/Boot/command.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
70 changes: 70 additions & 0 deletions lib/Drush/Command/Commandfiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* @file
* Definition of Drush\Command\Commandfiles.
*/

namespace Drush\Command;

/**
* Default commandfiles implementation.
*
* This class manages the list of commandfiles that are active
* in Drush for the current command invocation.
*/
class Commandfiles implements CommandfilesInterface {
const FOO = 'my constant';
protected $cache;
protected $deferred;

function __construct() {
$this->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;
}
}
10 changes: 10 additions & 0 deletions lib/Drush/Command/CommandfilesInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Drush\Command;

interface CommandfilesInterface {
function add($commandfile);
function get();
function deferred();
function sort();
}
63 changes: 63 additions & 0 deletions lib/Drush/Drush.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* @file
* Definition of Drush\Drush.
*/

namespace Drush;

/**
* The Drush API.
*/
class Drush {
static $commandfiles_cache;

/**
* Run a drush command in a new process
*/
static function drush($site, $command_name, $commandline_args = array(), $commandline_options = array(), $backend_options = TRUE) {
return drush_invoke_process($site, $command_name, $commandline_args, $commandline_options, $backend_options);
}

/**
* Run a drush subcommand in the same process, with the same options as the primary command.
*/
static function invoke($command, $arguments = array()) {
return drush_invoke($command, $arguments);
}

/**
* Register a Drush extension commandfile, and load its autoload
* file, if necessary.
*/
static function autoload($commandfile) {
$already_added = self::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;
}
}
}
}

// ====== 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;
}
}

0 comments on commit 59cf905

Please sign in to comment.