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-2012, 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-2012, 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: /**
29: * Shell to use to set params to tasks.
30: *
31: * @var Shell
32: */
33: protected $_Shell;
34:
35: /**
36: * The directory inside each shell path that contains tasks.
37: *
38: * @var string
39: */
40: public $taskPathPrefix = 'tasks/';
41:
42: /**
43: * Constructor
44: *
45: * @param Shell $Shell
46: */
47: public function __construct(Shell $Shell) {
48: $this->_Shell = $Shell;
49: }
50:
51: /**
52: * Loads/constructs a task. Will return the instance in the collection
53: * if it already exists.
54: *
55: * @param string $task Task name to load
56: * @param array $settings Settings for the task.
57: * @return Task A task object, Either the existing loaded task or a new one.
58: * @throws MissingTaskException when the task could not be found
59: */
60: public function load($task, $settings = array()) {
61: list($plugin, $name) = pluginSplit($task, true);
62:
63: if (isset($this->_loaded[$name])) {
64: return $this->_loaded[$name];
65: }
66: $taskClass = $name . 'Task';
67: App::uses($taskClass, $plugin . 'Console/Command/Task');
68: if (!class_exists($taskClass)) {
69: if (!class_exists($taskClass)) {
70: throw new MissingTaskException(array(
71: 'class' => $taskClass
72: ));
73: }
74: }
75:
76: $this->_loaded[$name] = new $taskClass(
77: $this->_Shell->stdout, $this->_Shell->stderr, $this->_Shell->stdin
78: );
79: return $this->_loaded[$name];
80: }
81:
82: }
83: