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