Skip to content

Commit

Permalink
fix: event dispatch with class name and arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pr3d4dor committed Aug 19, 2024
1 parent 81a0b5b commit 18d9057
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ protected function parseEventAndPayload($event, $payload)
{
if (is_object($event)) {
[$payload, $event] = [[$event], get_class($event)];
} elseif (class_exists($event)) {
[$payload, $event] = [[new $event(...$payload)], $event];
}

return [$event, Arr::wrap($payload)];
Expand Down
31 changes: 31 additions & 0 deletions tests/Events/EventsDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,21 @@ public function testInvokeIsCalled()

unset($_SERVER['__event.test']);
}

public function testDispatchWithObjectAndClassWithArguments()
{
unset($_SERVER['__event.test']);
$d = new Dispatcher;
$d->listen(TestEvent1::class, [TestListener4::class, 'handle']);

$test = 'hello';

$d->dispatch(new TestEvent1($test));
$this->assertSame($test, $_SERVER['__event.test']);

$d->dispatch(TestEvent1::class, [$test]);
$this->assertSame($test, $_SERVER['__event.test']);
}
}

class TestListenerLean
Expand Down Expand Up @@ -726,3 +741,19 @@ public function handle()
$_SERVER['__event.test'][] = 'handle-3';
}
}

class TestEvent1
{
public function __construct(
public string $test
) {
}
}

class TestListener4
{
public function handle(TestEvent1 $exampleEvent)
{
$_SERVER['__event.test'] = $exampleEvent->test;
}
}

0 comments on commit 18d9057

Please sign in to comment.