Skip to content

Commit

Permalink
Merge pull request #1457 from markstory/3.0-task-collection
Browse files Browse the repository at this point in the history
3.0 task collection & refactoring
  • Loading branch information
lorenzo committed Jul 29, 2013
2 parents 1256624 + 999aef9 commit 84b107b
Show file tree
Hide file tree
Showing 52 changed files with 692 additions and 515 deletions.
6 changes: 3 additions & 3 deletions lib/Cake/Console/Command/AclShell.php
Expand Up @@ -19,7 +19,7 @@
namespace Cake\Console\Command;

use Cake\Console\Shell;
use Cake\Controller\ComponentCollection;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Component\AclComponent;
use Cake\Controller\Controller;
use Cake\Core\App;
Expand Down Expand Up @@ -97,8 +97,8 @@ public function startup() {
}

if (!in_array($this->command, array('initdb'))) {
$collection = new ComponentCollection();
$this->Acl = new AclComponent($collection);
$registry = new ComponentRegistry();
$this->Acl = new AclComponent($registry);
$controller = new Controller();
$this->Acl->startup($controller);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Console/Command/Task/TestTask.php
Expand Up @@ -522,8 +522,8 @@ public function generateConstructor($type, $fullClassName, $plugin) {
$construct = "new {$className}(\$View);\n";
}
if ($type === 'component') {
$pre = "\$Collection = new ComponentCollection();\n";
$construct = "new {$className}(\$Collection);\n";
$pre = "\$registry = new ComponentRegistry();\n";
$construct = "new {$className}(\$registry);\n";
}
return array($pre, $construct, $post);
}
Expand All @@ -540,7 +540,7 @@ public function generateUses($type, $realType, $fullClassName) {
$uses = array();
$type = strtolower($type);
if ($type == 'component') {
$uses[] = 'Cake\Controller\ComponentCollection';
$uses[] = 'Cake\Controller\ComponentRegistry';
}
if ($type == 'helper') {
$uses[] = 'Cake\View\View';
Expand Down
48 changes: 48 additions & 0 deletions lib/Cake/Console/Command/UpgradeShell.php
Expand Up @@ -409,6 +409,50 @@ protected function _processFixture($content) {
return $content;
}

/**
* Rename collection classes
*
* @return void
*/
public function rename_collections() {
$path = $this->_getPath();

$Folder = new Folder($path);
$this->_paths = $Folder->tree(null, false, 'dir');
$this->_findFiles('php');
foreach ($this->_files as $filePath) {
$patterns = [
[
' Replace $this->_Collection with $this->_registry',
'#\$this->_Collection#',
'$this->_registry',
],
[
' Replace ComponentCollection arguments',
'#ComponentCollection\s+\$collection#',
'ComponentRegistry $registry',
],
[
' Rename ComponentCollection',
'#ComponentCollection#',
"ComponentRegistry",
],
[
' Rename HelperCollection',
'#HelperCollection#',
"HelperRegistry",
],
[
' Rename TaskCollection',
'#TaskCollection#',
"TaskRegistry",
],
];
$this->_updateFile($filePath, $patterns);
}
$this->out(__d('cake_console', '<success>Collection class uses rename successfully</success>'));
}

/**
* Filter paths to remove webroot, Plugin, tmp directories
*
Expand Down Expand Up @@ -595,6 +639,10 @@ public function getOptionParser() {
'help' => __d('cake_console', 'Update fixtures to use new index/constraint features. This is necessary before running tests.'),
'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')],
])
->addSubcommand('rename_collections', [
'help' => __d('cake_console', "Rename HelperCollection, ComponentCollection, and TaskCollection."),
'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')]
])
->addSubcommand('cache_config', [
'help' => __d('cake_console', "Replace Cache::config() with Configure."),
'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')]
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Console/Shell.php
Expand Up @@ -137,7 +137,7 @@ class Shell extends Object {
/**
* Task Collection for the command, used to create Tasks.
*
* @var TaskCollection
* @var TaskRegistry
*/
public $Tasks;

Expand Down Expand Up @@ -182,7 +182,7 @@ public function __construct($stdout = null, $stderr = null, $stdin = null) {
list(, $class) = namespaceSplit(get_class($this));
$this->name = str_replace(array('Shell', 'Task'), '', $class);
}
$this->Tasks = new TaskCollection($this);
$this->Tasks = new TaskRegistry($this);

$this->stdout = $stdout ? $stdout : new ConsoleOutput('php://stdout');
$this->stderr = $stderr ? $stderr : new ConsoleOutput('php://stderr');
Expand Down Expand Up @@ -314,7 +314,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 = $this->Tasks->normalizeArray((array)$this->tasks);
$this->taskNames = array_merge($this->taskNames, array_keys($this->_taskMap));
return true;
}
Expand Down
100 changes: 0 additions & 100 deletions lib/Cake/Console/TaskCollection.php

This file was deleted.

88 changes: 88 additions & 0 deletions lib/Cake/Console/TaskRegistry.php
@@ -0,0 +1,88 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Console;

use Cake\Core\App;
use Cake\Error;
use Cake\Utility\ObjectRegistry;

/**
* Registry for Tasks. Provides features
* for lazily loading tasks.
*/
class TaskRegistry extends ObjectRegistry {

/**
* Shell to use to set params to tasks.
*
* @var Shell
*/
protected $_Shell;

/**
* Constructor
*
* @param Shell $Shell
*/
public function __construct(Shell $Shell) {
$this->_Shell = $Shell;
}

/**
* Resolve a task classname.
*
* Part of the template method for Cake\Utility\ObjectRegistry::load()
*
* @param string $class Partial classname to resolve.
* @return string|false Either the correct classname or false.
*/
protected function _resolveClassName($class) {
return App::classname($class, 'Console/Command/Task', 'Task');
}

/**
* Throws an exception when a task is missing.
*
* Part of the template method for Cake\Utility\ObjectRegistry::load()
*
* @param string $class The classname that is missing.
* @param string $plugin The plugin the task is missing in.
* @throws Cake\Error\MissingTaskException
*/
protected function _throwMissingClassError($class, $plugin) {
throw new Error\MissingTaskException([
'class' => $class,
'plugin' => $plugin
]);
}

/**
* Create the task instance.
*
* Part of the template method for Cake\Utility\ObjectRegistry::load()
*
* @param string $class The classname that is missing.
* @param array $settings An array of settings to use for the task.
* @return Component The constructed task class.
*/
protected function _create($class, $settings) {
return new $class(
$this->_Shell->stdout,
$this->_Shell->stderr,
$this->_Shell->stdin
);
}

}
17 changes: 8 additions & 9 deletions lib/Cake/Controller/Component.php
Expand Up @@ -17,7 +17,6 @@
use Cake\Core\Object;
use Cake\Event\Event;
use Cake\Event\EventListener;
use Cake\Utility\ObjectCollection;

/**
* Base class for an individual Component. Components provide reusable bits of
Expand Down Expand Up @@ -65,11 +64,11 @@
class Component extends Object implements EventListener {

/**
* Component collection class used to lazy load components.
* Component registry class used to lazy load components.
*
* @var ComponentCollection
* @var ComponentRegistry
*/
protected $_Collection;
protected $_registry;

/**
* Settings for this Component
Expand All @@ -95,15 +94,15 @@ class Component extends Object implements EventListener {
/**
* Constructor
*
* @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
* @param ComponentRegistry $registry A ComponentRegistry this component can use to lazy load its components
* @param array $settings Array of configuration settings.
*/
public function __construct(ComponentCollection $collection, $settings = array()) {
$this->_Collection = $collection;
public function __construct(ComponentRegistry $registry, $settings = []) {
$this->_registry = $registry;
$this->settings = $settings;
$this->_set($settings);
if (!empty($this->components)) {
$this->_componentMap = ObjectCollection::normalizeObjectArray($this->components);
$this->_componentMap = $registry->normalizeArray($this->components);
}
}

Expand All @@ -116,7 +115,7 @@ public function __construct(ComponentCollection $collection, $settings = array()
public function __get($name) {
if (isset($this->_componentMap[$name]) && !isset($this->{$name})) {
$settings = array_merge((array)$this->_componentMap[$name]['settings'], array('enabled' => false));
$this->{$name} = $this->_Collection->load($this->_componentMap[$name]['class'], $settings);
$this->{$name} = $this->_registry->load($this->_componentMap[$name]['class'], $settings);
}
if (isset($this->{$name})) {
return $this->{$name};
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Controller/Component/AclComponent.php
Expand Up @@ -16,7 +16,7 @@

use Cake\Configure\IniReader;
use Cake\Controller\Component;
use Cake\Controller\ComponentCollection;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Component\Acl\AclInterface;
use Cake\Core\App;
use Cake\Core\Configure;
Expand Down Expand Up @@ -61,11 +61,11 @@ class AclComponent extends Component {
/**
* Constructor. Will return an instance of the correct ACL class as defined in `Configure::read('Acl.classname')`
*
* @param ComponentCollection $collection
* @param ComponentRegistry $collection
* @param array $settings
* @throws Cake\Error\Exception when Acl.classname could not be loaded.
*/
public function __construct(ComponentCollection $collection, $settings = array()) {
public function __construct(ComponentRegistry $collection, $settings = array()) {
parent::__construct($collection, $settings);
$classname = $name = Configure::read('Acl.classname');
if (!class_exists($classname)) {
Expand Down

0 comments on commit 84b107b

Please sign in to comment.