Skip to content

Commit ef6076b

Browse files
committed
added event queues.
1 parent af9f875 commit ef6076b

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

laravel/documentation/changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
- Fixed bug when using many-to-many relationships on non-default database connection.
7474
- Added true reflection based IoC to container.
7575
- Added `Request::route()->controller` and `Request::route()->controller_action`.
76+
- Added `Event::queue`, `Event::flusher`, and `Event::flush` methods to Event class.
7677

7778
<a name="upgrade-3.2"></a>
7879
## Upgrading From 3.1

laravel/event.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ class Event {
99
*/
1010
public static $events = array();
1111

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+
1226
/**
1327
* Determine if an event has any registered listeners.
1428
*
@@ -54,6 +68,31 @@ public static function override($event, $callback)
5468
static::listen($event, $callback);
5569
}
5670

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+
5796
/**
5897
* Clear all event listeners for a given event.
5998
*
@@ -99,6 +138,28 @@ public static function until($event, $parameters = array())
99138
return static::fire($event, $parameters, true);
100139
}
101140

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+
102163
/**
103164
* Fire an event so that all listeners are called.
104165
*

0 commit comments

Comments
 (0)