Base for a static organization website

TaskCollection.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 http://www.opensource.org/licenses/mit-license.php MIT License
  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 The shell this task collection is attached to.
  42. */
  43. public function __construct(Shell $Shell) {
  44. $this->_Shell = $Shell;
  45. }
  46. /**
  47. * Loads/constructs a task. Will return the instance in the registry if it already exists.
  48. *
  49. * You can alias your task as an existing task by setting the 'className' key, i.e.,
  50. * ```
  51. * public $tasks = array(
  52. * 'DbConfig' => array(
  53. * 'className' => 'Bakeplus.DbConfigure'
  54. * );
  55. * );
  56. * ```
  57. * All calls to the `DbConfig` task would use `DbConfigure` found in the `Bakeplus` plugin instead.
  58. *
  59. * @param string $task Task name to load
  60. * @param array $settings Settings for the task.
  61. * @return Task A task object, Either the existing loaded task or a new one.
  62. * @throws MissingTaskException when the task could not be found
  63. */
  64. public function load($task, $settings = array()) {
  65. if (is_array($settings) && isset($settings['className'])) {
  66. $alias = $task;
  67. $task = $settings['className'];
  68. }
  69. list($plugin, $name) = pluginSplit($task, true);
  70. if (!isset($alias)) {
  71. $alias = $name;
  72. }
  73. if (isset($this->_loaded[$alias])) {
  74. return $this->_loaded[$alias];
  75. }
  76. $taskClass = $name . 'Task';
  77. App::uses($taskClass, $plugin . 'Console/Command/Task');
  78. $exists = class_exists($taskClass);
  79. if (!$exists) {
  80. throw new MissingTaskException(array(
  81. 'class' => $taskClass,
  82. 'plugin' => substr($plugin, 0, -1)
  83. ));
  84. }
  85. $this->_loaded[$alias] = new $taskClass(
  86. $this->_Shell->stdout, $this->_Shell->stderr, $this->_Shell->stdin
  87. );
  88. return $this->_loaded[$alias];
  89. }
  90. }