TaskCollection.php 2.2 KB

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