Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ public function listen($events, $listener = null, $priority = 0)
} elseif ($listener instanceof QueuedClosure) {
$listener = $listener->resolve();
}
$listener = Serialization::wrapClosure($listener);

foreach ((array) $events as $event) {
if (Str::contains($event, '*')) {
$this->setupWildcardListen($event, $listener);
$this->setupWildcardListen($event, Serialization::wrapClosure($listener));
} else {
$this->listeners[$event][$priority][] = $this->makeListener($listener);

Expand All @@ -61,6 +60,14 @@ public function listen($events, $listener = null, $priority = 0)
}
}

// Serialize the listener created by laravel
public function makeListener($listener, $wildcard = false)
{
$listener = parent::makeListener($listener, $wildcard);

return Serialization::wrapClosure($listener);
}

/**
* Get the event that is currently firing.
*
Expand Down Expand Up @@ -127,9 +134,6 @@ public function dispatch($event, $payload = [], $halt = false)
}

foreach ($this->getListeners($event) as $listener) {
if ($listener instanceof SerializableClosure) {
$listener = $listener->getClosure();
}
$response = $listener($event, $payload);

// If a response is returned from the listener and event halting is enabled
Expand Down
26 changes: 26 additions & 0 deletions tests/Events/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ public function testTypedClosureListen()
$this->assertTrue($magic_value);
}

public function testClosureWithValueArgument()
{
$original = false;

$dispatcher = new Dispatcher();
$dispatcher->listen('test', function ($value) {
$value = true;
});
$dispatcher->dispatch('test', [$original]);

$this->assertFalse($original);
}

public function testClosureWithReferenceArgument()
{
$original = false;

$dispatcher = new Dispatcher();
$dispatcher->listen('test', function (&$value) {
$value = true;
});
$dispatcher->dispatch('test', [&$original]);

$this->assertTrue($original);
}

public function testStringEventPriorities()
{
$magic_value = 0;
Expand Down