Permalink
Browse files

Added Aliasing of Tasks in the TaskCollection() class and added tests

  • Loading branch information...
1 parent 6b0ea6a commit 2c4e9dc8f63d275f32ad5e5a2a50475ef71e2d46 @angelxmoreno angelxmoreno committed May 3, 2013
Showing with 50 additions and 8 deletions.
  1. +24 −8 lib/Cake/Console/TaskCollection.php
  2. +26 −0 lib/Cake/Test/Case/Console/TaskCollectionTest.php
View
32 lib/Cake/Console/TaskCollection.php
@@ -50,35 +50,51 @@ public function __construct(Shell $Shell) {
}
/**
- * Loads/constructs a task. Will return the instance in the collection
- * if it already exists.
+ * Loads/constructs a task. Will return the instance in the registry if it already exists.
+ *
+ * You can alias your task as an existing task by setting the 'className' key, i.e.,
+ * {{{
+ * public $tasks = array(
+ * 'DbConfig' => array(
+ * 'className' => 'Bakeplus.DbConfigure'
+ * );
+ * );
+ * }}}
+ * All calls to the `DbConfig` task would use `DbConfigure` found in the `Bakeplus` plugin instead.
*
* @param string $task Task name to load
* @param array $settings Settings for the task.
* @return Task A task object, Either the existing loaded task or a new one.
* @throws MissingTaskException when the task could not be found
*/
public function load($task, $settings = array()) {
+ if (is_array($settings) && isset($settings['className'])) {
+ $alias = $task;
+ $task = $settings['className'];
+ }
list($plugin, $name) = pluginSplit($task, true);
-
- if (isset($this->_loaded[$name])) {
- return $this->_loaded[$name];
+ if (!isset($alias)) {
+ $alias = $name;
}
+ if (isset($this->_loaded[$alias])) {
+ return $this->_loaded[$alias];
+ }
$taskClass = $name . 'Task';
App::uses($taskClass, $plugin . 'Console/Command/Task');
$exists = class_exists($taskClass);
if (!$exists) {
throw new MissingTaskException(array(
- 'class' => $taskClass
+ 'class' => $taskClass,
+ 'plugin' => substr($plugin, 0, -1)
));
}
- $this->_loaded[$name] = new $taskClass(
+ $this->_loaded[$alias] = new $taskClass(
$this->_Shell->stdout, $this->_Shell->stderr, $this->_Shell->stdin
);
- return $this->_loaded[$name];
+ return $this->_loaded[$alias];
}
}
View
26 lib/Cake/Test/Case/Console/TaskCollectionTest.php
@@ -20,6 +20,11 @@
App::uses('TaskCollection', 'Console');
App::uses('Shell', 'Console');
+/**
+ * Extended Task
+ */
+class DbConfigAliasedTask extends Shell {
+}
class TaskCollectionTest extends CakeTestCase {
@@ -122,4 +127,25 @@ public function testUnload() {
$this->assertEquals(array('Extract'), $result, 'loaded tasks is wrong');
}
+/**
+ * Tests loading as an alias
+ *
+ * @return void
+ */
+ public function testLoadWithAlias() {
+ $result = $this->Tasks->load('DbConfig', array('className' => 'DbConfigAliased'));
+ $this->assertInstanceOf('DbConfigAliasedTask', $result);
+ $this->assertInstanceOf('DbConfigAliasedTask', $this->Tasks->DbConfig);
+
+ $result = $this->Tasks->loaded();
+ $this->assertEquals(array('DbConfig'), $result, 'loaded() results are wrong.');
+
+ $result = $this->Tasks->load('SomeTask', array('className' => 'TestPlugin.OtherTask'));
+ $this->assertInstanceOf('OtherTaskTask', $result);
+ $this->assertInstanceOf('OtherTaskTask', $this->Tasks->SomeTask);
+
+ $result = $this->Tasks->loaded();
+ $this->assertEquals(array('DbConfig', 'SomeTask'), $result, 'loaded() results are wrong.');
+ }
+
}

0 comments on commit 2c4e9dc

Please sign in to comment.