PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ruckusing/lib/classes/task/class.Ruckusing_TaskManager.php

https://github.com/radicaldesigns/jaguar
PHP | 113 lines | 75 code | 20 blank | 18 comment | 14 complexity | 55307057c98561fea4c236c184b5be6a MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  1. <?php
  2. //requirements
  3. require_once RUCKUSING_BASE . '/lib/classes/util/class.Ruckusing_NamingUtil.php';
  4. define('RUCKUSING_TASK_DIR', RUCKUSING_BASE . '/lib/tasks');
  5. class Ruckusing_TaskManager {
  6. private $adapter;
  7. private $tasks = array();
  8. function __construct($adapter) {
  9. $this->set_adapter($adapter);
  10. $this->load_all_tasks(RUCKUSING_TASK_DIR);
  11. }//__construct
  12. public function set_adapter($adapter) {
  13. $this->adapter = $adapter;
  14. }
  15. public function get_adapter() {
  16. return $this->adapter;
  17. }
  18. /*
  19. Searches for the given task, and if found
  20. returns it. Otherwise null is returned.
  21. */
  22. public function get_task($key) {
  23. if( array_key_exists($key, $this->tasks)) {
  24. return $this->tasks[$key];
  25. } else {
  26. return null;
  27. }
  28. }
  29. public function has_task($key) {
  30. if( array_key_exists($key, $this->tasks)) {
  31. return true;
  32. } else {
  33. return false;
  34. }
  35. }
  36. /*
  37. Register a new task name under the specified key.
  38. $obj is a class which implements the iTask interface
  39. and has an execute() method defined.
  40. */
  41. public function register_task($key, $obj) {
  42. if( array_key_exists($key, $this->tasks)) {
  43. trigger_error(sprintf("Task key '%s' is already defined!", $key));
  44. return false;
  45. }
  46. //Reflect on the object and make sure it has an "execute()" method
  47. $refl = new ReflectionObject($obj);
  48. if( !$refl->hasMethod('execute')) {
  49. trigger_error(sprintf("Task '%s' does not have an 'execute' method defined", $key));
  50. return false;
  51. }
  52. $this->tasks[$key] = $obj;
  53. return true;
  54. }
  55. public function get_name() {
  56. }
  57. //---------------------
  58. // PRIVATE METHODS
  59. //---------------------
  60. private function load_all_tasks($task_dir) {
  61. if(!is_dir($task_dir)) {
  62. throw new Exception(sprintf("Task dir: %s does not exist", $task_dir));
  63. return false;
  64. }
  65. $files = scandir($task_dir);
  66. $regex = '/^class\.(\w+)\.php$/';
  67. foreach($files as $f) {
  68. //skip over invalid files
  69. if($f == '.' || $f == ".." || !preg_match($regex,$f, $matches) ) { continue; }
  70. require_once $task_dir . '/' . $f;
  71. $task_name = Ruckusing_NamingUtil::task_from_class_name($matches[1]);
  72. $klass = Ruckusing_NamingUtil::class_from_file_name($f);
  73. $this->register_task($task_name, new $klass($this->get_adapter()));
  74. }
  75. }//require_tasks
  76. /*
  77. Execute the supplied Task object
  78. */
  79. private function execute_task($task_obj) {
  80. }
  81. public function execute($framework, $task_name, $options) {
  82. if( !$this->has_task($task_name)) {
  83. throw new Exception("Task '$task_name' is not registered.");
  84. }
  85. $task = $this->get_task($task_name);
  86. if($task) {
  87. $task->set_framework($framework);
  88. return $task->execute($options);
  89. }
  90. return "";
  91. }
  92. }
  93. ?>