forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
drush-ops#572: Move commandfile cache to its own class, so that compo…
…ser libraries can interact with Drush via Drush\Drush::autoload().
- Loading branch information
1 parent
4983394
commit 59cf905
Showing
7 changed files
with
151 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |