TaskCollection.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. App::uses('ObjectCollection', 'Utility');
  18. /**
  19. * Collection object for Tasks. Provides features
  20. * for lazily loading tasks, and firing callbacks on loaded tasks.
  21. *
  22. * @package Cake.Console
  23. */
  24. class TaskCollection extends ObjectCollection {
  25. /**
  26. * Shell to use to set params to tasks.
  27. *
  28. * @var Shell
  29. */
  30. protected $_Shell;
  31. /**
  32. * The directory inside each shell path that contains tasks.
  33. *
  34. * @var string
  35. */
  36. public $taskPathPrefix = 'tasks/';
  37. /**
  38. * Constructor
  39. *
  40. * @param Shell $Shell
  41. */
  42. public function __construct(Shell $Shell) {
  43. $this->_Shell = $Shell;
  44. }
  45. /**
  46. * Loads/constructs a task. Will return the instance in the collection
  47. * if it already exists.
  48. *
  49. * @param string $task Task name to load
  50. * @param array $settings Settings for the task.
  51. * @return Task A task object, Either the existing loaded task or a new one.
  52. * @throws MissingTaskException when the task could not be found
  53. */
  54. public function load($task, $settings = array()) {
  55. list($plugin, $name) = pluginSplit($task, true);
  56. if (isset($this->_loaded[$name])) {
  57. return $this->_loaded[$name];
  58. }
  59. $taskClass = $name . 'Task';
  60. App::uses($taskClass, $plugin . 'Console/Command/Task');
  61. $exists = class_exists($taskClass);
  62. if (!$exists) {
  63. throw new MissingTaskException(array(
  64. 'class' => $taskClass
  65. ));
  66. }
  67. $this->_loaded[$name] = new $taskClass(
  68. $this->_Shell->stdout, $this->_Shell->stderr, $this->_Shell->stdin
  69. );
  70. return $this->_loaded[$name];
  71. }
  72. }