Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc5b3b34

Browse files
author
Robin Chalas
committed
[EventDispatcher] Fix TraceableEventDispatcher FC/BC layer
1 parent27d10a6 commitc5b3b34

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
useSymfony\Component\EventDispatcher\EventDispatcherInterface;
1919
useSymfony\Component\EventDispatcher\EventSubscriberInterface;
2020
useSymfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
21+
useSymfony\Component\EventDispatcher\LegacyEventProxy;
2122
useSymfony\Component\HttpFoundation\RequestStack;
2223
useSymfony\Component\Stopwatch\Stopwatch;
2324
useSymfony\Contracts\EventDispatcher\EventasContractsEvent;
@@ -295,7 +296,7 @@ public function __call($method, $arguments)
295296
*/
296297
protectedfunctionbeforeDispatch(string$eventName,$event)
297298
{
298-
$this->preDispatch($eventName,$event);
299+
$this->preDispatch($eventName,$eventinstanceof Event ?$event :newLegacyEventProxy($event));
299300
}
300301

301302
/**
@@ -305,7 +306,7 @@ protected function beforeDispatch(string $eventName, $event)
305306
*/
306307
protectedfunctionafterDispatch(string$eventName,$event)
307308
{
308-
$this->postDispatch($eventName,$event);
309+
$this->postDispatch($eventName,$eventinstanceof Event ?$event :newLegacyEventProxy($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
usePsr\EventDispatcher\StoppableEventInterface;
1515
useSymfony\Component\EventDispatcher\Event;
1616
useSymfony\Component\EventDispatcher\EventDispatcherInterface;
17-
useSymfony\Component\EventDispatcher\WrappedEvent;
17+
useSymfony\Component\EventDispatcher\LegacyEventProxy;
1818
useSymfony\Component\Stopwatch\Stopwatch;
1919
useSymfony\Component\VarDumper\Caster\ClassStub;
2020
useSymfony\Contracts\EventDispatcher\EventasContractsEvent;
@@ -112,8 +112,8 @@ public function getInfo($eventName)
112112

113113
publicfunction__invoke(Event$event,$eventName,EventDispatcherInterface$dispatcher)
114114
{
115-
if ($eventinstanceofWrappedEvent) {
116-
$event =$event->getWrappedEvent();
115+
if ($eventinstanceofLegacyEventProxy) {
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
namespaceSymfony\Component\EventDispatcher;
1313

1414
usePsr\EventDispatcher\StoppableEventInterface;
15+
useSymfony\Component\EventDispatcher\Debug\WrappedListener;
1516
useSymfony\Contracts\EventDispatcher\EventasContractsEvent;
1617

1718
/**
@@ -242,7 +243,8 @@ protected function callListeners(iterable $listeners, string $eventName, $event)
242243
if ($stoppable &&$event->isPropagationStopped()) {
243244
break;
244245
}
245-
$listener($eventinstanceof Event ?$event :newWrappedEvent($event),$eventName,$this);
246+
// @deprecated: the ternary operator is part of a BC layer and should be removed in 5.0
247+
$listener($listenerinstanceof WrappedListener ?newLegacyEventProxy($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 =$listenerinstanceof \Closure ?$listener : \Closure::fromCallable($listener);
301+
$closure =$listenerinstanceof \Closure||$listenerinstanceof 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-
finalclassWrappedEventextends Event
20+
finalclassLegacyEventProxyextends Event
2121
{
2222
private$event;
2323

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

5555
$this->event->stopPropagation();
5656
}
57+
58+
publicfunction__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
useSymfony\Component\EventDispatcher\EventDispatcherInterface;
1919
useSymfony\Component\EventDispatcher\EventSubscriberInterface;
2020
useSymfony\Component\Stopwatch\Stopwatch;
21+
useSymfony\Contracts\EventDispatcher\EventasContractsEvent;
2122

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

143+
publicfunctiontestDispatchContractsEvent()
144+
{
145+
$expectedEvent =newContractsEvent();
146+
$tdispatcher =newTraceableEventDispatcher(newEventDispatcher(),newStopwatch());
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
publicfunctiontestDispatchAfterReset()
143157
{
144158
$tdispatcher =newTraceableEventDispatcher(newEventDispatcher(),newStopwatch());

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp