Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Break inheritance with ObjectCollection
Make TaskCollection not inherit from ObjectCollection. This removes
methods that TaskCollection never needs and makes TaskCollection work
more like the new style factory/registry classes. More duplication has
been introduced but will be removed in future work.
  • Loading branch information
markstory committed Jul 27, 2013
1 parent f7fb12d commit e490d20
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
3 changes: 2 additions & 1 deletion lib/Cake/Console/Shell.php
Expand Up @@ -29,6 +29,7 @@
use Cake\Utility\File;
use Cake\Utility\Inflector;
use Cake\Utility\MergeVariablesTrait;
use Cake\Utility\ObjectCollection;
use Cake\Utility\String;

/**
Expand Down Expand Up @@ -314,7 +315,7 @@ public function loadTasks() {
if ($this->tasks === true || empty($this->tasks) || empty($this->Tasks)) {
return true;
}
$this->_taskMap = TaskCollection::normalizeObjectArray((array)$this->tasks);
$this->_taskMap = ObjectCollection::normalizeObjectArray((array)$this->tasks);
$this->taskNames = array_merge($this->taskNames, array_keys($this->_taskMap));
return true;
}
Expand Down
50 changes: 44 additions & 6 deletions lib/Cake/Console/TaskCollection.php
@@ -1,8 +1,5 @@
<?php
/**
* Task collection is used as a registry for loaded tasks and handles loading
* and constructing task class objects.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -23,11 +20,16 @@

/**
* Collection object for Tasks. Provides features
* for lazily loading tasks, and firing callbacks on loaded tasks.
* for lazily loading tasks.
*/
class TaskCollection {

/**
* Map of loaded tasks.
*
* @package Cake.Console
* @var array
*/
class TaskCollection extends ObjectCollection {
protected $_loaded = [];

/**
* Shell to use to set params to tasks.
Expand Down Expand Up @@ -97,4 +99,40 @@ public function load($task, $settings = array()) {
return $this->_loaded[$alias];
}

/**
* Get the loaded helpers list, or get the helper instance at a given name.
*
* @param null|string $name The helper name to get or null.
* @return array|Helper Either a list of helper names, or a loaded helper.
*/
public function loaded($name = null) {
if (!empty($name)) {
return isset($this->_loaded[$name]);
}
return array_keys($this->_loaded);
}

/**
* Provide public read access to the loaded objects
*
* @param string $name Name of property to read
* @return mixed
*/
public function __get($name) {
if (isset($this->_loaded[$name])) {
return $this->_loaded[$name];
}
return null;
}

/**
* Provide isset access to _loaded
*
* @param string $name Name of object being checked.
* @return boolean
*/
public function __isset($name) {
return isset($this->_loaded[$name]);
}

}
42 changes: 1 addition & 41 deletions lib/Cake/Test/TestCase/Console/TaskCollectionTest.php
@@ -1,9 +1,5 @@
<?php
/**
* TaskCollectionTest file
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -13,7 +9,6 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Console
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
Expand All @@ -27,7 +22,6 @@
/**
* Class TaskCollectionTest
*
* @package Cake.Test.Case.Console
*/
class TaskCollectionTest extends TestCase {

Expand All @@ -39,8 +33,7 @@ class TaskCollectionTest extends TestCase {
public function setUp() {
parent::setUp();
$shell = $this->getMock('Cake\Console\Shell', array(), array(), '', false);
$dispatcher = $this->getMock('Cake\Console\ShellDispatcher', array(), array(), '', false);
$this->Tasks = new TaskCollection($shell, $dispatcher);
$this->Tasks = new TaskCollection($shell);
}

/**
Expand All @@ -67,19 +60,6 @@ public function testLoad() {
$this->assertEquals(array('DbConfig'), $result, 'loaded() results are wrong.');
}

/**
* test load and enable = false
*
* @return void
*/
public function testLoadWithEnableFalse() {
$result = $this->Tasks->load('DbConfig', array('enabled' => false));
$this->assertInstanceOf('Cake\Console\Command\Task\DbConfigTask', $result);
$this->assertInstanceOf('Cake\Console\Command\Task\DbConfigTask', $this->Tasks->DbConfig);

$this->assertFalse($this->Tasks->enabled('DbConfig'), 'DbConfigTask should be disabled');
}

/**
* test missingtask exception
*
Expand Down Expand Up @@ -110,26 +90,6 @@ public function testLoadPluginTask() {
Plugin::unload();
}

/**
* test unload()
*
* @return void
*/
public function testUnload() {
$this->Tasks->load('Extract');
$this->Tasks->load('DbConfig');

$result = $this->Tasks->loaded();
$this->assertEquals(array('Extract', 'DbConfig'), $result, 'loaded tasks is wrong');

$this->Tasks->unload('DbConfig');
$this->assertFalse(isset($this->Tasks->DbConfig));
$this->assertTrue(isset($this->Tasks->Extract));

$result = $this->Tasks->loaded();
$this->assertEquals(array('Extract'), $result, 'loaded tasks is wrong');
}

/**
* Tests loading as an alias
*
Expand Down

0 comments on commit e490d20

Please sign in to comment.