8000 [EventDispatcher] Fix TraceableEventDispatcher FC/BC layer · symfony/symfony@c5b3b34 · GitHub
[go: up one dir, main page]

Skip to content

Commit c5b3b34

Browse files
author
Robin Chalas
committed
[EventDispatcher] Fix TraceableEventDispatcher FC/BC layer
1 parent 27d10a6 commit c5b3b34

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1919
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2020
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
21+
use Symfony\Component\EventDispatcher\LegacyEventProxy;
2122
use Symfony\Component\HttpFoundation\RequestStack;
2223
use Symfony\Component\Stopwatch\Stopwatch;
2324
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
@@ -295,7 +296,7 @@ public function __call($method, $arguments)
295296
*/
296297
protected function beforeDispatch(string $eventName, $event)
297298
{
298-
$this->preDispatch($eventName, $event);
299+
$this->preDispatch($eventName, $event instanceof Event ? $event : new LegacyEventProxy($event));
299300
}
300301

301302
/**
@@ -305,7 +306,7 @@ protected function beforeDispatch(string $eventName, $event)
305306
*/
306307
protected function afterDispatch(string $eventName, $event)
307308
{
308-
$this->postDispatch($eventName, $event);
309+
$this->postDispatch($eventName, $event instanceof Event ? $event : new LegacyEventProxy($event));
309310
}
310311

311312
/**

src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Psr\EventDispatcher\StoppableEventInterface;
1515
use Symfony\Component\EventDispatcher\Event;
1616
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17-
use Symfony\Component\EventDispatcher\WrappedEvent;
17+
use Symfony\Component\EventDispatcher\LegacyEventProxy;
1818
use Symfony\Component\Stopwatch\Stopwatch;
1919
use Symfony\Component\VarDumper\Caster\ClassStub;
2020
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
@@ -112,8 +112,8 @@ public function getInfo($eventName)
112112

113113
public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher)
114114
{
115-
if ($event instanceof WrappedEvent) {
116-
$event = $event->getWrappedEvent();
115+
if ($event instanceof LegacyEventProxy) {
116+
$event = $event->getEvent();
117117
}
118118

119119
$dispatcher = $this->dispatcher ?: $dispatcher;

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\EventDispatcher;
1313

1414
use Psr\EventDispatcher\StoppableEventInterface;
15+
use Symfony\Component\EventDispatcher\Debug\WrappedListener;
1516
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
1617

1718
/**
@@ -242,7 +243,8 @@ protected function callListeners(iterable $listeners, string $eventName, $event)
242243
if ($stoppable && $event->isPropagationStopped()) {
243244
break;
244245
}
245-
$listener($event instanceof Event ? $event : new WrappedEvent($event), $eventName, $this);
246+
// @deprecated: the ternary operator is part of a BC layer and should be removed in 5.0
247+
$listener($listener instanceof WrappedListener ? new LegacyEventProxy($event) : $event, $eventName, $this);
246248
}
247249
}
248250

@@ -296,7 +298,7 @@ private function optimizeListeners(string $eventName): array
296298
($closure = \Closure::fromCallable($listener))(...$args);
297299
};
298300
} else {
299-
$closure = $listener instanceof \Closure ? $listener : \Closure::fromCallable($listener);
301+
$closure = $listener instanceof \Closure || $listener instanceof WrappedListener ? $listener : \Closure::fromCallable($listener);
300302
}
301303
}
302304
}

src/Symfony/Component/EventDispatcher/WrappedEvent.php renamed to src/Symfony/Component/EventDispatcher/LegacyEventProxy.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @internal to be removed in 5.0.
1919
*/
20-
final class WrappedEvent extends Event
20+
final class LegacyEventProxy extends Event
2121
{
2222
private $event;
2323

@@ -32,7 +32,7 @@ public function __construct($event)
3232
/**
3333
* @return object $event
3434
*/
35-
public function getWrappedEvent()
35+
public function getEvent()
3636
{
3737
return $this->event;
3838
}
@@ -54,4 +54,9 @@ public function stopPropagation()
5454

5555
$this->event->stopPropagation();
5656
}
57+
58+
public function __call($name, $args)
59+
{
60+
return $this->event->{$name}(...$args);
61+
}
5762
}

src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1919
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2020
use Symfony\Component\Stopwatch\Stopwatch;
21+
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
2122

2223
class TraceableEventDispatcherTest extends TestCase
2324
{
@@ -139,6 +140,19 @@ public function testClearCalledListeners()
139140
$this->assertEquals([['event' => 'foo', 'pretty' => 'closure', 'priority' => 5]], $listeners);
140141
}
141142

143+
public function testDispatchContractsEvent()
144+
{
145+
$expectedEvent = new ContractsEvent();
146+
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
147+
$tdispatcher->addListener('foo', function ($event) use ($expectedEvent) {
148+
$this->assertSame($event, $expectedEvent);
149+
}, 5);
150+
$tdispatcher->dispatch($expectedEvent, 'foo');
151+
152+
$listeners = $tdispatcher->getCalledListeners();
153+
$this->assertArrayHasKey('stub', $listeners[0]);
154+
}
155+
142156
public function testDispatchAfterReset()
143157
{
144158
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());

0 commit comments

Comments
 (0)
0