Browse Source

Initial implementation of ShellDispatcher::alias().

mark_story 12 years ago
parent
commit
6d39aa80ab
2 changed files with 43 additions and 4 deletions
  1. 27 0
      src/Console/ShellDispatcher.php
  2. 16 4
      tests/TestCase/Console/ShellDispatcherTest.php

+ 27 - 0
src/Console/ShellDispatcher.php

@@ -35,6 +35,13 @@ class ShellDispatcher {
 	public $args = [];
 
 /**
+ * List of connected aliases.
+ *
+ * @var array
+ */
+	protected static $_aliases = [];
+
+/**
  * Constructor
  *
  * The execution of the script is stopped after dispatching the request with
@@ -53,6 +60,23 @@ class ShellDispatcher {
 	}
 
 /**
+ * Add an alias for a shell command.
+ *
+ * Aliases allow you to call shells by alternate names. This is most
+ * useful when dealing with plugin shells that you want to have shorter
+ * names for.
+ *
+ * If you re-use an alias the last alias set will be the one available.
+ *
+ * @param string $short The new short name for the shell.
+ * @param string $original The original full name for the shell.
+ * @return void
+ */
+	public static function alias($short, $original) {
+		static::$_aliases[$short] = $original;
+	}
+
+/**
  * Run the dispatcher
  *
  * @param array $argv The argv from PHP
@@ -165,6 +189,9 @@ class ShellDispatcher {
  * @throws \Cake\Console\Error\MissingShellException when errors are encountered.
  */
 	protected function _getShell($shell) {
+		if (isset(static::$_aliases[$shell])) {
+			$shell = static::$_aliases[$shell];
+		}
 		list($plugin, $shell) = pluginSplit($shell);
 
 		$plugin = Inflector::camelize($plugin);

+ 16 - 4
tests/TestCase/Console/ShellDispatcherTest.php

@@ -112,6 +112,7 @@ class ShellDispatcherTest extends TestCase {
 	public function setUp() {
 		parent::setUp();
 		Plugin::load('TestPlugin');
+		Configure::write('App.namespace', 'TestApp');
 	}
 
 /**
@@ -120,10 +121,6 @@ class ShellDispatcherTest extends TestCase {
  * @return void
  */
 	public function testGetShell() {
-		$this->skipIf(class_exists('SampleShell'), 'SampleShell Class already loaded.');
-		$this->skipIf(class_exists('ExampleShell'), 'ExampleShell Class already loaded.');
-
-		Configure::write('App.namespace', 'TestApp');
 		$Dispatcher = new TestShellDispatcher();
 
 		$result = $Dispatcher->getShell('sample');
@@ -141,6 +138,21 @@ class ShellDispatcherTest extends TestCase {
 	}
 
 /**
+ * Test getting shells with aliases.
+ *
+ * @return void
+ */
+	public function testGetShellAliased() {
+		TestShellDispatcher::alias('short', 'test_plugin.example');
+
+		$dispatcher = new TestShellDispatcher();
+		$result = $dispatcher->getShell('short');
+		$this->assertInstanceOf('TestPlugin\Console\Command\ExampleShell', $result);
+		$this->assertEquals('TestPlugin', $result->plugin);
+		$this->assertEquals('Example', $result->name);
+	}
+
+/**
  * Verify correct dispatch of Shell subclasses with a main method
  *
  * @return void