2014-04-23 10:51:50 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Task collection is used as a registry for loaded tasks and handles loading
|
|
|
|
* and constructing task class objects.
|
|
|
|
*
|
2018-03-18 09:24:15 +08:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2014-04-23 10:51:50 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2018-03-18 09:24:15 +08:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2014-04-23 10:51:50 +08:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2018-03-18 09:24:15 +08:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2014-04-23 10:51:50 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
App::uses('ObjectCollection', 'Utility');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collection object for Tasks. Provides features
|
|
|
|
* for lazily loading tasks, and firing callbacks on loaded tasks.
|
|
|
|
*
|
|
|
|
* @package Cake.Console
|
|
|
|
*/
|
|
|
|
class TaskCollection extends ObjectCollection {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shell to use to set params to tasks.
|
|
|
|
*
|
|
|
|
* @var Shell
|
|
|
|
*/
|
|
|
|
protected $_Shell;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The directory inside each shell path that contains tasks.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $taskPathPrefix = 'tasks/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2016-02-25 20:25:24 +08:00
|
|
|
* @param Shell $Shell The shell this task collection is attached to.
|
2014-04-23 10:51:50 +08:00
|
|
|
*/
|
|
|
|
public function __construct(Shell $Shell) {
|
|
|
|
$this->_Shell = $Shell;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.,
|
2016-02-25 20:25:24 +08:00
|
|
|
* ```
|
2014-04-23 10:51:50 +08:00
|
|
|
* public $tasks = array(
|
|
|
|
* 'DbConfig' => array(
|
|
|
|
* 'className' => 'Bakeplus.DbConfigure'
|
|
|
|
* );
|
|
|
|
* );
|
2016-02-25 20:25:24 +08:00
|
|
|
* ```
|
2014-04-23 10:51:50 +08:00
|
|
|
* 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.
|
2018-03-18 09:24:15 +08:00
|
|
|
* @return AppShell A task object, Either the existing loaded task or a new one.
|
2014-04-23 10:51:50 +08:00
|
|
|
* @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($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,
|
|
|
|
'plugin' => substr($plugin, 0, -1)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_loaded[$alias] = new $taskClass(
|
|
|
|
$this->_Shell->stdout, $this->_Shell->stderr, $this->_Shell->stdin
|
|
|
|
);
|
|
|
|
return $this->_loaded[$alias];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|