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

Make sure EventDispatcherAggregate calls async and regular events #57

Merged
merged 1 commit into from
Nov 17, 2023
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
* Drop support for PHP 8.1

### Fixed
* *Nothing*
* Fix EventDispatcherAggregate, making sure it dispatches both regular and async listeners if the event is registered for both


## [3.0.0] - 2023-05-23
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ return [
],
],

'fallback_async_to_regular' => true, // Defaults to false

],

];
Expand All @@ -70,7 +68,6 @@ The `events` config entry has these blocks.

* `regular`: A list of events with all the listeners tha should be dispatched synchronously for each one of them.
* `async`: A list of events with all the listeners that should be executed as RoadRunner jobs or openswoole tasks for each one of them.
* `fallback_async_to_regular`: Tells if async event listeners should be dispatched as regular ones in case neither RoadRunner nor openswoole are not installed. It is false by default.

In both cases, listeners are identified by their service name, making the services to be lazily resolved only when their corresponding event gets dispatched.

Expand Down
17 changes: 11 additions & 6 deletions src/Dispatcher/EventDispatcherAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,34 @@
use Psr\EventDispatcher\EventDispatcherInterface;

use function array_keys;
use function get_class;
use function in_array;

class EventDispatcherAggregate implements EventDispatcherInterface
{
private array $asyncEvents;
private bool $fallbackAsync;
private array $regularEvents;

public function __construct(
private readonly EventDispatcherInterface $asyncDispatcher,
private readonly EventDispatcherInterface $regularDispatcher,
array $eventsConfig,
) {
$this->asyncEvents = array_keys($eventsConfig['async'] ?? []);
$this->fallbackAsync = (bool) ($eventsConfig['fallback_async_to_regular'] ?? false);
$this->regularEvents = array_keys($eventsConfig['regular'] ?? []);
}

public function dispatch(object $event): object
{
if (! $this->fallbackAsync && in_array(get_class($event), $this->asyncEvents, true)) {
return $this->asyncDispatcher->dispatch($event);
$initialEventClass = $event::class;

if (in_array($initialEventClass, $this->regularEvents, true)) {
$event = $this->regularDispatcher->dispatch($event);
}

if (in_array($initialEventClass, $this->asyncEvents, true)) {
$event = $this->asyncDispatcher->dispatch($event);
}

return $this->regularDispatcher->dispatch($event);
return $event;
}
}
2 changes: 1 addition & 1 deletion src/Dispatcher/EventDispatcherAggregateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __invoke(ContainerInterface $container): EventDispatcherAggregat
}

/**
* @deprecated
* @deprecated Use $container->get(RoadRunnerEventDispatcherFactory::ROAD_RUNNER_DISPATCHER) instead
*/
private function resolveAsyncDispatcher(ContainerInterface $container): EventDispatcherInterface
{
Expand Down
4 changes: 0 additions & 4 deletions src/Dispatcher/SyncEventDispatcherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ public function __invoke(ContainerInterface $container): EventDispatcher
{
$provider = new PrioritizedListenerRegistry();
$eventsConfig = $container->get('config')['events'] ?? [];
$fallback = $eventsConfig['fallback_async_to_regular'] ?? false;

$this->registerEvents($provider, $container, $eventsConfig['regular'] ?? []);
if ($fallback) {
$this->registerEvents($provider, $container, $eventsConfig['async'] ?? []);
}

return new EventDispatcher($provider);
}
Expand Down
27 changes: 8 additions & 19 deletions test/Dispatcher/EventDispatcherAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,31 @@ public function expectedDispatcherIsInvoked(array $eventsConfig, int $asyncCalls

public static function provideEventsConfigs(): iterable
{
yield 'no async events' => [[], 0, 1];
yield 'no events' => [[], 0, 0];
yield 'async events' => [
['async' => [
stdClass::class => [],
]],
1,
0,
];
yield 'async events with fallback' => [
[
'async' => [
stdClass::class => [],
],
'fallback_async_to_regular' => true,
],
yield 'regular events' => [
['regular' => [
stdClass::class => [],
]],
0,
1,
];
yield 'async events with falsy fallback' => [
yield 'both events' => [
[
'async' => [
'regular' => [
stdClass::class => [],
],
'fallback_async_to_regular' => 0,
],
1,
0,
];
yield 'async events with truthy fallback' => [
[
'async' => [
stdClass::class => [],
],
'fallback_async_to_regular' => 'true',
],
0,
1,
1,
];
}
Expand Down
22 changes: 0 additions & 22 deletions test/Dispatcher/SyncEventDispatcherFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,6 @@ static function (ListenerProviderInterface $provider): void {
Assert::assertEmpty([...$provider->getListenersForEvent(new Event())]);
},
];
yield 'non-empty regular events and async with fallback' => [
['events' => [
'regular' => [
stdClass::class => [
'foo',
'bar',
],
],
'async' => [
Event::class => [
'foo',
'bar',
'baz',
],
],
'fallback_async_to_regular' => true,
]],
static function (ListenerProviderInterface $provider): void {
Assert::assertCount(2, [...$provider->getListenersForEvent(new stdClass())]);
Assert::assertCount(3, [...$provider->getListenersForEvent(new Event())]);
},
];
}

#[Test]
Expand Down