Skip to content
Permalink
Browse files

Dramatically changing how ShellDispatcher and Shell interact.

Shell::runCommand is now responsible for delegating out to tasks and correctly checking methods to run.  ShellDispatcher no longer parses parameters and instead only pulls out the path information that it needs to setup the environment.
  • Loading branch information...
markstory committed Oct 10, 2010
1 parent cd18c82 commit 3d351e7760bf78fb6fa5392a606ce256b0c3a6a2
Showing with 74 additions and 63 deletions.
  1. +37 −0 cake/console/libs/shell.php
  2. +37 −63 cake/console/shell_dispatcher.php
@@ -322,6 +322,43 @@ public function hasMethod($name) {
}
}
/**
* Runs the Shell with the provided argv
*
* @param array $argv Array of arguments to run the shell with. This array should be missing the shell name.
* @return void
*/
public function runCommand($command, $argv) {
$this->startup();
if (!empty($command) && $this->hasTask($command)) {
return $this->{$command}->runCommand('execute', $argv);
}
$this->parser = $this->_getOptionParser();
list($this->params, $this->args) = $this->parser->parse($argv);
if (isset($this->params['help'])) {
return $this->out($this->parser->help());
}
if ($this->hasMethod($command)) {
return $this->{$command}();
}
if ($this->hasMethod('main')) {
return $this->main();
}
throw new RuntimeException(sprintf(__('Unhandled method `%s`'), $command));
}
/**
* Gets the option parser instance and configures it.
* By overriding this method you can configure the ConsoleOptionParser before returning it.
*
* @return ConsoleOptionParser
*/
protected function _getOptionParser() {
$parser = new ConsoleOptionParser($this->name);
return $parser;
}
/**
* Overload get for lazy building of tasks
*
@@ -243,64 +243,43 @@ function __bootstrap() {
* @return boolean
*/
public function dispatch() {
$command = $this->shiftArgs();
$shell = $this->shiftArgs();
if (!$command) {
if (!$shell) {
$this->help();
return false;
}
if (in_array($command, array('help', '--help', '-h'))) {
if (in_array($shell, array('help', '--help', '-h'))) {
$this->help();
return true;
}
list($plugin, $shell) = pluginSplit($command);
list($plugin, $shell) = pluginSplit($shell);
$this->shell = $shell;
$this->shellName = Inflector::camelize($shell);
$this->shellClass = $this->shellName . 'Shell';
$arg = null;
$Shell = $this->_getShell($plugin);
$command = null;
if (isset($this->args[0])) {
$arg = $this->args[0];
$this->shellCommand = Inflector::variable($arg);
$command = $this->args[0];
}
$Shell = $this->_getShell($plugin);
$methods = array();
if ($Shell instanceof Shell) {
$Shell->initialize();
$Shell->loadTasks();
$task = Inflector::camelize($arg);
if (in_array($task, $Shell->taskNames)) {
$this->shiftArgs();
$Shell->{$task}->startup();
if (isset($this->args[0]) && $this->args[0] == 'help') {
if (method_exists($Shell->{$task}, 'help')) {
$Shell->{$task}->help();
} else {
$this->help();
}
return true;
}
return $Shell->{$task}->execute();
}
$methods = array_diff(get_class_methods('Shell'), array('help'));
return $Shell->runCommand($command, $this->args);
}
$methods = array_diff(get_class_methods($Shell), $methods);
$added = in_array(strtolower($arg), array_map('strtolower', $methods));
$private = $arg[0] == '_' && method_exists($Shell, $arg);
$methods = array_diff(get_class_methods($Shell), get_class_methods('Shell'));
$added = in_array($command, $methods);
$private = $arg[0] == '_' && method_exists($Shell, $command);
if (!$private) {
if ($added) {
$this->shiftArgs();
$Shell->startup();
return $Shell->{$arg}();
return $Shell->{$command}();
}
if (method_exists($Shell, 'main')) {
$Shell->startup();
@@ -348,13 +327,19 @@ protected function _getShell($plugin = null) {
}
/**
* Parses command line options
* Parses command line options and extracts the directory paths from $params
*
* @param array $params Parameters to parse
*/
public function parseParams($params) {
$this->__parseParams($params);
$defaults = array('app' => 'app', 'root' => dirname(dirname(dirname(__FILE__))), 'working' => null, 'webroot' => 'webroot');
public function parseParams($args) {
$this->_parsePaths($args);
$defaults = array(
'app' => 'app',
'root' => dirname(dirname(dirname(__FILE__))),
'working' => null,
'webroot' => 'webroot'
);
$params = array_merge($defaults, array_intersect_key($this->params, $defaults));
$isWin = false;
foreach ($defaults as $default => $value) {
@@ -391,35 +376,24 @@ public function parseParams($params) {
}
/**
* Helper for recursively parsing params
* Parses out the paths from from the argv
*
* @return array params
* @access private
* @return void
*/
function __parseParams($params) {
$count = count($params);
for ($i = 0; $i < $count; $i++) {
if (isset($params[$i])) {
if ($params[$i]{0} === '-') {
$key = substr($params[$i], 1);
$this->params[$key] = true;
unset($params[$i]);
if (isset($params[++$i])) {
if ($params[$i]{0} !== '-') {
$this->params[$key] = str_replace('"', '', $params[$i]);
unset($params[$i]);
} else {
$i--;
$this->__parseParams($params);
}
}
} else {
$this->args[] = $params[$i];
unset($params[$i]);
}
protected function _parsePaths($args) {
$parsed = array();
$keys = array('-working', '--working', '-app', '--app', '-root', '--root');
foreach ($keys as $key) {
$index = array_search($key, $args);
if ($index !== false) {
$keyname = str_replace('-', '', $key);
$valueIndex = $index + 1;
$parsed[$keyname] = $args[$valueIndex];
array_splice($args, $index, 2);
}
}
$this->args = $args;
$this->params = $parsed;
}
/**

0 comments on commit 3d351e7

Please sign in to comment.
You can’t perform that action at this time.