Skip to content

Commit

Permalink
Refactored the shell reflection portion into a seperate task.
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Oct 2, 2013
1 parent 5c523a0 commit ac9b7f3
Show file tree
Hide file tree
Showing 6 changed files with 461 additions and 158 deletions.
51 changes: 8 additions & 43 deletions lib/Cake/Console/Command/CommandListShell.php
Expand Up @@ -24,6 +24,13 @@
*/
class CommandListShell extends AppShell {

/**
* Contains tasks to load and instantiate
*
* @var array
*/
public $tasks = array('Command');

/**
* startup
*
Expand Down Expand Up @@ -55,7 +62,7 @@ public function main() {
$this->out(__d('cake_console', "<info>Available Shells:</info>"), 2);
}

$shellList = $this->_getShellList();
$shellList = $this->Command->getShellList();
if (empty($shellList)) {
return;
}
Expand All @@ -67,48 +74,6 @@ public function main() {
}
}

/**
* Gets the shell command listing.
*
* @return array
*/
protected function _getShellList() {
$skipFiles = array('AppShell');

$plugins = CakePlugin::loaded();
$shellList = array_fill_keys($plugins, null) + array('CORE' => null, 'app' => null);

$corePath = App::core('Console/Command');
$shells = App::objects('file', $corePath[0]);
$shells = array_diff($shells, $skipFiles);
$this->_appendShells('CORE', $shells, $shellList);

$appShells = App::objects('Console/Command', null, false);
$appShells = array_diff($appShells, $shells, $skipFiles);
$this->_appendShells('app', $appShells, $shellList);

foreach ($plugins as $plugin) {
$pluginShells = App::objects($plugin . '.Console/Command');
$this->_appendShells($plugin, $pluginShells, $shellList);
}

return array_filter($shellList);
}

/**
* Scan the provided paths for shells, and append them into $shellList
*
* @param string $type
* @param array $shells
* @param array $shellList
* @return void
*/
protected function _appendShells($type, $shells, &$shellList) {
foreach ($shells as $shell) {
$shellList[$type][] = Inflector::underscore(str_replace('Shell', '', $shell));
}
}

/**
* Output text.
*
Expand Down
131 changes: 16 additions & 115 deletions lib/Cake/Console/Command/CompletionShell.php
Expand Up @@ -14,14 +14,21 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

App::uses('CommandListShell', 'Console/Command');
App::uses('AppShell', 'Console/Command');

/**
* Provide command completion shells such as bash.
*
* @package Cake.Console.Command
*/
class CompletionShell extends CommandListShell {
class CompletionShell extends AppShell {

/**
* Contains tasks to load and instantiate
*
* @var array
*/
public $tasks = array('Command');

/**
* Echo no header by overriding the startup method
Expand All @@ -37,7 +44,7 @@ public function startup() {
* @return void
*/
public function main() {
return $this->out($this->OptionParser->help());
return $this->out($this->getOptionParser()->help());
}

/**
Expand All @@ -46,7 +53,7 @@ public function main() {
* @return void
*/
public function commands() {
$options = $this->_commands();
$options = $this->Command->commands();
return $this->_output($options);
}

Expand All @@ -56,26 +63,12 @@ public function commands() {
* @return void
*/
public function options() {
if (!$this->args) {
$parser = new ConsoleOptionParser();
} else {
$Shell = $this->_getShell($this->args[0]);
if (!$Shell) {
$parser = new ConsoleOptionParser();
} else {
$parser = $Shell->getOptionParser();
}
$commandName = '';
if (!empty($this->args[0])) {
$commandName = $this->args[0];
}
$options = $this->Command->options($commandName);

$options = array();
$array = $parser->options();
foreach ($array as $name => $obj) {
$options[] = "--$name";
$short = $obj->short();
if ($short) {
$options[] = "-$short";
}
}
return $this->_output($options);
}

Expand All @@ -89,7 +82,7 @@ public function subCommands() {
return $this->_output();
}

$options = $this->_subCommands($this->args[0]);
$options = $this->Command->subCommands($this->args[0]);
return $this->_output($options);
}

Expand Down Expand Up @@ -148,98 +141,6 @@ public function getOptionParser() {
return $parser;
}

/**
* Return a list of all commands
*
* @return array
*/
protected function _commands() {
$shellList = $this->_getShellList();
unset($shellList['Completion']);

$options = array();
foreach ($shellList as $type => $commands) {
$prefix = '';
if (!in_array(strtolower($type), array('app', 'core'))) {
$prefix = $type . '.';
}

foreach ($commands as $shell) {
$options[] = $prefix . $shell;
}
}

return $options;
}

/**
* Return a list of subcommands for a given command
*
* @param string $commandName
* @return array
*/
protected function _subCommands($commandName) {
$Shell = $this->_getShell($commandName);

if (!$Shell) {
return array();
}

$taskMap = TaskCollection::normalizeObjectArray((array)$Shell->tasks);
$return = array_keys($taskMap);
$return = array_map('Inflector::underscore', $return);

$ShellReflection = new ReflectionClass('AppShell');
$shellMethods = $ShellReflection->getMethods(ReflectionMethod::IS_PUBLIC);
$shellMethodNames = array('main', 'help');
foreach ($shellMethods as $method) {
$shellMethodNames[] = $method->getName();
}

$Reflection = new ReflectionClass($Shell);
$methods = $Reflection->getMethods(ReflectionMethod::IS_PUBLIC);
$methodNames = array();
foreach ($methods as $method) {
$methodNames[] = $method->getName();
}

$return += array_diff($methodNames, $shellMethodNames);
sort($return);

return $return;
}

/**
* Get Shell instance for the given command
*
* @param mixed $commandName
* @return mixed
*/
protected function _getShell($commandName) {
list($pluginDot, $name) = pluginSplit($commandName, true);

if (in_array(strtolower($pluginDot), array('app.', 'core.'))) {
$commandName = $name;
$pluginDot = '';
}

if (!in_array($commandName, $this->_commands())) {
return false;
}

$name = Inflector::camelize($name);
$pluginDot = Inflector::camelize($pluginDot);
$class = $name . 'Shell';
APP::uses($class, $pluginDot . 'Console/Command');

$Shell = new $class();
$Shell->plugin = trim($pluginDot, '.');
$Shell->initialize();
$Shell->loadTasks();

return $Shell;
}

/**
* Emit results as a string, space delimited
*
Expand Down

0 comments on commit ac9b7f3

Please sign in to comment.