Skip to content

Commit b80ddf4

Browse files
committed
handle array callbacks
1 parent 058d92f commit b80ddf4

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/Illuminate/Events/Dispatcher.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ protected function addInterfaceListeners($eventName, array $listeners = [])
365365
*/
366366
public function makeListener($listener, $wildcard = false)
367367
{
368-
if (is_string($listener)) {
368+
if (is_string($listener) || is_array($listener)) {
369369
return $this->createClassListener($listener, $wildcard);
370370
}
371371

@@ -381,7 +381,7 @@ public function makeListener($listener, $wildcard = false)
381381
/**
382382
* Create a class based listener using the IoC container.
383383
*
384-
* @param string $listener
384+
* @param string|array $listener
385385
* @param bool $wildcard
386386
* @return \Closure
387387
*/
@@ -401,12 +401,12 @@ public function createClassListener($listener, $wildcard = false)
401401
/**
402402
* Create the class based event callable.
403403
*
404-
* @param string $listener
404+
* @param string|array $listener
405405
* @return callable
406406
*/
407407
protected function createClassCallable($listener)
408408
{
409-
[$class, $method] = $this->parseClassCallable($listener);
409+
[$class, $method] = is_array($listener) ? $listener : $this->parseClassCallable($listener);
410410

411411
if ($this->handlerShouldBeQueued($class)) {
412412
return $this->createQueuedHandlerCallable($class, $method);

tests/Events/EventsDispatcherTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,16 @@ public function testClassesWork()
327327
$this->assertSame('baz', $_SERVER['__event.test']);
328328
}
329329

330+
public function testArrayCallbackListenersAreHandled()
331+
{
332+
unset($_SERVER['__event.ExampleListener']);
333+
$d = new Dispatcher;
334+
$d->listen(ExampleEvent::class, [ExampleListener::class, 'hear']);
335+
$d->dispatch(new ExampleEvent);
336+
337+
$this->assertTrue($_SERVER['__event.ExampleListener']);
338+
}
339+
330340
public function testEventClassesArePayload()
331341
{
332342
unset($_SERVER['__event.test']);
@@ -390,3 +400,11 @@ class AnotherEvent implements SomeEventInterface
390400
{
391401
//
392402
}
403+
404+
class ExampleListener
405+
{
406+
public function hear()
407+
{
408+
$_SERVER['__event.ExampleListener'] = true;
409+
}
410+
}

0 commit comments

Comments
 (0)