testing
[namibia] / module / Utility / src / Utility / Event.php
1 <?php
2 namespace Utility;
3
4
5
6 /**
7  * Simple event handler.
8  * @author andre.fourie
9  *
10  */
11 class Event
12 {
13
14         /**
15          * @var array
16          */
17         static protected $listener = array();
18
19
20         /**
21          * Register an event listener.
22          * @param string $eventName
23          * @param object|string $class
24          * @param string $action
25          */
26         static public function listen($eventName, $class, $action)
27         {
28                 !isset(self::$listener[$eventName])
29                         && self::$listener[$eventName] = array();
30                 self::$listener[$eventName][] = array(
31                                 'Class'  => $class,
32                                 'Action' => $action
33                 );
34         }
35
36         /**
37          * Trigger an event.
38          * @param string $eventName
39          * @param unknown|null $data
40          */
41         static public function trigger($eventName, $data = null)
42         {
43                 if (!isset(self::$listener[$eventName]))
44                 {
45                         return;
46                 }
47                 foreach (self::$listener[$eventName] as $listener)
48                 {
49                         $obj = is_object($listener['Class'])
50                                 ? $listener['Class']
51                                 : new $listener['Class']();
52                         $action = $listener['Action'];
53                         $obj->$action($eventName, $data);
54                 }
55         }
56
57 }
58