Permalink
Browse files

Second pass at dispatch refactor

Updating command dispatching
Fixing incorrect arg handling
Moving assignments out of conditionals
Updating tests to reset args
Adding dispatch task test
  • Loading branch information...
1 parent ebe1cd2 commit 1d8f57d37a5bdd7b4d6f077e78e7ac4d88b9257b @davidpersson davidpersson committed May 1, 2009
Showing with 104 additions and 67 deletions.
  1. +29 −40 cake/console/cake.php
  2. +75 −27 cake/tests/cases/console/cake.test.php
View
69 cake/console/cake.php
@@ -279,7 +279,9 @@ function __bootstrap() {
* @access public
*/
function dispatch() {
- if (!$arg = $this->shiftArgs()) {
+ $arg = $this->shiftArgs();
+
+ if (!$arg) {
$this->help();
return false;
}
@@ -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();
@@ -321,7 +328,7 @@ function dispatch() {
}
}
- $task = Inflector::camelize($this->shellCommand);
+ $task = Inflector::camelize($arg);
if (in_array($task, $Shell->taskNames)) {
$this->shiftArgs();
@@ -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;
}
/**
View
102 cake/tests/cases/console/cake.test.php
@@ -498,19 +498,21 @@ function testDispatchShellWithMain() {
$Shell->expectOnce('main');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_with_main'));
+ $Dispatcher->args = array('mock_with_main');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array());
$Shell = new MockWithMainShell();
$Shell->setReturnValue('main', true);
$Shell->expectOnce('startup');
$Shell->expectOnce('main');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_with_main', 'initdb'));
+ $Dispatcher->args = array('mock_with_main', 'initdb');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array('initdb'));
$Shell = new MockWithMainShell();
$Shell->setReturnValue('main', true);
@@ -519,37 +521,40 @@ function testDispatchShellWithMain() {
$Shell->expectOnce('main');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_with_main', 'hr'));
+ $Dispatcher->args = array('mock_with_main', 'hr');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array('hr'));
$Shell = new MockWithMainShell();
$Shell->setReturnValue('main', true);
$Shell->expectOnce('startup');
$Shell->expectOnce('main');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_with_main', 'dispatch'));
+ $Dispatcher->args = array('mock_with_main', 'dispatch');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array('dispatch'));
$Shell = new MockWithMainShell();
$Shell->setReturnValue('main', true);
$Shell->expectOnce('startup');
$Shell->expectOnce('main');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_with_main', 'idontexist'));
+ $Dispatcher->args = array('mock_with_main', 'idontexist');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array('idontexist'));
$Shell = new MockWithMainShell();
$Shell->expectNever('startup');
$Shell->expectNever('main');
$Shell->expectNever('_secret');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_with_main', '_secret'));
+ $Dispatcher->args = array('mock_with_main', '_secret');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
}
@@ -571,45 +576,47 @@ function testDispatchShellWithoutMain() {
$Shell->expectNever('startup');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_without_main'));
+ $Dispatcher->args = array('mock_without_main');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
+ $this->assertEqual($Dispatcher->args, array());
$Shell = new MockWithoutMainShell();
$Shell->setReturnValue('initDb', true);
$Shell->expectOnce('startup');
$Shell->expectOnce('initDb');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_without_main', 'initdb'));
+ $Dispatcher->args = array('mock_without_main', 'initdb');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array());
- /* Currently fails when it should not */
- /* $Shell = new MockWithoutMainShell();
+ $Shell = new MockWithoutMainShell();
$Shell->setReturnValue('initDb', true);
$Shell->expectNever('startup');
$Shell->expectNever('hr');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_without_main', 'hr'));
+ $Dispatcher->args = array('mock_without_main', 'hr');
$result = $Dispatcher->dispatch();
- $this->assertFalse($result); */
+ $this->assertFalse($result);
+ $this->assertEqual($Dispatcher->args, array('hr'));
$Shell = new MockWithoutMainShell();
$Shell->setReturnValue('initDb', true);
$Shell->expectNever('startup');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_without_main', 'dispatch'));
+ $Dispatcher->args = array('mock_without_main', 'dispatch');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
$Shell = new MockWithoutMainShell();
$Shell->expectNever('startup');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_without_main', 'idontexist'));
+ $Dispatcher->args = array('mock_without_main', 'idontexist');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
@@ -618,7 +625,7 @@ function testDispatchShellWithoutMain() {
$Shell->expectNever('_secret');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_without_main', '_secret'));
+ $Dispatcher->args = array('mock_without_main', '_secret');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
}
@@ -642,57 +649,62 @@ function testDispatchNotAShellWithMain() {
$Shell->expectOnce('main');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_with_main_not_a'));
+ $Dispatcher->args = array('mock_with_main_not_a');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array());
$Shell = new MockWithMainNotAShell();
$Shell->setReturnValue('main', true);
$Shell->expectOnce('startup');
$Shell->expectOnce('main');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_with_main_not_a', 'initdb'));
+ $Dispatcher->args = array('mock_with_main_not_a', 'initdb');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array('initdb'));
$Shell = new MockWithMainNotAShell();
$Shell->setReturnValue('main', true);
$Shell->expectOnce('startup');
$Shell->expectOnce('main');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_with_main_not_a', 'hr'));
+ $Dispatcher->args = array('mock_with_main_not_a', 'hr');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array('hr'));
$Shell = new MockWithMainNotAShell();
$Shell->setReturnValue('main', true);
$Shell->expectOnce('startup');
$Shell->expectOnce('main');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_with_main_not_a', 'dispatch'));
+ $Dispatcher->args = array('mock_with_main_not_a', 'dispatch');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array('dispatch'));
$Shell = new MockWithMainNotAShell();
$Shell->setReturnValue('main', true);
$Shell->expectOnce('startup');
$Shell->expectOnce('main');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_with_main_not_a', 'idontexist'));
+ $Dispatcher->args = array('mock_with_main_not_a', 'idontexist');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array('idontexist'));
$Shell = new MockWithMainNotAShell();
$Shell->expectNever('startup');
$Shell->expectNever('main');
$Shell->expectNever('_secret');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_with_main_not_a', '_secret'));
+ $Dispatcher->args = array('mock_with_main_not_a', '_secret');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
}
@@ -715,7 +727,7 @@ function testDispatchNotAShellWithoutMain() {
$Shell->expectNever('startup');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_without_main_not_a'));
+ $Dispatcher->args = array('mock_without_main_not_a');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
@@ -725,16 +737,17 @@ function testDispatchNotAShellWithoutMain() {
$Shell->expectOnce('initDb');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_without_main_not_a', 'initdb'));
+ $Dispatcher->args = array('mock_without_main_not_a', 'initdb');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array());
$Shell = new MockWithoutMainNotAShell();
$Shell->setReturnValue('initDb', true);
$Shell->expectNever('startup');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_without_main_not_a', 'hr'));
+ $Dispatcher->args = array('mock_without_main_not_a', 'hr');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
@@ -743,15 +756,15 @@ function testDispatchNotAShellWithoutMain() {
$Shell->expectNever('startup');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_without_main_not_a', 'dispatch'));
+ $Dispatcher->args = array('mock_without_main_not_a', 'dispatch');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
$Shell = new MockWithoutMainNotAShell();
$Shell->expectNever('startup');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_without_main_not_a', 'idontexist'));
+ $Dispatcher->args = array('mock_without_main_not_a', 'idontexist');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
@@ -760,11 +773,46 @@ function testDispatchNotAShellWithoutMain() {
$Shell->expectNever('_secret');
$Dispatcher->TestShell =& $Shell;
- $Dispatcher->parseParams(array('mock_without_main_not_a', '_secret'));
+ $Dispatcher->args = array('mock_without_main_not_a', '_secret');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
}
/**
+ * Verify that a task is called instead of the shell if the first arg equals
+ * the name of the task
+ *
+ * @access public
+ * @return void
+ */
+ function testDispatchTask() {
+ Mock::generate('Shell', 'MockWeekShell', array('main'));
+ Mock::generate('Shell', 'MockOnSundayTask', array('execute'));
+
+ $Dispatcher =& new TestShellDispatcher();
+
+ $Shell = new MockWeekShell();
+ $Shell->expectOnce('initialize');
+ $Shell->expectOnce('loadTasks');
+ $Shell->expectNever('startup');
+ $Shell->expectNever('main');
+
+ $Task = new MockOnSundayTask();
+ $Task->setReturnValue('execute', true);
+ $Task->expectOnce('initialize');
+ $Task->expectOnce('loadTasks');
+ $Task->expectOnce('startup');
+ $Task->expectOnce('execute');
+
+ $Shell->MockOnSunday =& $Task;
+ $Shell->taskNames = array('MockOnSunday');
+ $Dispatcher->TestShell =& $Shell;
+
+ $Dispatcher->args = array('mock_week', 'mock_on_sunday');
+ $result = $Dispatcher->dispatch();
+ $this->assertTrue($result);
+ $this->assertEqual($Dispatcher->args, array());
+ }
+/**
* Verify shifting of arguments
*
* @access public

0 comments on commit 1d8f57d

Please sign in to comment.