Skip to content

Commit

Permalink
Allow Shells to be dispatched with a new "extra" arguments
Browse files Browse the repository at this point in the history
This "extra" arguments allow additional parameters to be passed to the Shell without them needing to be a CLI flag or a command argument.
 This commit also introduce a "requested" extra parameter that is automatically passed when a Shell is dispatched with a Shell::dispatchShell() call. This parameter, when set and not null or false, prevents the default cake shell welcome message from being displayed
  • Loading branch information
HavokInspiration committed Apr 17, 2015
1 parent faa531f commit 9933497
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
29 changes: 25 additions & 4 deletions src/Console/Shell.php
Expand Up @@ -211,7 +211,9 @@ public function initialize()
*/
public function startup()
{
$this->_welcome();
if (!(bool)$this->param('requested')) {
$this->_welcome();
}
}

/**
Expand Down Expand Up @@ -304,12 +306,23 @@ public function hasMethod($name)
public function dispatchShell()
{
$args = func_get_args();
if (is_string($args[0]) && count($args) === 1) {
$extra = [];
if (is_array($args[0]) && isset($args[0]['command'])) {
if (!empty($args[0]['extra'])) {
$extra = $args[0]['extra'];
}

$args = explode(' ', $args[0]['command']);
} elseif (is_string($args[0]) && count($args) === 1) {
$args = explode(' ', $args[0]);
}

if (!isset($extra['requested'])) {
$extra['requested'] = true;
}

$dispatcher = new ShellDispatcher($args, false);
return $dispatcher->dispatch();
return $dispatcher->dispatch($extra);
}

/**
Expand All @@ -332,10 +345,14 @@ public function dispatchShell()
* @param array $argv Array of arguments to run the shell with. This array should be missing the shell name.
* @param bool $autoMethod Set to true to allow any public method to be called even if it
* was not defined as a subcommand. This is used by ShellDispatcher to make building simple shells easy.
* @param array $extra Extra parameters that you can manually pass to the Shell
* to be dispatched.
* Built-in extra parameter is :
* - `requested` : if used, will prevent the Shell welcome message to be displayed
* @return mixed
* @link http://book.cakephp.org/3.0/en/console-and-shells.html#the-cakephp-console
*/
public function runCommand($argv, $autoMethod = false)
public function runCommand($argv, $autoMethod = false, $extra = [])
{
$command = isset($argv[0]) ? $argv[0] : null;
$this->OptionParser = $this->getOptionParser();
Expand All @@ -347,6 +364,10 @@ public function runCommand($argv, $autoMethod = false)
return false;
}

if (!empty($extra) && is_array($extra)) {
$this->params = array_merge($this->params, $extra);
}

if (!empty($this->params['quiet'])) {
$this->_io->level(ConsoleIo::QUIET);
$this->_io->setLoggers(false);
Expand Down
21 changes: 15 additions & 6 deletions src/Console/ShellDispatcher.php
Expand Up @@ -114,12 +114,13 @@ public static function resetAliases()
* Run the dispatcher
*
* @param array $argv The argv from PHP
* @param array $extra Extra parameters
* @return int The exit code of the shell process.
*/
public static function run($argv)
public static function run($argv, $extra = [])
{
$dispatcher = new ShellDispatcher($argv);
return $dispatcher->dispatch();
return $dispatcher->dispatch($extra);
}

/**
Expand Down Expand Up @@ -164,11 +165,15 @@ protected function _bootstrap()
* Converts a shell command result into an exit code. Null/True
* are treated as success. All other return values are an error.
*
* @param array $extra Extra parameters that you can manually pass to the Shell
* to be dispatched.
* Built-in extra parameter is :
* - `requested` : if used, will prevent the Shell welcome message to be displayed
* @return int The cli command exit code. 0 is success.
*/
public function dispatch()
public function dispatch($extra = [])
{
$result = $this->_dispatch();
$result = $this->_dispatch($extra);
if ($result === null || $result === true) {
return 0;
}
Expand All @@ -178,10 +183,14 @@ public function dispatch()
/**
* Dispatch a request.
*
* @param array $extra Extra parameters that you can manually pass to the Shell
* to be dispatched.
* Built-in extra parameter is :
* - `requested` : if used, will prevent the Shell welcome message to be displayed
* @return bool
* @throws \Cake\Console\Exception\MissingShellMethodException
*/
protected function _dispatch()
protected function _dispatch($extra = [])
{
$shell = $this->shiftArgs();

Expand All @@ -197,7 +206,7 @@ protected function _dispatch()
$Shell = $this->findShell($shell);

$Shell->initialize();
return $Shell->runCommand($this->args, true);
return $Shell->runCommand($this->args, true, $extra);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/TestCase/Console/ShellTest.php
Expand Up @@ -615,6 +615,33 @@ public function testRunCommandWithMethod()
$this->assertTrue($result);
}

/**
* test that a command called with an extra parameter passed merges the extra parameters
* to the shell's one
* Also tests that if an extra `requested` parameter prevents the welcome message from
* being displayed
*
* @return void
*/
public function testRunCommandWithExtra()
{
$Parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
$io = $this->getMock('Cake\Console\ConsoleIo');
$Shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'slice', '_welcome', 'param'], [$io]);
$Parser->addSubCommand('slice');
$Shell->expects($this->once())
->method('getOptionParser')
->will($this->returnValue($Parser));
$Shell->expects($this->once())
->method('slice')
->with('cakes');
$Shell->expects($this->never())->method('_welcome');
$Shell->expects($this->once())->method('param')
->with('requested')
->will($this->returnValue(true));
$Shell->runCommand(['slice', 'cakes'], false, ['requested' => true]);
}

/**
* Test that runCommand() doesn't call public methods when the second arg is false.
*
Expand Down

0 comments on commit 9933497

Please sign in to comment.