|
12 | 12 |
|
13 | 13 | /** Notify modules using this class*/
|
14 | 14 | class MIDAS_Notifier
|
15 |
| - { |
16 |
| - |
17 |
| - private $modules = array(); |
| 15 | + { |
| 16 | + public $modules = array(); |
| 17 | + public $tasks = array(); |
| 18 | + private $tasksByModule = array(); |
| 19 | + public $notifications = array(); |
18 | 20 |
|
19 | 21 | /** init the notifier*/
|
20 | 22 | public function __construct()
|
@@ -44,18 +46,137 @@ public function __construct()
|
44 | 46 | }
|
45 | 47 | $this->modules['core'] = new $name();
|
46 | 48 | }
|
| 49 | + |
| 50 | + foreach($this->modules as $module => $notificationClass) |
| 51 | + { |
| 52 | + $tasks = $notificationClass->getTasks(); |
| 53 | + foreach($tasks as $name => $task) |
| 54 | + { |
| 55 | + if(isset($this->tasks[$name])) |
| 56 | + { |
| 57 | + throw new Zend_Exception('Task already exits'); |
| 58 | + } |
| 59 | + if(strpos($name, "TASK_") === false || !is_string($name)) |
| 60 | + { |
| 61 | + throw new Zend_Exception('Task name should be a string: TASK_MODULE_NAME'); |
| 62 | + } |
| 63 | + $this->tasks[$name] = $task; |
| 64 | + $this->tasks[$name]['module'] = $module; |
| 65 | + |
| 66 | + $this->tasksByModule[$module] = $task; |
| 67 | + $this->tasksByModule[$module]['name'] = $name; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + foreach($this->modules as $module => $notificationClass) |
| 72 | + { |
| 73 | + $notifications = $notificationClass->getNotifications(); |
| 74 | + foreach($notifications as $name => $notificationArray) |
| 75 | + { |
| 76 | + if(!isset($this->notifications[$name])) |
| 77 | + { |
| 78 | + $this->notifications[$name] = array(); |
| 79 | + } |
| 80 | + |
| 81 | + foreach($notificationArray as $notification) |
| 82 | + { |
| 83 | + if($notification['type'] == 'callback') |
| 84 | + { |
| 85 | + if(strpos($name, "CALLBACK_") === false || !is_string($name)) |
| 86 | + { |
| 87 | + throw new Zend_Exception('Callback name should be a string: CALLBACK_MODULE_NAME. Was '.$name); |
| 88 | + } |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + if(strpos($name, "EVENT_") === false || !is_string($name)) |
| 93 | + { |
| 94 | + throw new Zend_Exception('Event name should be a string: EVENT_MODULE_NAME. Was '.$name); |
| 95 | + } |
| 96 | + if(!isset($this->tasks[$notification['call']])) |
| 97 | + { |
| 98 | + throw new Zend_Exception('The task doesn\'t exit'); |
| 99 | + } |
| 100 | + } |
| 101 | + $notification['module'] = $module; |
| 102 | + $this->notifications[$name][] = $notification; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
47 | 106 | }//end contruct()
|
48 | 107 |
|
49 | 108 | /** notify enabled modules*/
|
50 |
| - public function notify($type, $params = null) |
| 109 | + public function notifyEvent($name, $params = null, $moduleFilter = array()) |
| 110 | + { |
| 111 | + if(strpos($name, "EVENT_") === false || !is_string($name)) |
| 112 | + { |
| 113 | + throw new Zend_Exception('Event name should be a string: EVENT_MODULE_NAME. Was '.$name); |
| 114 | + } |
| 115 | + if(!isset($this->notifications[$name])) |
| 116 | + { |
| 117 | + return; |
| 118 | + } |
| 119 | + if(is_string($moduleFilter)) |
| 120 | + { |
| 121 | + $moduleFilter = array($moduleFilter); |
| 122 | + } |
| 123 | + $return = array(); |
| 124 | + foreach($this->notifications[$name] as $key => $notification) |
| 125 | + { |
| 126 | + $module = $this->modules[$notification['module']]; |
| 127 | + if(empty($moduleFilter) || in_array($module, $moduleFilter)) |
| 128 | + { |
| 129 | + $this->_setTask($notification['call'], $params, $notification['priority']); |
| 130 | + } |
| 131 | + } |
| 132 | + return $return; |
| 133 | + }//end notify |
| 134 | + |
| 135 | + /** schedule or execute a task*/ |
| 136 | + private function _setTask($name, $params, $priority) |
| 137 | + { |
| 138 | + $modules = Zend_Registry::get('modulesEnable'); |
| 139 | + if(!isset($this->tasks[$name])) |
| 140 | + { |
| 141 | + return; |
| 142 | + } |
| 143 | + if(isset($this->modules['scheduler'])) |
| 144 | + { |
| 145 | + $params = array('task' => $name, 'priority' => $priority, 'params' => $params, 'run_only_once' => true, 'fire_time' => date('c')); |
| 146 | + call_user_func(array($this->modules['scheduler'], $this->tasks['TASK_SCHEDULER_SCHEDULE_TASK']['method']), $params); |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + call_user_func(array($this->modules[$this->tasks[$name]['module']], $this->tasks[$name]['method']), $params); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** notify enabled modules*/ |
| 155 | + public function callback($name, $params = null, $moduleFilter = array()) |
51 | 156 | {
|
| 157 | + if(strpos($name, "CALLBACK_") === false || !is_string($name)) |
| 158 | + { |
| 159 | + throw new Zend_Exception('Callback name should be a string: CALLBACK_MODULE_NAME. Was '.$name); |
| 160 | + } |
| 161 | + if(!isset($this->notifications[$name])) |
| 162 | + { |
| 163 | + return array(); |
| 164 | + } |
| 165 | + if(is_string($moduleFilter)) |
| 166 | + { |
| 167 | + $moduleFilter = array($moduleFilter); |
| 168 | + } |
52 | 169 | $return = array();
|
53 |
| - foreach($this->modules as $key => $module) |
| 170 | + foreach($this->notifications[$name] as $key => $notification) |
54 | 171 | {
|
55 |
| - $tmp = call_user_func(array($module, 'init'), $type, $params); |
56 |
| - if($tmp != null) |
| 172 | + $module = $this->modules[$notification['module']]; |
| 173 | + if(empty($moduleFilter) || in_array($module, $moduleFilter)) |
57 | 174 | {
|
58 |
| - $return[$key] = $tmp; |
| 175 | + $tmp = call_user_func(array($module, $notification['call']), $params); |
| 176 | + if($tmp != null) |
| 177 | + { |
| 178 | + $return[$notification['module']] = $tmp; |
| 179 | + } |
59 | 180 | }
|
60 | 181 | }
|
61 | 182 | return $return;
|
|
0 commit comments