1: <?php namespace Laravel;
2:
3: class Event {
4:
5: /**
6: * All of the registered events.
7: *
8: * @var array
9: */
10: public static $events = array();
11:
12: /**
13: * The queued events waiting for flushing.
14: *
15: * @var array
16: */
17: public static $queued = array();
18:
19: /**
20: * All of the registered queue flusher callbacks.
21: *
22: * @var array
23: */
24: public static $flushers = array();
25:
26: /**
27: * Determine if an event has any registered listeners.
28: *
29: * @param string $event
30: * @return bool
31: */
32: public static function listeners($event)
33: {
34: return isset(static::$events[$event]);
35: }
36:
37: /**
38: * Register a callback for a given event.
39: *
40: * <code>
41: * // Register a callback for the "start" event
42: * Event::listen('start', function() {return 'Started!';});
43: *
44: * // Register an object instance callback for the given event
45: * Event::listen('event', array($object, 'method'));
46: * </code>
47: *
48: * @param string $event
49: * @param mixed $callback
50: * @return void
51: */
52: public static function listen($event, $callback)
53: {
54: static::$events[$event][] = $callback;
55: }
56:
57: /**
58: * Override all callbacks for a given event with a new callback.
59: *
60: * @param string $event
61: * @param mixed $callback
62: * @return void
63: */
64: public static function override($event, $callback)
65: {
66: static::clear($event);
67:
68: static::listen($event, $callback);
69: }
70:
71: /**
72: * Add an item to an event queue for processing.
73: *
74: * @param string $queue
75: * @param string $key
76: * @param mixed $data
77: * @return void
78: */
79: public static function queue($queue, $key, $data = array())
80: {
81: static::$queued[$queue][$key] = $data;
82: }
83:
84: /**
85: * Register a queue flusher callback.
86: *
87: * @param string $queue
88: * @param mixed $callback
89: * @return void
90: */
91: public static function flusher($queue, $callback)
92: {
93: static::$flushers[$queue][] = $callback;
94: }
95:
96: /**
97: * Clear all event listeners for a given event.
98: *
99: * @param string $event
100: * @return void
101: */
102: public static function clear($event)
103: {
104: unset(static::$events[$event]);
105: }
106:
107: /**
108: * Fire an event and return the first response.
109: *
110: * <code>
111: * // Fire the "start" event
112: * $response = Event::first('start');
113: *
114: * // Fire the "start" event passing an array of parameters
115: * $response = Event::first('start', array('Laravel', 'Framework'));
116: * </code>
117: *
118: * @param string $event
119: * @param array $parameters
120: * @return mixed
121: */
122: public static function first($event, $parameters = array())
123: {
124: return head(static::fire($event, $parameters));
125: }
126:
127: /**
128: * Fire an event and return the first response.
129: *
130: * Execution will be halted after the first valid response is found.
131: *
132: * @param string $event
133: * @param array $parameters
134: * @return mixed
135: */
136: public static function until($event, $parameters = array())
137: {
138: return static::fire($event, $parameters, true);
139: }
140:
141: /**
142: * Flush an event queue, firing the flusher for each payload.
143: *
144: * @param string $queue
145: * @return void
146: */
147: public static function flush($queue)
148: {
149: foreach (static::$flushers[$queue] as $flusher)
150: {
151: // We will simply spin through each payload registered for the event and
152: // fire the flusher, passing each payloads as we go. This allows all
153: // the events on the queue to be processed by the flusher easily.
154: if ( ! isset(static::$queued[$queue])) continue;
155:
156: foreach (static::$queued[$queue] as $key => $payload)
157: {
158: array_unshift($payload, $key);
159:
160: call_user_func_array($flusher, $payload);
161: }
162: }
163: }
164:
165: /**
166: * Fire an event so that all listeners are called.
167: *
168: * <code>
169: * // Fire the "start" event
170: * $responses = Event::fire('start');
171: *
172: * // Fire the "start" event passing an array of parameters
173: * $responses = Event::fire('start', array('Laravel', 'Framework'));
174: *
175: * // Fire multiple events with the same parameters
176: * $responses = Event::fire(array('start', 'loading'), $parameters);
177: * </code>
178: *
179: * @param string|array $events
180: * @param array $parameters
181: * @param bool $halt
182: * @return array
183: */
184: public static function fire($events, $parameters = array(), $halt = false)
185: {
186: $responses = array();
187:
188: $parameters = (array) $parameters;
189:
190: // If the event has listeners, we will simply iterate through them and call
191: // each listener, passing in the parameters. We will add the responses to
192: // an array of event responses and return the array.
193: foreach ((array) $events as $event)
194: {
195: if (static::listeners($event))
196: {
197: foreach (static::$events[$event] as $callback)
198: {
199: $response = call_user_func_array($callback, $parameters);
200:
201: // If the event is set to halt, we will return the first response
202: // that is not null. This allows the developer to easily stack
203: // events but still get the first valid response.
204: if ($halt and ! is_null($response))
205: {
206: return $response;
207: }
208:
209: // After the handler has been called, we'll add the response to
210: // an array of responses and return the array to the caller so
211: // all of the responses can be easily examined.
212: $responses[] = $response;
213: }
214: }
215: }
216:
217: return $halt ? null : $responses;
218: }
219:
220: }