1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Ajde_Event extends Ajde_Object_Static |
4
|
|
|
{ |
5
|
|
|
protected static $eventStack = []; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Register a callback on an object event. Note that when an object instance |
9
|
|
|
* is passed, only the classname is used. Thus, an Ajde_Event can not be used |
10
|
|
|
* to trigger different callbacks for the same event on different object instances |
11
|
|
|
* of the same class. |
12
|
|
|
* |
13
|
|
|
* @param mixed $object Object instance or classname triggering the event |
14
|
|
|
* @param string $event Event name |
15
|
|
|
* @param mixed $callback Callback |
16
|
|
|
* |
17
|
|
|
* @return bool true |
18
|
|
|
*/ |
19
|
|
|
public static function register($object, $event, $callback) |
20
|
|
|
{ |
21
|
|
|
self::$eventStack[self::className($object)][$event][] = $callback; |
22
|
|
|
|
23
|
|
|
return true; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function unregister($object, $event) |
27
|
|
|
{ |
28
|
|
|
if (isset(self::$eventStack[self::className($object)][$event])) { |
29
|
|
|
unset(self::$eventStack[self::className($object)][$event]); |
30
|
|
|
|
31
|
|
|
return true; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function has($object, $event, $callback = null) |
38
|
|
|
{ |
39
|
|
|
if (isset($callback)) { |
40
|
|
|
return |
41
|
|
|
isset(self::$eventStack[self::className($object)][$event]) && |
42
|
|
|
in_array($callback, self::$eventStack[self::className($object)][$event]); |
43
|
|
|
} else { |
44
|
|
|
return isset(self::$eventStack[self::className($object)][$event]); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function trigger($object, $event, array $parameters = []) |
49
|
|
|
{ |
50
|
|
|
foreach (self::$eventStack as $className => $eventStack) { |
51
|
|
|
if (self::className($object) == $className || |
52
|
|
|
is_subclass_of(self::className($object), $className) |
|
|
|
|
53
|
|
|
) { |
54
|
|
|
if (isset($eventStack[$event])) { |
55
|
|
|
foreach ($eventStack[$event] as $eventCallback) { |
56
|
|
|
$retval = null; |
|
|
|
|
57
|
|
|
$callback = null; |
58
|
|
|
if (is_callable($eventCallback)) { |
59
|
|
|
$callback = $eventCallback; |
60
|
|
|
} elseif (is_string($eventCallback)) { |
61
|
|
|
if (is_callable([$object, $eventCallback])) { |
62
|
|
|
$callback = [$object, $eventCallback]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
if (isset($callback)) { |
66
|
|
|
$trace = debug_backtrace(); |
67
|
|
|
$current = array_shift($trace); |
|
|
|
|
68
|
|
|
$caller = array_shift($trace); |
69
|
|
|
|
70
|
|
|
if (isset($caller['object'])) { |
71
|
|
|
// http://www.php.net/manual/en/function.call-user-func.php |
72
|
|
|
// Note: Note that the parameters for call_user_func() are not passed by reference. |
73
|
|
|
$parameterArray = array_merge([&$caller['object']], $parameters); |
74
|
|
|
$retval = call_user_func_array($callback, $parameterArray); |
75
|
|
|
if (isset($retval)) { |
76
|
|
|
// TODO: Aborts execution of cueue! |
77
|
|
|
return $retval; |
78
|
|
|
} |
79
|
|
|
} elseif (isset($caller['class']) && isset($caller['function']) && $caller['type'] === '::') { |
80
|
|
|
// triggered from static context |
81
|
|
|
// TODO: update exception 90015 |
82
|
|
|
$retval = call_user_func_array($callback, $parameters); |
83
|
|
|
if (isset($retval)) { |
84
|
|
|
// TODO: Aborts execution of cueue! |
85
|
|
|
return $retval; |
86
|
|
|
} |
87
|
|
|
} elseif ($callback instanceof Closure) { |
88
|
|
|
$retval = $callback(); |
89
|
|
|
if (isset($retval)) { |
90
|
|
|
// TODO: Aborts execution of cueue! |
91
|
|
|
return $retval; |
92
|
|
|
} |
93
|
|
|
} else { |
94
|
|
|
throw new Ajde_Exception('Event context needed to |
95
|
|
|
fire, none detected', 90015); |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
// TODO: right now never fires in Object_Magic objects |
99
|
|
|
// because of the __call magic function. Workaround |
100
|
|
|
// could be something like in_array("bar",get_class_methods($f1) |
101
|
|
|
// see: http://php.net/manual/en/function.method-exists.php |
102
|
|
|
throw new Ajde_Exception('Callback is not valid', 90016); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected static function className($object) |
111
|
|
|
{ |
112
|
|
|
if (is_object($object)) { |
113
|
|
|
return get_class($object); |
114
|
|
|
} elseif (is_string($object) && class_exists($object)) { |
115
|
|
|
return $object; |
116
|
|
|
} |
117
|
|
|
throw new Ajde_Exception('No classname or object instance given, or classname is incorrect', 90012); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|