1: <?php
2: /**
3: * Task collection is used as a registry for loaded tasks and handles loading
4: * and constructing task class objects.
5: *
6: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
7: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
8: *
9: * Licensed under The MIT License
10: * For full copyright and license information, please see the LICENSE.txt
11: * Redistributions of files must retain the above copyright notice.
12: *
13: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
14: * @link http://cakephp.org CakePHP(tm) Project
15: * @since CakePHP(tm) v 2.0
16: * @license http://www.opensource.org/licenses/mit-license.php MIT License
17: */
18:
19: App::uses('ObjectCollection', 'Utility');
20:
21: /**
22: * Collection object for Tasks. Provides features
23: * for lazily loading tasks, and firing callbacks on loaded tasks.
24: *
25: * @package Cake.Console
26: */
27: class TaskCollection extends ObjectCollection {
28:
29: /**
30: * Shell to use to set params to tasks.
31: *
32: * @var Shell
33: */
34: protected $_Shell;
35:
36: /**
37: * The directory inside each shell path that contains tasks.
38: *
39: * @var string
40: */
41: public $taskPathPrefix = 'tasks/';
42:
43: /**
44: * Constructor
45: *
46: * @param Shell $Shell The shell this task collection is attached to.
47: */
48: public function __construct(Shell $Shell) {
49: $this->_Shell = $Shell;
50: }
51:
52: /**
53: * Loads/constructs a task. Will return the instance in the registry if it already exists.
54: *
55: * You can alias your task as an existing task by setting the 'className' key, i.e.,
56: * ```
57: * public $tasks = array(
58: * 'DbConfig' => array(
59: * 'className' => 'Bakeplus.DbConfigure'
60: * );
61: * );
62: * ```
63: * All calls to the `DbConfig` task would use `DbConfigure` found in the `Bakeplus` plugin instead.
64: *
65: * @param string $task Task name to load
66: * @param array $settings Settings for the task.
67: * @return AppShell A task object, Either the existing loaded task or a new one.
68: * @throws MissingTaskException when the task could not be found
69: */
70: public function load($task, $settings = array()) {
71: if (is_array($settings) && isset($settings['className'])) {
72: $alias = $task;
73: $task = $settings['className'];
74: }
75: list($plugin, $name) = pluginSplit($task, true);
76: if (!isset($alias)) {
77: $alias = $name;
78: }
79:
80: if (isset($this->_loaded[$alias])) {
81: return $this->_loaded[$alias];
82: }
83: $taskClass = $name . 'Task';
84: App::uses($taskClass, $plugin . 'Console/Command/Task');
85:
86: $exists = class_exists($taskClass);
87: if (!$exists) {
88: throw new MissingTaskException(array(
89: 'class' => $taskClass,
90: 'plugin' => substr($plugin, 0, -1)
91: ));
92: }
93:
94: $this->_loaded[$alias] = new $taskClass(
95: $this->_Shell->stdout, $this->_Shell->stderr, $this->_Shell->stdin
96: );
97: return $this->_loaded[$alias];
98: }
99:
100: }
101: