-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.php
67 lines (55 loc) · 1.8 KB
/
events.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
<?php
/**
* Class to manage the event listeners, and emit events.
*
* @author Faizan Ayubi
*/
namespace Framework {
class Events {
private static $_callbacks = array();
private function __construct() {
// do nothing
}
private function __clone() {
// do nothing
}
/**
* Adds the provided $callback to the $_callbacks array, so that it can be executed when any $type events happen.
* @param type $type
* @param type $callback
*/
public static function add($type, $callback) {
if (empty(self::$_callbacks[$type])) {
self::$_callbacks[$type] = array();
}
self::$_callbacks[$type][] = $callback;
}
/**
* Emits/triggers an event. If you provide an optional $parameters array,
* this array will be available to all the callbacks executed.
* @param type $type
* @param type $parameters
*/
public static function fire($type, $parameters = null) {
if (!empty(self::$_callbacks[$type])) {
foreach (self::$_callbacks[$type] as $callback) {
call_user_func_array($callback, $parameters);
}
}
}
/**
* Simply removes the stored $callback from the $_callbacks array.
* @param type $type
* @param type $callback
*/
public static function remove($type, $callback) {
if (!empty(self::$_callbacks[$type])) {
foreach (self::$_callbacks[$type] as $i => $found) {
if ($callback == $found) {
unset(self::$_callbacks[$type][$i]);
}
}
}
}
}
}