Skip to content

Commit

Permalink
Second pass at dispatch refactor
Browse files Browse the repository at this point in the history
Updating command dispatching
Fixing incorrect arg handling
Moving assignments out of conditionals
Updating tests to reset args
Adding dispatch task test
  • Loading branch information
mariuswilms committed May 1, 2009
1 parent ebe1cd2 commit 1d8f57d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 67 deletions.
69 changes: 29 additions & 40 deletions cake/console/cake.php
Expand Up @@ -279,7 +279,9 @@ function __bootstrap() {
* @access public
*/
function dispatch() {
if (!$arg = $this->shiftArgs()) {
$arg = $this->shiftArgs();

if (!$arg) {
$this->help();
return false;
}
Expand All @@ -298,18 +300,23 @@ function dispatch() {
$this->shellName = Inflector::camelize($shell);
$this->shellClass = $this->shellName . 'Shell';

if ($arg = $this->shiftArgs()) {
$arg = null;

if (isset($this->args[0])) {
$arg = $this->args[0];
$this->shellCommand = Inflector::variable($arg);
}

$Shell = $this->_getShell($plugin);

if (!$Shell) {
$message = sprintf(__('Class `%s` could not be loaded', true), $this->shellClass);
$this->stderr($message . "\n");
$title = sprintf(__('Error: Class %s could not be loaded.', true), $this->shellClass);
$this->stderr($title . "\n");
return false;
}

$methods = array();

if (is_a($Shell, 'Shell')) {
$Shell->initialize();
$Shell->loadTasks();
Expand All @@ -321,7 +328,7 @@ function dispatch() {
}
}

$task = Inflector::camelize($this->shellCommand);
$task = Inflector::camelize($arg);

if (in_array($task, $Shell->taskNames)) {
$this->shiftArgs();
Expand All @@ -337,45 +344,27 @@ function dispatch() {
}
return $Shell->{$task}->execute();
}

}

$classMethods = get_class_methods($Shell);

$privateMethod = $missingCommand = false;
if ((in_array($arg, $classMethods) || in_array(strtolower($arg), $classMethods))
&& $arg[0] == '_') {
$privateMethod = true;
}

if (!in_array($arg, $classMethods) && !in_array(strtolower($arg), $classMethods)) {
$missingCommand = true;
}

$protectedCommands = array(
'initialize','in','out','err','hr',
'createfile', 'isdir','copydir','object','tostring',
'requestaction','log','cakeerror', 'shelldispatcher',
'__initconstants','__initenvironment','__construct',
'dispatch','__bootstrap','getinput','stdout','stderr','parseparams','shiftargs'
);

if (in_array(strtolower($arg), $protectedCommands)) {
$missingCommand = true;
$methods = get_class_methods('Shell');
}
$methods = array_diff(get_class_methods($Shell), $methods);
$added = in_array(strtolower($arg), array_map('strtolower', $methods));
$private = $arg[0] == '_' && method_exists($Shell, $arg);

if ($missingCommand && method_exists($Shell, 'main')) {
$Shell->startup();
return $Shell->main();
} elseif (!$privateMethod && method_exists($Shell, $arg)) {
$this->shiftArgs();
$Shell->startup();
return $Shell->{$arg}();
if (!$private) {
if ($added) {
$this->shiftArgs();
$Shell->startup();
return $Shell->{$arg}();
}
if (method_exists($Shell, 'main')) {
$Shell->startup();
return $Shell->main();
}
}

$message = sprintf(__('Unknown %1$s command `%2$s`. For usage try `cake %3$s help`.', true),
$this->shellName, $this->shellCommand, $this->shell);
$this->stderr($message . "\n");
$title = sprintf(__('Error: Unknown %1$s command %2$s.', true), $this->shellName, $arg);
$message = sprintf(__('For usage try `cake %s help`', true), $this->shell);
$this->stderr($title . "\n" . $message . "\n");
return false;
}
/**
Expand Down

0 comments on commit 1d8f57d

Please sign in to comment.