1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pimf |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Provides a great way to build de-coupled applications and allows plug-ins to tap |
13
|
|
|
* into the core of your application without modifying the code. |
14
|
|
|
* |
15
|
|
|
* Register a callback for a given event. |
16
|
|
|
* |
17
|
|
|
* <code> |
18
|
|
|
* // register a callback for the "start" event |
19
|
|
|
* Efs_Event::listen('start', function () {return 'Started!';}); |
20
|
|
|
* |
21
|
|
|
* // register an object instance callback for the given event |
22
|
|
|
* Efs_Event::listen('event', array($object, 'method')); |
23
|
|
|
* </code> |
24
|
|
|
* |
25
|
|
|
* Fire an event and return the first response. |
26
|
|
|
* |
27
|
|
|
* <code> |
28
|
|
|
* // fire the "start" event |
29
|
|
|
* $response = Efs_Event::first('start'); |
30
|
|
|
* |
31
|
|
|
* // fire the "start" event passing an array of parameters |
32
|
|
|
* $response = Efs_Event::first('start', array('Pimf', 'Framework')); |
33
|
|
|
* </code> |
34
|
|
|
* |
35
|
|
|
* Fire an event so that all listeners are called. |
36
|
|
|
* |
37
|
|
|
* <code> |
38
|
|
|
* // fire the "start" event |
39
|
|
|
* $responses = Efs_Event::fire('start'); |
40
|
|
|
* |
41
|
|
|
* // fire the "start" event passing an array of parameters |
42
|
|
|
* $responses = Efs_Event::fire('start', array('Pimf', 'Framework')); |
43
|
|
|
* |
44
|
|
|
* // fire multiple events with the same parameters |
45
|
|
|
* $responses = Efs_Event::fire(array('start', 'loading'), $parameters); |
46
|
|
|
* </code> |
47
|
|
|
* |
48
|
|
|
* @package Pimf |
49
|
|
|
* @author Gjero Krsteski <[email protected]> |
50
|
|
|
*/ |
51
|
|
|
class Event |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* All registered events. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected static $events = array(); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Determine if an event has any registered listeners. |
62
|
|
|
* |
63
|
|
|
* @param string $event |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public static function listeners($event) |
68
|
|
|
{ |
69
|
|
|
return isset(static::$events[$event]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Register a callback for a given event. |
74
|
|
|
* |
75
|
|
|
* @param string $event |
76
|
|
|
* @param mixed $callback |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public static function listen($event, $callback) |
81
|
|
|
{ |
82
|
|
|
static::$events[$event][] = $callback; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Override all callbacks for a given event with a new callback. |
87
|
|
|
* |
88
|
|
|
* @param string $event |
89
|
|
|
* @param mixed $callback |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public static function override($event, $callback) |
94
|
|
|
{ |
95
|
|
|
static::clear($event); |
96
|
|
|
static::listen($event, $callback); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Clear all event listeners for a given event. |
101
|
|
|
* |
102
|
|
|
* @param string $event |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public static function clear($event) |
107
|
|
|
{ |
108
|
|
|
unset(static::$events[$event]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Fire an event and return the first response. |
113
|
|
|
* |
114
|
|
|
* @param string $event |
115
|
|
|
* @param \Exception[] $parameters |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public static function first($event, $parameters = array()) |
120
|
|
|
{ |
121
|
|
|
$responses = static::fire($event, $parameters); |
122
|
|
|
|
123
|
|
|
return reset($responses); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Fire an event and return the first response. |
128
|
|
|
* Execution will be halted after the first valid response is found. |
129
|
|
|
* |
130
|
|
|
* @param string $event |
131
|
|
|
* @param array $parameters |
132
|
|
|
* |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
|
public static function until($event, $parameters = array()) |
136
|
|
|
{ |
137
|
|
|
return static::fire($event, $parameters, true); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Fire an event so that all listeners are called. |
142
|
|
|
* |
143
|
|
|
* @param string $events |
144
|
|
|
* @param array $parameters |
145
|
|
|
* @param bool $halt |
146
|
|
|
* |
147
|
|
|
* @return array|null |
148
|
|
|
*/ |
149
|
|
|
public static function fire($events, $parameters = array(), $halt = false) |
150
|
|
|
{ |
151
|
|
|
$responses = array(); |
152
|
|
|
$parameters = (array)$parameters; |
153
|
|
|
|
154
|
|
|
// If the event has listeners, iterate through them and call each listener, |
155
|
|
|
// passing in the parameters. |
156
|
|
|
foreach ((array)$events as $event) { |
157
|
|
|
|
158
|
|
|
if (static::listeners($event)) { |
159
|
|
|
|
160
|
|
|
foreach (static::$events[$event] as $callback) { |
161
|
|
|
$response = call_user_func_array($callback, $parameters); |
162
|
|
|
|
163
|
|
|
// If the event is set to halt, |
164
|
|
|
// return the first response that is not null. |
165
|
|
|
if ($halt && !is_null($response)) { |
166
|
|
|
return $response; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$responses[] = $response; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $halt ? null : $responses; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|