Skip to content

Commit b2eb39b

Browse files
authored
Dispatcher changed the order of params (#20)
1 parent 2d4ed4e commit b2eb39b

File tree

7 files changed

+129
-39
lines changed

7 files changed

+129
-39
lines changed

AsyncEventDispatcherInterface.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,13 @@ interface AsyncEventDispatcherInterface extends EventDispatcherInterface
2727
/**
2828
* Dispatch an event asynchronously.
2929
*
30-
* @param string $eventName
3130
* @param Event $event
31+
* @param string $eventName
3232
*
3333
* @return PromiseInterface
3434
*/
3535
public function asyncDispatch(
36-
string $eventName,
37-
Event $event
38-
);
39-
40-
/**
41-
* Triggers the listeners of an event.
42-
*
43-
* This method can be overridden to add functionality that is executed
44-
* for each listener.
45-
*
46-
* @param callable[] $listeners
47-
* @param string $eventName
48-
* @param Event $event
49-
*
50-
* @return PromiseInterface
51-
*/
52-
public function doAsyncDispatch(
53-
array $listeners,
54-
string $eventName,
55-
Event $event
36+
Event $event,
37+
string $eventName
5638
);
5739
}

AsyncEventDispatcherMethods.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ trait AsyncEventDispatcherMethods
2727
/**
2828
* Dispatch an event asynchronously.
2929
*
30-
* @param string $eventName
3130
* @param Event $event
31+
* @param string $eventName
3232
*
3333
* @return PromiseInterface
3434
*/
3535
public function asyncDispatch(
36-
string $eventName,
37-
Event $event
36+
Event $event,
37+
string $eventName = null
3838
) {
39+
$eventName = $eventName ?? \get_class($event);
40+
3941
if ($listeners = $this->getListeners($eventName)) {
4042
return $this->doAsyncDispatch($listeners, $eventName, $event);
4143
}
@@ -55,7 +57,7 @@ public function asyncDispatch(
5557
*
5658
* @return PromiseInterface
5759
*/
58-
public function doAsyncDispatch(
60+
private function doAsyncDispatch(
5961
array $listeners,
6062
string $eventName,
6163
Event $event

AsyncHttpKernel.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use React\Promise\FulfilledPromise;
2222
use React\Promise\PromiseInterface;
2323
use RingCentral\Psr7\Response as Psr7Response;
24-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2524
use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
2625
use Symfony\Component\HttpFoundation\Request;
2726
use Symfony\Component\HttpFoundation\RequestStack;
@@ -42,6 +41,7 @@
4241
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
4342
use Symfony\Component\HttpKernel\HttpKernel;
4443
use Symfony\Component\HttpKernel\KernelEvents;
44+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
4545
use Throwable;
4646

4747
/**
@@ -101,7 +101,7 @@ public function preload(): PromiseInterface
101101
{
102102
return $this
103103
->dispatcher
104-
->asyncDispatch(AsyncKernelEvents::PRELOAD, new PreloadEvent());
104+
->asyncDispatch(new PreloadEvent(), AsyncKernelEvents::PRELOAD);
105105
}
106106

107107
/**
@@ -144,7 +144,7 @@ private function handleAsyncRaw(Request $request): PromiseInterface
144144
$event = new RequestEvent($this, $request, $type);
145145

146146
return $dispatcher
147-
->asyncDispatch(KernelEvents::REQUEST, $event)
147+
->asyncDispatch($event, KernelEvents::REQUEST)
148148
->then(function (RequestEvent $event) use ($request, $type) {
149149
return $event->hasResponse()
150150
? $this->filterResponsePromise(
@@ -225,7 +225,7 @@ private function callAsyncView(
225225

226226
return $this
227227
->dispatcher
228-
->asyncDispatch(KernelEvents::VIEW, $event)
228+
->asyncDispatch($event, KernelEvents::VIEW)
229229
->then(function (ViewEvent $event) use ($controller, $response) {
230230
if ($event->hasResponse()) {
231231
return $event->getResponse();
@@ -259,7 +259,7 @@ private function filterResponsePromise(Response $response, Request $request, int
259259

260260
return $this
261261
->dispatcher
262-
->asyncDispatch(KernelEvents::RESPONSE, $event)
262+
->asyncDispatch($event, KernelEvents::RESPONSE)
263263
->then(function (ResponseEvent $event) use ($request, $type) {
264264
$this->finishRequestPromise($request, $type);
265265

@@ -310,7 +310,7 @@ private function handleExceptionPromise(
310310

311311
return $this
312312
->dispatcher
313-
->asyncDispatch(KernelEvents::EXCEPTION, $event)
313+
->asyncDispatch($event, KernelEvents::EXCEPTION)
314314
->then(function (ExceptionEvent $event) use ($request, $type) {
315315
// Supporting both 4.3 and 5.0
316316
$throwable = ($event instanceof GetResponseForExceptionEvent)

DependencyInjection/CompilerPass/EventDispatcherCompilerPass.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public function __construct(bool $isDebug)
5151
public function process(ContainerBuilder $container)
5252
{
5353
if ($container->has('event_dispatcher')) {
54-
$this->isDebug
54+
$this->isDebug &&
55+
$container->hasDefinition('debug.event_dispatcher')
5556
? $this->processEventDispatcherDebug($container)
5657
: $this->processEventDispatcher($container);
5758
}
@@ -78,16 +79,10 @@ private function processEventDispatcher(ContainerBuilder $container)
7879
*/
7980
private function processEventDispatcherDebug(ContainerBuilder $container)
8081
{
81-
if (!$container->hasDefinition('debug.event_dispatcher')) {
82-
$this->processEventDispatcher($container);
83-
84-
return;
85-
}
86-
8782
$container
8883
->getDefinition('debug.event_dispatcher')
8984
->setClass(TraceableAsyncEventDispatcher::class);
9085

91-
$container->setAlias(AsyncEventDispatcherInterface::class, 'event_dispatcher');
86+
$container->setAlias(AsyncEventDispatcherInterface::class, 'debug.event_dispatcher');
9287
}
9388
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DriftPHP Project
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <yuhu@mmoreram.com>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Drift\HttpKernel\Tests\Base;
17+
18+
use Clue\React\Block;
19+
use Drift\HttpKernel\AsyncEventDispatcherInterface;
20+
use Drift\HttpKernel\Tests\AsyncKernelFunctionalTest;
21+
use Drift\HttpKernel\Tests\Event\Event1;
22+
use Drift\HttpKernel\Tests\Listener;
23+
use React\EventLoop\Factory;
24+
25+
/**
26+
* Class AsyncEventDispatcherTest.
27+
*/
28+
class AsyncEventDispatcherTest extends AsyncKernelFunctionalTest
29+
{
30+
/**
31+
* Decorate configuration.
32+
*
33+
* @param array $configuration
34+
*
35+
* @return array
36+
*/
37+
protected static function decorateConfiguration(array $configuration): array
38+
{
39+
$configuration = parent::decorateConfiguration($configuration);
40+
$configuration['services']['dispatcher_test'] = [
41+
'alias' => AsyncEventDispatcherInterface::class,
42+
];
43+
44+
$configuration['services']['listener'] = [
45+
'class' => Listener::class,
46+
'tags' => [
47+
[
48+
'name' => 'kernel.event_listener',
49+
'event' => Event1::class,
50+
'method' => 'handleEvent1',
51+
],
52+
],
53+
];
54+
55+
return $configuration;
56+
}
57+
58+
/**
59+
* Test async event dispatcher.
60+
*/
61+
public function testAsyncDispatch()
62+
{
63+
$loop = Factory::create();
64+
65+
$_GET['event1'] = false;
66+
$promise = self::get('dispatcher_test')
67+
->asyncDispatch(new Event1())
68+
->then(function (Event1 $_) {
69+
$this->assertTrue($_GET['event1']);
70+
});
71+
72+
$loop->run();
73+
Block\await($promise, $loop);
74+
}
75+
}

Tests/Event/Event1.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DriftPHP Project
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <yuhu@mmoreram.com>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Drift\HttpKernel\Tests\Event;
17+
18+
use Symfony\Contracts\EventDispatcher\Event;
19+
20+
/**
21+
* Class Event1.
22+
*/
23+
class Event1 extends Event
24+
{
25+
}

Tests/Listener.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace Drift\HttpKernel\Tests;
1717

1818
use Drift\HttpKernel\Event\PreloadEvent;
19+
use Drift\HttpKernel\Tests\Event\Event1;
1920
use React\Promise\FulfilledPromise;
2021
use React\Promise\PromiseInterface;
2122
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -150,4 +151,14 @@ public function handlePreload(PreloadEvent $event)
150151
{
151152
$_GET['preloaded'] = true;
152153
}
154+
155+
/**
156+
* Handle event1.
157+
*
158+
* @param Event1 $event1
159+
*/
160+
public function handleEvent1(Event1 $event1)
161+
{
162+
$_GET['event1'] = true;
163+
}
153164
}

0 commit comments

Comments
 (0)