TaskCollection.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package cake.console.libs
  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. class TaskCollection extends ObjectCollection {
  20. /**
  21. * Shell to use to set params to tasks.
  22. *
  23. * @var array
  24. */
  25. protected $_Shell;
  26. /**
  27. * The directory inside each shell path that contains tasks.
  28. *
  29. * @var string
  30. */
  31. public $taskPathPrefix = 'tasks/';
  32. /**
  33. * Constructor
  34. *
  35. * @param array $paths Array of paths to search for tasks on .
  36. * @return void
  37. */
  38. public function __construct(Shell $Shell) {
  39. $this->_Shell = $Shell;
  40. }
  41. /**
  42. * Loads/constructs a task. Will return the instance in the collection
  43. * if it already exists.
  44. *
  45. * @param string $task Task name to load
  46. * @param array $settings Settings for the task.
  47. * @return Task A task object, Either the existing loaded task or a new one.
  48. * @throws MissingTaskFileException, MissingTaskClassException when the task could not be found
  49. */
  50. public function load($task, $settings = array()) {
  51. list($plugin, $name) = pluginSplit($task, true);
  52. if (isset($this->_loaded[$name])) {
  53. return $this->_loaded[$name];
  54. }
  55. $taskFile = Inflector::underscore($name);
  56. $taskClass = $name . 'Task';
  57. App::uses($taskClass, $plugin . 'Console/Command/Task');
  58. if (!class_exists($taskClass)) {
  59. if (!class_exists($taskClass)) {
  60. throw new MissingTaskClassException($taskClass);
  61. }
  62. }
  63. $this->_loaded[$name] = new $taskClass(
  64. $this->_Shell->stdout, $this->_Shell->stderr, $this->_Shell->stdin
  65. );
  66. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  67. if ($enable === true) {
  68. $this->_enabled[] = $name;
  69. }
  70. return $this->_loaded[$name];
  71. }
  72. }