-
Notifications
You must be signed in to change notification settings - Fork 7
/
EventDispatcher.php
98 lines (88 loc) · 2.73 KB
/
EventDispatcher.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
<?php
namespace Luracast\Restler;
/**
* Static event broadcasting system for Restler
*
* @category Framework
* @package Restler
* @author R.Arul Kumaran <arul@luracast.com>
* @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/
*
*/
use Closure;
class EventDispatcher
{
private $listeners = array();
protected static $_waitList = array();
public static $self;
protected $events = array();
public function __construct() {
static::$self = $this;
if (!empty(static::$_waitList)) {
foreach (static::$_waitList as $param) {
call_user_func_array(array($this,$param[0]), $param[1]);
}
}
}
public static function __callStatic($eventName, $params)
{
if (0 === strpos($eventName, 'on')) {
if(static::$self){
return call_user_func_array(array(static::$self, $eventName), $params);
}
static::$_waitList[] = func_get_args();
return false;
}
}
public function __call($eventName, $params)
{
if (0 === strpos($eventName, 'on')) {
if (!isset($this->listeners[$eventName]) || !is_array($this->listeners[$eventName]))
$this->listeners[$eventName] = array();
$this->listeners[$eventName][] = $params[0];
}
return $this;
}
public static function addListener($eventName, Closure $callback)
{
return static::$eventName($callback);
}
public function on(array $eventHandlers)
{
for (
$count = count($eventHandlers),
$events = array_map(
'ucfirst',
$keys = array_keys(
$eventHandlers = array_change_key_case(
$eventHandlers,
CASE_LOWER
)
)
),
$i = 0;
$i < $count;
call_user_func(
array($this, "on{$events[$i]}"),
$eventHandlers[$keys[$i++]]
)
);
}
/**
* Fire an event to notify all listeners
*
* @param string $eventName name of the event
* @param array $params event related data
*/
protected function dispatch($eventName, array $params = array())
{
$this->events[] = $eventName;
$params = func_get_args();
$eventName = 'on'.ucfirst(array_shift($params));
if (isset($this->listeners[$eventName]))
foreach ($this->listeners[$eventName] as $callback)
call_user_func_array($callback, $params);
}
}