2010-07-07 03:30:24 +00:00
|
|
|
<?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)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-07 03:30:24 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-07 03:30:24 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2011-02-14 04:12:41 +00:00
|
|
|
|
2010-12-07 05:56:10 +00:00
|
|
|
App::uses('ObjectCollection', 'Utility');
|
2010-07-07 03:30:24 +00:00
|
|
|
|
2011-07-26 01:46:52 +00:00
|
|
|
/**
|
|
|
|
* Collection object for Tasks. Provides features
|
|
|
|
* for lazily loading tasks, and firing callbacks on loaded tasks.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console
|
2011-07-26 01:46:52 +00:00
|
|
|
*/
|
2010-07-07 03:30:24 +00:00
|
|
|
class TaskCollection extends ObjectCollection {
|
2012-03-03 23:55:07 +00:00
|
|
|
|
2010-07-07 03:30:24 +00:00
|
|
|
/**
|
2010-10-24 18:55:16 +00:00
|
|
|
* Shell to use to set params to tasks.
|
2010-07-07 03:30:24 +00:00
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @var Shell
|
2010-07-07 03:30:24 +00:00
|
|
|
*/
|
2010-10-04 04:23:54 +00:00
|
|
|
protected $_Shell;
|
2010-07-07 03:30:24 +00:00
|
|
|
|
2010-10-17 19:58:44 +00:00
|
|
|
/**
|
|
|
|
* The directory inside each shell path that contains tasks.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $taskPathPrefix = 'tasks/';
|
|
|
|
|
2010-07-07 03:30:24 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param Shell $Shell
|
2010-07-07 03:30:24 +00:00
|
|
|
*/
|
2010-10-24 18:55:16 +00:00
|
|
|
public function __construct(Shell $Shell) {
|
2010-10-04 04:23:54 +00:00
|
|
|
$this->_Shell = $Shell;
|
2010-07-07 03:30:24 +00:00
|
|
|
}
|
2010-10-24 18:55:16 +00:00
|
|
|
|
2010-07-07 03:30:24 +00:00
|
|
|
/**
|
2010-10-17 19:07:00 +00:00
|
|
|
* Loads/constructs a task. Will return the instance in the collection
|
|
|
|
* if it already exists.
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
2010-07-07 03:30:24 +00:00
|
|
|
* @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.
|
2011-10-15 18:06:31 +00:00
|
|
|
* @throws MissingTaskException when the task could not be found
|
2010-07-07 03:30:24 +00:00
|
|
|
*/
|
2011-01-19 00:33:05 +00:00
|
|
|
public function load($task, $settings = array()) {
|
2010-07-07 03:30:24 +00:00
|
|
|
list($plugin, $name) = pluginSplit($task, true);
|
|
|
|
|
|
|
|
if (isset($this->_loaded[$name])) {
|
|
|
|
return $this->_loaded[$name];
|
|
|
|
}
|
|
|
|
$taskClass = $name . 'Task';
|
2011-03-07 05:15:00 +00:00
|
|
|
App::uses($taskClass, $plugin . 'Console/Command/Task');
|
2010-07-07 03:30:24 +00:00
|
|
|
if (!class_exists($taskClass)) {
|
|
|
|
if (!class_exists($taskClass)) {
|
2011-10-15 18:06:31 +00:00
|
|
|
throw new MissingTaskException(array(
|
|
|
|
'class' => $taskClass
|
|
|
|
));
|
2010-07-07 03:30:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-06 04:15:51 +00:00
|
|
|
$this->_loaded[$name] = new $taskClass(
|
2010-10-24 18:55:16 +00:00
|
|
|
$this->_Shell->stdout, $this->_Shell->stderr, $this->_Shell->stdin
|
2010-10-06 04:15:51 +00:00
|
|
|
);
|
2010-07-07 03:30:24 +00:00
|
|
|
return $this->_loaded[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|