1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
<?php
namespace Dhtmlx\Connector\Tools;
/*! Class which allows to assign|fire events.
*/
class EventMaster{
private $events;//!< hash of event handlers
private $master;
private static $eventsStatic=array();
/*! constructor
*/
function __construct(){
$this->events=array();
$this->master = false;
}
/*! Method check if event with such name already exists.
@param name
name of event, case non-sensitive
@return
true if event with such name registered, false otherwise
*/
public function exist($name){
$name=strtolower($name);
return (isset($this->events[$name]) && sizeof($this->events[$name]));
}
/*! Attach custom code to event.
Only on event handler can be attached in the same time. If new event handler attached - old will be detached.
@param name
name of event, case non-sensitive
@param method
function which will be attached. You can use array(class, method) if you want to attach the method of the class.
*/
public function attach($name,$method=false){
//use class for event handling
if ($method === false){
$this->master = $name;
return;
}
//use separate functions
$name=strtolower($name);
if (!array_key_exists($name,$this->events))
$this->events[$name]=array();
$this->events[$name][]=$method;
}
public static function attach_static($name, $method){
$name=strtolower($name);
if (!array_key_exists($name,EventMaster::$eventsStatic))
EventMaster::$eventsStatic[$name]=array();
EventMaster::$eventsStatic[$name][]=$method;
}
public static function trigger_static($name, $method){
$arg_list = func_get_args();
$name=strtolower(array_shift($arg_list));
if (isset(EventMaster::$eventsStatic[$name]))
foreach(EventMaster::$eventsStatic[$name] as $method){
if (is_array($method) && !method_exists($method[0],$method[1]))
throw new Exception("Incorrect method assigned to event: ".$method[0].":".$method[1]);
if (!is_array($method) && !function_exists($method))
throw new Exception("Incorrect function assigned to event: ".$method);
call_user_func_array($method, $arg_list);
}
return true;
}
/*! Detach code from event
@param name
name of event, case non-sensitive
*/
public function detach($name){
$name=strtolower($name);
unset($this->events[$name]);
}
/*! Trigger event.
@param name
name of event, case non-sensitive
@param data
value which will be provided as argument for event function,
you can provide multiple data arguments, method accepts variable number of parameters
@return
true if event handler was not assigned , result of event hangler otherwise
*/
public function trigger($name,$data){
$arg_list = func_get_args();
$name=strtolower(array_shift($arg_list));
if (isset($this->events[$name]))
foreach($this->events[$name] as $method){
if (is_array($method) && !method_exists($method[0],$method[1]))
throw new Exception("Incorrect method assigned to event: ".$method[0].":".$method[1]);
if (!is_array($method) && !function_exists($method))
throw new Exception("Incorrect function assigned to event: ".$method);
call_user_func_array($method, $arg_list);
}
if ($this->master !== false)
if (method_exists($this->master, $name))
call_user_func_array(array($this->master, $name), $arg_list);
return true;
}
}
|