Skip to content

Commit

Permalink
Add version shell and --version
Browse files Browse the repository at this point in the history
By having aliases and standalone shells we avoid having 'command' logic
in the command runner. It also means that application developers can
replace the default --version behavior if they want to.
  • Loading branch information
markstory committed Jun 24, 2017
1 parent 67d6b55 commit 5ce3e76
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
36 changes: 35 additions & 1 deletion src/Console/CommandRunner.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Console\Exception\StopException;
use Cake\Console\Shell;
use Cake\Http\BaseApplication;
use Cake\Shell\VersionShell;
use RuntimeException;

/**
Expand All @@ -40,6 +41,13 @@ class CommandRunner
*/
protected $root;

/**
* Alias mappings.
*
* @var array
*/
protected $aliases = [];

/**
* Constructor
*
Expand All @@ -50,6 +58,26 @@ public function __construct(BaseApplication $app, $root = 'cake')
{
$this->app = $app;
$this->root = $root;
$this->aliases = [
'--version' => 'version'
];
}

/**
* Replace the alias map for a runner.
*
* Aliases allow you to define alternate names for commands
* in the collection. This can be useful to add top level switches
* like `--version` or `-h`
*
* @param array $aliases The map of aliases to replace.
* @return $this
*/
public function setAliases(array $aliases)
{
$this->aliases = $aliases;

return $this;
}

/**
Expand All @@ -64,7 +92,10 @@ public function run(array $argv, ConsoleIo $io = null)
{
$this->app->bootstrap();

$commands = $this->app->console(new CommandCollection());
$commands = new CommandCollection([
'version' => VersionShell::class,
]);
$commands = $this->app->console($commands);
if (!($commands instanceof CommandCollection)) {
$type = is_object($commands) ? get_class($commands) : gettype($commands);
throw new RuntimeException(
Expand Down Expand Up @@ -111,6 +142,9 @@ public function run(array $argv, ConsoleIo $io = null)
*/
protected function getShell(ConsoleIo $io, CommandCollection $commands, $name)
{
if (isset($this->aliases[$name])) {
$name = $this->aliases[$name];
}
if (!$commands->has($name)) {
throw new RuntimeException(
"Unknown command `{$this->root} {$name}`." .
Expand Down
35 changes: 35 additions & 0 deletions src/Shell/VersionShell.php
@@ -0,0 +1,35 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.5.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace Cake\Shell;

use Cake\Console\Shell;
use Cake\Core\Configure;

/**
* Print out the version of CakePHP in use.
*/
class VersionShell extends Shell
{
/**
* Print out the version of CakePHP in use.
*
* @return void
*/
public function main()
{
$this->out(Configure::version());
}
}
20 changes: 9 additions & 11 deletions tests/TestCase/Console/CommandRunnerTest.php
Expand Up @@ -138,19 +138,17 @@ public function testRunHelpShortOption()
*
* @return void
*/
public function testRunVersionLongOption()
public function testRunVersionAlias()
{
$this->markTestIncomplete();
}
$app = $this->getMockBuilder(BaseApplication::class)
->setMethods(['middleware', 'bootstrap'])
->setConstructorArgs([$this->config])
->getMock();

/**
* Test using `cake -v` invokes the version command
*
* @return void
*/
public function testRunVersionShortOption()
{
$this->markTestIncomplete();
$output = new ConsoleOutput();
$runner = new CommandRunner($app, 'cake');
$result = $runner->run(['cake', '--version'], $this->getMockIo($output));
$this->assertContains(Configure::version(), $output->messages()[0]);
}

/**
Expand Down

0 comments on commit 5ce3e76

Please sign in to comment.