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 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
8: *
9: * Licensed under The MIT License
10: * Redistributions of files must retain the above copyright notice.
11: *
12: * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
13: * @link http://cakephp.org CakePHP(tm) Project
14: * @since CakePHP(tm) v 2.0
15: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
16: */
17:
18: App::uses('ObjectCollection', 'Utility');
19:
20: /**
21: * Collection object for Tasks. Provides features
22: * for lazily loading tasks, and firing callbacks on loaded tasks.
23: *
24: * @package Cake.Console
25: */
26: class TaskCollection extends ObjectCollection {
27: /**
28: * Shell to use to set params to tasks.
29: *
30: * @var Shell
31: */
32: protected $_Shell;
33:
34: /**
35: * The directory inside each shell path that contains tasks.
36: *
37: * @var string
38: */
39: public $taskPathPrefix = 'tasks/';
40:
41: /**
42: * Constructor
43: *
44: * @param Shell $Shell
45: */
46: public function __construct(Shell $Shell) {
47: $this->_Shell = $Shell;
48: }
49:
50: /**
51: * Loads/constructs a task. Will return the instance in the collection
52: * if it already exists.
53: *
54: * @param string $task Task name to load
55: * @param array $settings Settings for the task.
56: * @return Task A task object, Either the existing loaded task or a new one.
57: * @throws MissingTaskException when the task could not be found
58: */
59: public function load($task, $settings = array()) {
60: list($plugin, $name) = pluginSplit($task, true);
61:
62: if (isset($this->_loaded[$name])) {
63: return $this->_loaded[$name];
64: }
65: $taskClass = $name . 'Task';
66: App::uses($taskClass, $plugin . 'Console/Command/Task');
67: if (!class_exists($taskClass)) {
68: if (!class_exists($taskClass)) {
69: throw new MissingTaskException(array(
70: 'class' => $taskClass
71: ));
72: }
73: }
74:
75: $this->_loaded[$name] = new $taskClass(
76: $this->_Shell->stdout, $this->_Shell->stderr, $this->_Shell->stdin
77: );
78: $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
79: if ($enable === true) {
80: $this->_enabled[] = $name;
81: }
82: return $this->_loaded[$name];
83: }
84:
85: }
86: