@@ -9,6 +9,20 @@ class Event {
9
9
*/
10
10
public static $ events = array ();
11
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
+
12
26
/**
13
27
* Determine if an event has any registered listeners.
14
28
*
@@ -54,6 +68,31 @@ public static function override($event, $callback)
54
68
static ::listen ($ event , $ callback );
55
69
}
56
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 )
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
+
57
96
/**
58
97
* Clear all event listeners for a given event.
59
98
*
@@ -99,6 +138,28 @@ public static function until($event, $parameters = array())
99
138
return static ::fire ($ event , $ parameters , true );
100
139
}
101
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
+ foreach (static ::$ queued [$ queue ] as $ key => $ payload )
155
+ {
156
+ array_unshift ($ payload , $ key );
157
+
158
+ call_user_func_array ($ flusher , $ payload );
159
+ }
160
+ }
161
+ }
162
+
102
163
/**
103
164
* Fire an event so that all listeners are called.
104
165
*
0 commit comments