-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventEmitter.class.php
119 lines (107 loc) · 3.1 KB
/
EventEmitter.class.php
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
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
*
* @author mE
* @link https://github.com/Wolfy87/EventEmitter
* @see Mootools/Class.Extras.js
* @license MIT-style license
* @version 0.1.0
*/
class EventEmitter{
/**
* array holding arrays of event handler functions
*
* @var array
*/
protected $events;
/**
* instanciate new EventEmitter
*/
public function __construct(){
$this->events = array();
}
/**
*
*
* @param string $name the type of event to wait for
* @param callback $handler callback function to execute
* @return EventEmitter $this (chainable)
*/
public function addEvent($name,$handler){
if(!isset($this->events[$name])){
$this->events[$name] = array($handler);
}else{
$this->events[$name][] = $handler;
}
}
/**
* The same as addEvent, but accepts an array to add multiple events at once.
*
* @param array $events An object with key/value representing: key the event name (e.g. 'start'), and value the function that is called when the Event occurs.
* @return EventEmitter $this (chainable)
*/
public function addEvents($events){
foreach($events as $name=>$handler){
$this->addEvent($name,$handler);
}
return $this;
}
/**
*
*
* @param string $name
* @param callback|NULL $handler
* @return EventEmitter $this (chainable)
*/
public function removeEvent($name,$handler=NULL){
if(isset($this->events[$name])){
if($handler === NULL){
unset($this->events[$name]);
}else{
foreach($this->events[$name] as $key=>$fn){
if($fn === $handler){
unset($this->events[$key]);
}
}
if(!$this->events[$name]){
unset($this->events[$name]);
}
}
}
return $this;
}
/**
* Fires all events of the specified type
*
* @param string $name
* @return EventEmitter $this (chainable)
*/
public function fireEvent($name){
if(isset($this->events[$name])){
$args = func_get_args();
array_shift($args);
foreach($this->events[$name] as $event){
call_user_func_array($event,$args);
}
}
return $this;
}
////////////////////////////////////////////////////////////////////////////
// alias definitions
public function on($name,$handler){
return $this->addEvent($name,$handler);
}
public function addListener($name,$handler){
return $this->addEvent($name,$handler);
}
public function removeListener($name,$handler=NULL){
return $this->removeEvent($name,$handler);
}
public function emit($name){
if(isset($this->events[$name])){
$args = func_get_args();
call_user_func_array(array($this,'fireEvent'),$args);
}
return $this;
}
}