├── README.md ├── class_event.php └── test.php /README.md: -------------------------------------------------------------------------------- 1 | # PHP Event Dispatcher 2 | 3 | ## This library is abandoned, please use [Event Dispatcher](https://github.com/terrylinooo/event-dispatcher) instead. 4 | 5 | PHP Event Dispatcher is the most easy-to-use and simple Event Dispatcher PHP Class (Singleton Pattern). 6 | 7 | Original idea is from **Symfony's event listener system.** 8 | http://symfony.com/doc/current/components/event_dispatcher/index.html 9 | 10 | But I think it is a bit complex to me, I just want a simple Event Dispatcher for my probject. Therefore I made this for myself, and someone esle needs the simple EventDispatcher. 11 | 12 | ### Installation 13 | 14 | Download class.event.php to your probject folder, and then include it anywhere. 15 | 16 | ```php 17 | include_once('class.event.php'); 18 | ``` 19 | 20 | PHP Event Dispatcher is designed as Singleton Pattern, you can not call it by **New EventDispatcher()** Just because to make sure that only an Event Dispatcher to handle all events. 21 | 22 | Use the following **global** functions to use PHP Event Dispatcher, you can put it anywhere and easy inject in any framework. 23 | 24 | ### How to use 25 | 26 | Add a listener function to an event 27 | 28 | ```php 29 | addListener('event_name', 'function_name'); 30 | ``` 31 | 32 | Run this event 33 | 34 | ```php 35 | doDispatch('event_name'); 36 | ``` 37 | 38 | Remove a listener function from an envet 39 | 40 | Return: true / false 41 | 42 | ```php 43 | removeListener('event_name', 'function_name'); 44 | ``` 45 | 46 | Check out if there is any listener defined by an event 47 | 48 | Return: true / false 49 | 50 | ```php 51 | hasListener('event_name'); 52 | ``` 53 | 54 | Check if an event Listener actually exists 55 | 56 | Return: true / false 57 | 58 | ```php 59 | isListening('event_name'); 60 | ``` 61 | 62 | Check current running event Listener 63 | 64 | ```php 65 | nowListener(); 66 | ``` 67 | 68 | ## Examples 69 | 70 | test.php 71 | 72 | ```php 73 | A message has been sent!!

'; 81 | echo '

Current Running Event is called: ' . nowListener() . '

'; 82 | } 83 | 84 | doDispatch('event_send_message'); 85 | 86 | $event_name = 'event_send_message'; 87 | 88 | if ( hasListener($event_name) ) { 89 | echo '

Listener ' . $event_name . ' is existed.

'; 90 | } 91 | else { 92 | echo '

Listener ' . $event_name . ' is not existed.

'; 93 | } 94 | 95 | ?> 96 | ``` 97 | 98 | -------------------------------------------------------------------------------- /class_event.php: -------------------------------------------------------------------------------- 1 | $function); 71 | } 72 | } else { 73 | self::$events[$name][$priority][$function] = array("function" => $function); 74 | } 75 | 76 | return true; 77 | } 78 | 79 | /** 80 | * doDispatch() 81 | * 82 | * Trigger an event Listener 83 | * 84 | * @param mixed $name 85 | * @param mixed $arguments 86 | * @return mixed 87 | */ 88 | public function doDispatch($name, $arguments = "") 89 | { 90 | if (!isset(self::$events[$name])) { 91 | return $arguments; 92 | } 93 | 94 | // Set the current running event Listener 95 | self::$current_event = $name; 96 | 97 | ksort(self::$events[$name]); 98 | 99 | foreach (self::$events[$name] as $priority => $actions) { 100 | if (is_array($actions)) { 101 | foreach ($actions as $action) { 102 | // run Listener and store the value returned by registered functions 103 | if (function_exists($action['function'])) { 104 | $return_arguments = call_user_func_array($action['function'], array(&$arguments)); 105 | 106 | if ($return_arguments) { 107 | $arguments = $return_arguments; 108 | } 109 | // Store called Listeners 110 | self::$happened_events[$name][$priority] = $action['function']; 111 | } else { 112 | $this->error_message = 'Event Dispatcher: Function "' . $action['function'] . '" not found. (' . $name . ')'; 113 | $this->hasError(); 114 | } 115 | } 116 | } 117 | } 118 | 119 | // This listener is finished its job 120 | self::$current_event = ''; 121 | 122 | return $arguments; 123 | } 124 | 125 | /** 126 | * removeListener() 127 | * 128 | * Remove the event Listener from event array 129 | * 130 | * @param mixed $name 131 | * @param mixed $function 132 | * @param mixed $priority 133 | */ 134 | public function removeListener($name, $function, $priority = 10) 135 | { 136 | unset(self::$events[$name][$priority][$function]); 137 | 138 | if (!isset(self::$events[$name][$priority][$function])) { 139 | return true; 140 | } 141 | } 142 | 143 | /** 144 | * nowListener() 145 | * 146 | * Get the currently running event Listener 147 | * 148 | */ 149 | public function nowListener() 150 | { 151 | return self::$current_event; 152 | } 153 | 154 | /** 155 | * isListening() 156 | * 157 | * Check if a particular Listener has been called 158 | * 159 | * @param mixed $name 160 | * @param mixed $priority 161 | */ 162 | public function isListening($name, $priority = 10) 163 | { 164 | if (isset(self::$events[$name][$priority])) { 165 | return true; 166 | } 167 | return false; 168 | } 169 | 170 | /** 171 | * hasListener() 172 | * 173 | * @param mixed $name 174 | */ 175 | public function hasListener($name) 176 | { 177 | if (isset(self::$events[$name])) { 178 | return true; 179 | } 180 | return false; 181 | } 182 | 183 | /** 184 | * Debug 185 | * 186 | * @return void 187 | */ 188 | public static function debug() 189 | { 190 | if (isset(self::$events)) { 191 | echo "

Listeners

"; 192 | echo "
";
193 |             print_r(self::$events);
194 |             echo "
"; 195 | } 196 | 197 | if (isset(self::$happened_events)) { 198 | echo "

Listeners Called

"; 199 | echo "
";
200 |             print_r(self::$happened_events);
201 |             echo "
"; 202 | } 203 | } 204 | 205 | private function hasError() 206 | { 207 | die($this->error_message); 208 | } 209 | } 210 | 211 | /** 212 | * Add a new event Listener 213 | * 214 | * @param mixed $name 215 | * @param mixed $function 216 | * @param mixed $priority 217 | */ 218 | function addListener($name, $function, $priority = 10) 219 | { 220 | return EventDispatcher::instance()->addListener($name, $function, $priority); 221 | } 222 | 223 | /** 224 | * Run an event 225 | * 226 | * @param mixed $name 227 | * @param mixed $arguments 228 | * @return mixed 229 | */ 230 | function doDispatch($name, $arguments = "") 231 | { 232 | return EventDispatcher::instance()->doDispatch($name, $arguments); 233 | } 234 | 235 | /** 236 | * Remove an event Listener 237 | * 238 | * @param mixed $name 239 | * @param mixed $function 240 | * @param mixed $priority 241 | */ 242 | function removeListener($name, $function, $priority = 10) 243 | { 244 | return EventDispatcher::instance()->removeListener($name, $function, $priority); 245 | } 246 | 247 | /** 248 | * Check if an event Listener actually exists 249 | * 250 | * @param mixed $name 251 | */ 252 | function hasListener($name) 253 | { 254 | return EventDispatcher::instance()->hasListener($name); 255 | } 256 | 257 | /** 258 | * Check if a particular Listener has been called 259 | * 260 | * @param mixed $name 261 | */ 262 | function isListening($name, $priority = 10) 263 | { 264 | return EventDispatcher::instance()->isListening($name, $priority); 265 | } 266 | 267 | /** 268 | * Get current lister 269 | */ 270 | function nowListener() 271 | { 272 | return EventDispatcher::instance()->nowListener(); 273 | } 274 | -------------------------------------------------------------------------------- /test.php: -------------------------------------------------------------------------------- 1 | A message has been sent!!

'; 9 | echo '

Current Running Event is called: ' . nowListener() . '

'; 10 | } 11 | 12 | doDispatch('event_send_message'); 13 | 14 | $event_name = 'event_send_message'; 15 | 16 | if ( hasListener($event_name) ) { 17 | echo '

Listener ' . $event_name . ' is existed.

'; 18 | } 19 | else { 20 | echo '

Listener ' . $event_name . ' is not existed.

'; 21 | } 22 | 23 | ?> 24 | --------------------------------------------------------------------------------