Skip to content

Commit

Permalink
Refactor shell finding so aliases do not overwrite app classes.
Browse files Browse the repository at this point in the history
Aliasing a shell plugin should not overwrite a core/app shell. Instead
aliases should be fallbacks for when a core/app class cannot be found.

Refs #3325
  • Loading branch information
markstory committed Apr 19, 2014
1 parent 6d39aa8 commit 4000c2f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 181 deletions.
50 changes: 34 additions & 16 deletions src/Console/ShellDispatcher.php
Expand Up @@ -76,6 +76,15 @@ public static function alias($short, $original) {
static::$_aliases[$short] = $original;
}

/**
* Clear any aliases that have been set.
*
* @return void
*/
public static function resetAliases() {
static::$_aliases = [];
}

/**
* Run the dispatcher
*
Expand Down Expand Up @@ -148,7 +157,7 @@ protected function _dispatch() {
return true;
}

$Shell = $this->_getShell($shell);
$Shell = $this->findShell($shell);

$command = null;
if (isset($this->args[0])) {
Expand Down Expand Up @@ -188,27 +197,36 @@ protected function _dispatch() {
* @return mixed An object
* @throws \Cake\Console\Error\MissingShellException when errors are encountered.
*/
protected function _getShell($shell) {
if (isset(static::$_aliases[$shell])) {
public function findShell($shell) {
$classname = $this->_shellExists($shell);
if (!$classname && isset(static::$_aliases[$shell])) {
$shell = static::$_aliases[$shell];
$classname = $this->_shellExists($shell);
}
if ($classname) {
list($plugin) = pluginSplit($shell);
$instance = new $classname();
$instance->plugin = Inflector::camelize(trim($plugin, '.'));
return $instance;
}
list($plugin, $shell) = pluginSplit($shell);
throw new Error\MissingShellException([
'class' => $shell,
]);
}

$plugin = Inflector::camelize($plugin);
/**
* Check if a shell class exists for the given name.
*
* @param string $shell The shell name to look for.
* @return string|boolean Either the classname or false.
*/
protected function _shellExists($shell) {
$class = Inflector::camelize($shell);
if ($plugin) {
$class = $plugin . '.' . $class;
}
$class = App::classname($class, 'Console/Command', 'Shell');

if (!class_exists($class)) {
throw new Error\MissingShellException([
'class' => $shell,
]);
if (class_exists($class)) {
return $class;
}
$Shell = new $class();
$Shell->plugin = trim($plugin, '.');
return $Shell;
return false;
}

/**
Expand Down

0 comments on commit 4000c2f

Please sign in to comment.