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
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 collection
54: * if it already exists.
55: *
56: * @param string $task Task name to load
57: * @param array $settings Settings for the task.
58: * @return Task A task object, Either the existing loaded task or a new one.
59: * @throws MissingTaskException when the task could not be found
60: */
61: public function load($task, $settings = array()) {
62: list($plugin, $name) = pluginSplit($task, true);
63:
64: if (isset($this->_loaded[$name])) {
65: return $this->_loaded[$name];
66: }
67:
68: $taskClass = $name . 'Task';
69: App::uses($taskClass, $plugin . 'Console/Command/Task');
70:
71: $exists = class_exists($taskClass);
72: if (!$exists) {
73: throw new MissingTaskException(array(
74: 'class' => $taskClass
75: ));
76: }
77:
78: $this->_loaded[$name] = new $taskClass(
79: $this->_Shell->stdout, $this->_Shell->stderr, $this->_Shell->stdin
80: );
81: return $this->_loaded[$name];
82: }
83:
84: }
85: