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.
This commit is contained in:
mark_story 2010-10-09 22:48:34 -04:00
parent cd18c8214c
commit 3d351e7760
2 changed files with 75 additions and 64 deletions

View file

@ -322,6 +322,43 @@ class Shell extends Object {
}
}
/**
* 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
*

View file

@ -243,64 +243,43 @@ class ShellDispatcher {
* @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;
if (isset($this->args[0])) {
$arg = $this->args[0];
$this->shellCommand = Inflector::variable($arg);
}
$Shell = $this->_getShell($plugin);
$methods = array();
$command = null;
if (isset($this->args[0])) {
$command = $this->args[0];
}
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 $Shell->runCommand($command, $this->args);
}
return true;
}
return $Shell->{$task}->execute();
}
$methods = array_diff(get_class_methods('Shell'), array('help'));
}
$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 @@ class ShellDispatcher {
}
/**
* 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 @@ class ShellDispatcher {
}
/**
* 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;
}
/**