Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow multiple events to be dispatched at once #48631

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions src/Illuminate/Contracts/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public function until($event, $payload = []);
*/
public function dispatch($event, $payload = [], $halt = false);

/**
* Dispatch multiple events and call their listeners.
*
* @param array<int|string, mixed> $events
* @return void
*/
public function dispatchMultiple($events);
mateusjatenee marked this conversation as resolved.
Show resolved Hide resolved

/**
* Register an event and payload to be fired later.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Illuminate/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ public function dispatch($event, $payload = [], $halt = false)
return $halt ? null : $responses;
}

/**
* Dispatch multiple events and call their listeners.
*
* @param array<int|string, mixed> $events
* @return void
*/
public function dispatchMultiple($events)
{
foreach ($events as $event => $payload) {
// If the key is an integer, we can assume we are dealing with an array and not a hashmap.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we care if there are string keys?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To support something similar to the dispatch('key', 'payload') syntax. e.g:

Event::dispatchMany([
    'event' => [1, 2, 3]
]);

if (is_int($event)) {
// The payload is the event itself.
$this->dispatch($payload);

continue;
}

$this->dispatch($event, $payload);
}
}

/**
* Parse the given event and payload and prepare them for dispatching.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Events/NullDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ public function dispatch($event, $payload = [], $halt = false)
//
}

/**
* Don't fire multiple events.
*
* @param array<int|string, mixed> $events
* @return void
*/
public function dispatchMultiple($events)
{
//
}

/**
* Don't register an event and payload to be fired later.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @method static void subscribe(object|string $subscriber)
* @method static mixed until(string|object $event, mixed $payload = [])
* @method static array|null dispatch(string|object $event, mixed $payload = [], bool $halt = false)
* @method static void dispatchMultiple(array $events)
* @method static array getListeners(string $eventName)
* @method static \Closure makeListener(\Closure|string|array $listener, bool $wildcard = false)
* @method static \Closure createClassListener(string $listener, bool $wildcard = false)
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/EventFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,26 @@ public function dispatch($event, $payload = [], $halt = false)
}
}

/**
* Dispatch multiple events and call their listeners.
*
* @param array<int|string, mixed> $events
* @return void
*/
public function dispatchMultiple($events)
{
foreach ($events as $event => $payload) {
if (is_int($event)) {
// The payload is the event itself.
$this->dispatch($payload);

continue;
}

$this->dispatch($event, $payload);
}
}

/**
* Determine if an event should be faked or actually dispatched.
*
Expand Down
53 changes: 53 additions & 0 deletions tests/Events/EventsDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,59 @@ public function testBasicEventExecution()
$this->assertSame('barbar', $_SERVER['__event.test']);
}

public function testMultipleEventsCanBeDispatchedArray()
{
unset($_SERVER['__event.test_foo']);
unset($_SERVER['__event.test_bar']);
$d = new Dispatcher;
$d->listen('foo', fn ($foo) => $_SERVER['__event.test_foo'] = $foo);
$d->listen('bar', fn ($bar) => $_SERVER['__event.test_bar'] = $bar);
$d->dispatchMultiple([
'foo' => 'bar',
'bar' => 'baz',
]);

$this->assertSame('bar', $_SERVER['__event.test_foo']);
$this->assertSame('baz', $_SERVER['__event.test_bar']);
}

public function testMultipleEventsCanBeDispatchedArrayNoPayload()
{
unset($_SERVER['__event.test_foo']);
unset($_SERVER['__event.test_bar']);
$d = new Dispatcher;
$d->listen('foo', fn () => $_SERVER['__event.test_foo'] = 1);
$d->listen('bar', fn () => $_SERVER['__event.test_bar'] = 2);
$d->dispatchMultiple([
'foo',
'bar'
]);

$this->assertSame(1, $_SERVER['__event.test_foo']);
$this->assertSame(2, $_SERVER['__event.test_bar']);
}

public function testMultipleEventsCanBeDispatchedObject()
{
unset($_SERVER['__event.test_foo']);
unset($_SERVER['__event.test_bar']);
$d = new Dispatcher;

$d->listen(ExampleEvent::class, function () {
$_SERVER['__event.test_foo'] = 'one';
});
$d->listen(AnotherEvent::class, function () {
$_SERVER['__event.test_bar'] = 'two';
});
$d->dispatchMultiple([
new ExampleEvent,
new AnotherEvent
]);

$this->assertSame('one', $_SERVER['__event.test_foo']);
$this->assertSame('two', $_SERVER['__event.test_bar']);
}

public function testHaltingEventExecution()
{
unset($_SERVER['__event.test']);
Expand Down
41 changes: 41 additions & 0 deletions tests/Integration/Events/EventFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,42 @@ public function testEventsListedInExceptAreProperlyDispatched()
$this->assertEquals(['important'], Event::dispatch('important-event'));
}

public function testEventsAreFakedWhenMultipleDispatchedObject()
{
Event::fake();

Event::dispatchMultiple([new NonImportantEvent(), new AnotherEvent()]);

Event::assertDispatched(NonImportantEvent::class);
Event::assertDispatched(AnotherEvent::class);
}

public function testEventsAreFakedWhenMultipleDispatchedArray()
{
Event::fake();

Event::dispatchMultiple([
NonImportantEvent::class => 'foo',
AnotherEvent::class => 'bar'
]);

Event::assertDispatched(NonImportantEvent::class);
Event::assertDispatched(AnotherEvent::class);
}

public function testEventsAreFakedWhenMultipleDispatchedArrayNoPayload()
{
Event::fake();

Event::dispatchMultiple([
NonImportantEvent::class,
AnotherEvent::class
]);

Event::assertDispatched(NonImportantEvent::class);
Event::assertDispatched(AnotherEvent::class);
}

public function testAssertListening()
{
Event::fake();
Expand Down Expand Up @@ -201,6 +237,11 @@ class NonImportantEvent
//
}

class AnotherEvent
{

}

class PostEventSubscriber
{
public function handlePostCreated($event)
Expand Down