Skip to content

Commit dbbfc62

Browse files
committed
Remove event priorities.
The whole concept of event priorities doesn’t make sense as a concept and can’t even be trusted as an end user because you are relying on the exact order of listeners. Priority also makes no sense in modern apps where many events may be queued. Can be implemented as a synchronous pipeline. Also removed halting events as this suffers from same problem of relying on the exact order and implementation of your events, depending on no handlers being queued, etc. Should use other pattern when this is needed.
1 parent 0f76617 commit dbbfc62

5 files changed

Lines changed: 59 additions & 179 deletions

File tree

src/Illuminate/Contracts/Events/Dispatcher.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ interface Dispatcher
99
*
1010
* @param string|array $events
1111
* @param mixed $listener
12-
* @param int $priority
1312
* @return void
1413
*/
15-
public function listen($events, $listener, $priority = 0);
14+
public function listen($events, $listener);
1615

1716
/**
1817
* Determine if a given event has listeners.
@@ -39,15 +38,6 @@ public function push($event, $payload = []);
3938
*/
4039
public function subscribe($subscriber);
4140

42-
/**
43-
* Fire an event until the first non-null response is returned.
44-
*
45-
* @param string $event
46-
* @param array $payload
47-
* @return mixed
48-
*/
49-
public function until($event, $payload = []);
50-
5141
/**
5242
* Flush a set of pushed events.
5343
*
@@ -61,17 +51,9 @@ public function flush($event);
6151
*
6252
* @param string|object $event
6353
* @param mixed $payload
64-
* @param bool $halt
6554
* @return array|null
6655
*/
67-
public function dispatch($event, $payload = [], $halt = false);
68-
69-
/**
70-
* Get the event that is currently firing.
71-
*
72-
* @return string
73-
*/
74-
public function dispatching();
56+
public function dispatch($event, $payload = []);
7557

7658
/**
7759
* Remove a set of listeners from the dispatcher.

src/Illuminate/Events/Dispatcher.php

Lines changed: 41 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,6 @@ class Dispatcher implements DispatcherContract
3434
*/
3535
protected $wildcards = [];
3636

37-
/**
38-
* The sorted event listeners.
39-
*
40-
* @var array
41-
*/
42-
protected $sorted = [];
43-
44-
/**
45-
* The event firing stack.
46-
*
47-
* @var array
48-
*/
49-
protected $firing = [];
50-
5137
/**
5238
* The queue resolver instance.
5339
*
@@ -71,18 +57,15 @@ public function __construct(ContainerContract $container = null)
7157
*
7258
* @param string|array $events
7359
* @param mixed $listener
74-
* @param int $priority
7560
* @return void
7661
*/
77-
public function listen($events, $listener, $priority = 0)
62+
public function listen($events, $listener)
7863
{
7964
foreach ((array) $events as $event) {
8065
if (Str::contains($event, '*')) {
8166
$this->setupWildcardListen($event, $listener);
8267
} else {
83-
$this->listeners[$event][$priority][] = $this->makeListener($listener);
84-
85-
unset($this->sorted[$event]);
68+
$this->listeners[$event][] = $this->makeListener($event, $listener);
8669
}
8770
}
8871
}
@@ -96,7 +79,7 @@ public function listen($events, $listener, $priority = 0)
9679
*/
9780
protected function setupWildcardListen($event, $listener)
9881
{
99-
$this->wildcards[$event][] = $this->makeListener($listener);
82+
$this->wildcards[$event][] = $this->makeListener($event, $listener, true);
10083
}
10184

10285
/**
@@ -152,18 +135,6 @@ protected function resolveSubscriber($subscriber)
152135
return $subscriber;
153136
}
154137

155-
/**
156-
* Fire an event until the first non-null response is returned.
157-
*
158-
* @param string|object $event
159-
* @param array $payload
160-
* @return mixed
161-
*/
162-
public function until($event, $payload = [])
163-
{
164-
return $this->fire($event, $payload, true);
165-
}
166-
167138
/**
168139
* Flush a set of pushed events.
169140
*
@@ -175,48 +146,26 @@ public function flush($event)
175146
$this->fire($event.'_pushed');
176147
}
177148

178-
/**
179-
* Get the event that is currently firing.
180-
*
181-
* @return string
182-
*/
183-
public function dispatching()
184-
{
185-
return $this->firing();
186-
}
187-
188-
/**
189-
* Get the event that is currently firing.
190-
*
191-
* @return string
192-
*/
193-
public function firing()
194-
{
195-
return last($this->firing);
196-
}
197-
198149
/**
199150
* Fire an event and call the listeners.
200151
*
201152
* @param string|object $event
202153
* @param mixed $payload
203-
* @param bool $halt
204154
* @return array|null
205155
*/
206-
public function dispatch($event, $payload = [], $halt = false)
156+
public function dispatch($event, $payload = [])
207157
{
208-
return $this->fire($event, $payload, $halt);
158+
return $this->fire($event, $payload);
209159
}
210160

211161
/**
212162
* Fire an event and call the listeners.
213163
*
214164
* @param string|object $event
215165
* @param mixed $payload
216-
* @param bool $halt
217166
* @return array|null
218167
*/
219-
public function fire($event, $payload = [], $halt = false)
168+
public function fire($event, $payload = [])
220169
{
221170
// When the given "event" is actually an object we will assume it is an event
222171
// object and use the class as the event name and this event itself as the
@@ -225,25 +174,14 @@ public function fire($event, $payload = [], $halt = false)
225174
$event, $payload
226175
);
227176

228-
$responses = [];
229-
230-
$this->firing[] = $event;
231-
232177
if ($this->shouldBroadcast($payload)) {
233178
$this->broadcastEvent($payload[0]);
234179
}
235180

236-
foreach ($this->getListeners($event) as $listener) {
237-
$response = call_user_func_array($listener, $payload);
238-
239-
// If a response is returned from the listener and event halting is enabled
240-
// we will just return this response, and not call the rest of the event
241-
// listeners. Otherwise we will add the response on the response list.
242-
if (! is_null($response) && $halt) {
243-
array_pop($this->firing);
181+
$responses = [];
244182

245-
return $response;
246-
}
183+
foreach ($this->getListeners($event) as $listener) {
184+
$response = $listener($event, $payload);
247185

248186
// If a boolean false is returned from a listener, we will stop propagating
249187
// the event to any further listeners down in the chain, else we keep on
@@ -255,9 +193,7 @@ public function fire($event, $payload = [], $halt = false)
255193
$responses[] = $response;
256194
}
257195

258-
array_pop($this->firing);
259-
260-
return $halt ? null : $responses;
196+
return $responses;
261197
}
262198

263199
/**
@@ -306,13 +242,15 @@ protected function broadcastEvent($event)
306242
*/
307243
public function getListeners($eventName)
308244
{
309-
$wildcards = $this->getWildcardListeners($eventName);
245+
$listeners = isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : [];
310246

311-
if (! isset($this->sorted[$eventName])) {
312-
$this->sortListeners($eventName);
313-
}
247+
$listeners = array_merge(
248+
$listeners, $this->getWildcardListeners($eventName)
249+
);
314250

315-
return array_merge($this->sorted[$eventName], $wildcards);
251+
return class_exists($eventName, false)
252+
? $this->addInterfaceListeners($eventName, $listeners)
253+
: $listeners;
316254
}
317255

318256
/**
@@ -334,33 +272,6 @@ protected function getWildcardListeners($eventName)
334272
return $wildcards;
335273
}
336274

337-
/**
338-
* Sort the listeners for a given event by priority.
339-
*
340-
* @param string $eventName
341-
* @return void
342-
*/
343-
protected function sortListeners($eventName)
344-
{
345-
// If listeners exist for the given event, we will sort them by the priority
346-
// so that we can call them in the correct order. We will cache off these
347-
// sorted event listeners so we do not have to re-sort on every events.
348-
$listeners = isset($this->listeners[$eventName])
349-
? $this->listeners[$eventName] : [];
350-
351-
if (class_exists($eventName, false)) {
352-
$listeners = $this->addInterfaceListeners($eventName, $listeners);
353-
}
354-
355-
if ($listeners) {
356-
krsort($listeners);
357-
358-
$this->sorted[$eventName] = call_user_func_array('array_merge', $listeners);
359-
} else {
360-
$this->sorted[$eventName] = [];
361-
}
362-
}
363-
364275
/**
365276
* Add the listeners for the event's interfaces to the given array.
366277
*
@@ -372,12 +283,8 @@ protected function addInterfaceListeners($eventName, array $listeners = [])
372283
{
373284
foreach (class_implements($eventName) as $interface) {
374285
if (isset($this->listeners[$interface])) {
375-
foreach ($this->listeners[$interface] as $priority => $names) {
376-
if (isset($listeners[$priority])) {
377-
$listeners[$priority] = array_merge($listeners[$priority], $names);
378-
} else {
379-
$listeners[$priority] = $names;
380-
}
286+
foreach ($this->listeners[$interface] as $names) {
287+
$listeners = array_merge($listeners, (array) $names);
381288
}
382289
}
383290
}
@@ -391,9 +298,19 @@ protected function addInterfaceListeners($eventName, array $listeners = [])
391298
* @param string|\Closure $listener
392299
* @return mixed
393300
*/
394-
public function makeListener($listener)
301+
public function makeListener($event, $listener, $wildcard = false)
395302
{
396-
return is_string($listener) ? $this->createClassListener($listener) : $listener;
303+
if (is_string($listener)) {
304+
return $this->createClassListener($listener, $wildcard);
305+
}
306+
307+
return function ($event, $payload) use ($listener, $wildcard) {
308+
if ($wildcard) {
309+
return $listener($event, $payload);
310+
} else {
311+
return $listener(...array_values($payload));
312+
}
313+
};
397314
}
398315

399316
/**
@@ -402,12 +319,16 @@ public function makeListener($listener)
402319
* @param string $listener
403320
* @return \Closure
404321
*/
405-
public function createClassListener($listener)
322+
public function createClassListener($listener, $wildcard = false)
406323
{
407-
return function () use ($listener) {
408-
return call_user_func_array(
409-
$this->createClassCallable($listener), func_get_args()
410-
);
324+
return function ($event, $payload) use ($listener, $wildcard) {
325+
if ($wildcard) {
326+
return call_user_func($this->createClassCallable($listener), $event, $payload);
327+
} else {
328+
return call_user_func_array(
329+
$this->createClassCallable($listener), $payload
330+
);
331+
}
411332
};
412333
}
413334

@@ -533,7 +454,7 @@ public function forget($event)
533454
if (Str::contains($event, '*')) {
534455
unset($this->wildcards[$event]);
535456
} else {
536-
unset($this->listeners[$event], $this->sorted[$event]);
457+
unset($this->listeners[$event]);
537458
}
538459
}
539460

src/Illuminate/Support/Testing/Fakes/EventFake.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,9 @@ protected function mapEventArguments($arguments)
102102
*
103103
* @param string|array $events
104104
* @param mixed $listener
105-
* @param int $priority
106105
* @return void
107106
*/
108-
public function listen($events, $listener, $priority = 0)
107+
public function listen($events, $listener)
109108
{
110109
//
111110
}
@@ -144,18 +143,6 @@ public function subscribe($subscriber)
144143
//
145144
}
146145

147-
/**
148-
* Fire an event until the first non-null response is returned.
149-
*
150-
* @param string $event
151-
* @param array $payload
152-
* @return mixed
153-
*/
154-
public function until($event, $payload = [])
155-
{
156-
return $this->dispatch($event, $payload, true);
157-
}
158-
159146
/**
160147
* Flush a set of pushed events.
161148
*
@@ -172,26 +159,15 @@ public function flush($event)
172159
*
173160
* @param string|object $event
174161
* @param mixed $payload
175-
* @param bool $halt
176162
* @return array|null
177163
*/
178-
public function dispatch($event, $payload = [], $halt = false)
164+
public function dispatch($event, $payload = [])
179165
{
180166
$name = is_object($event) ? get_class($event) : (string) $event;
181167

182168
$this->events[$name][] = func_get_args();
183169
}
184170

185-
/**
186-
* Get the event that is currently dispatching.
187-
*
188-
* @return string
189-
*/
190-
public function dispatching()
191-
{
192-
//
193-
}
194-
195171
/**
196172
* Remove a set of listeners from the dispatcher.
197173
*

0 commit comments

Comments
 (0)