1 | <?php |
---|
2 | |
---|
3 | class event |
---|
4 | { |
---|
5 | function event() |
---|
6 | { |
---|
7 | trigger_error('The class "event" may only be invoked statically.', E_USER_ERROR); |
---|
8 | } |
---|
9 | |
---|
10 | function register($event, $callback) |
---|
11 | { |
---|
12 | if (is_array($callback)) |
---|
13 | { |
---|
14 | if (is_object($callback[0])) |
---|
15 | { |
---|
16 | trigger_error('The supplied callback has to point to a static method.', E_USER_WARNING); |
---|
17 | return; |
---|
18 | } |
---|
19 | |
---|
20 | // store callbacks as 'object::function' |
---|
21 | $callback = $callback[0].'::'.$callback[1]; |
---|
22 | } |
---|
23 | if (!strpos($callback, '::')) |
---|
24 | { |
---|
25 | trigger_error('The supplied callback "'.$callback.'" has to point to a static method.', E_USER_WARNING); |
---|
26 | return; |
---|
27 | } |
---|
28 | |
---|
29 | // we store the event callbacks reverse so you need one function for every event |
---|
30 | event::_put($callback, $event); |
---|
31 | } |
---|
32 | |
---|
33 | function call($event, &$object) |
---|
34 | { |
---|
35 | $cache = &event::_getCache(); |
---|
36 | foreach ($cache as $callback => $c_event) |
---|
37 | { |
---|
38 | // if the callback registered to the calling event we'll try to use his callback |
---|
39 | if ($event == $c_event) |
---|
40 | { |
---|
41 | $cb = explode('::', $callback); |
---|
42 | if (is_callable($cb)) |
---|
43 | { |
---|
44 | if (is_object($object)) |
---|
45 | { |
---|
46 | call_user_func($cb, &$object); |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | call_user_func($cb, null); |
---|
51 | } |
---|
52 | } |
---|
53 | else |
---|
54 | { |
---|
55 | trigger_error('The stored event handler "'.$c_event.'" is not callable (CB: "'.$callback.'").', E_USER_WARNING); |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | function add($event) |
---|
62 | { |
---|
63 | |
---|
64 | } |
---|
65 | |
---|
66 | function init() |
---|
67 | { |
---|
68 | // $db = new DBQuery(); |
---|
69 | // $db->execute('select rol_name, rol_descr from kb3_roles where rol_site=\''.KB_SITE."'"); |
---|
70 | // while ($row = $db->getRow()) |
---|
71 | // { |
---|
72 | // role::_put($row['rol_name'], $row['rol_descr']); |
---|
73 | // } |
---|
74 | } |
---|
75 | |
---|
76 | function &_getCache() |
---|
77 | { |
---|
78 | static $cache; |
---|
79 | |
---|
80 | if (!isset($cache)) |
---|
81 | { |
---|
82 | $cache = array(); |
---|
83 | } |
---|
84 | return $cache; |
---|
85 | } |
---|
86 | |
---|
87 | function _put($key, $data) |
---|
88 | { |
---|
89 | $cache = &event::_getCache(); |
---|
90 | $cache[$key] = $data; |
---|
91 | } |
---|
92 | |
---|
93 | function _get($key) |
---|
94 | { |
---|
95 | $cache = &event::_getCache(); |
---|
96 | |
---|
97 | if (!isset($cache[$key])) |
---|
98 | { |
---|
99 | return null; |
---|
100 | } |
---|
101 | return $cache[$key]; |
---|
102 | } |
---|
103 | } |
---|
104 | ?> |
---|