8000 [EventDispatcher] Fix FC layer (really) · symfony/symfony@2e02986 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e02986

Browse files
author
Robin Chalas
committed
[EventDispatcher] Fix FC layer (really)
1 parent 9669e13 commit 2e02986

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +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;
1718
use Symfony\Component\Stopwatch\Stopwatch;
1819
use Symfony\Component\VarDumper\Caster\ClassStub;
1920
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
@@ -111,6 +112,10 @@ public function getInfo($eventName)
111112

112113
public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher)
113114
{
115+
if ($event instanceof WrappedEvent) {
116+
$event = $event->getWrappedEvent();
117+
}
118+
114119
$dispatcher = $this->dispatcher ?: $dispatcher;
115120

116121
$this->called = true;

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function dispatch($event/*, string $eventName = null*/)
7171
}
7272

7373
if ($listeners) {
74-
$this->doDispatch($listeners, $eventName, $event);
74+
$this->callListeners($listeners, $eventName, $event);
7575
}
7676

7777
return $event;
@@ -242,7 +242,7 @@ protected function callListeners(iterable $listeners, string $eventName, $event)
242242
if ($stoppable && $event->isPropagationStopped()) {
243243
break;
244244
}
245-
$listener($event, $eventName, $this);
245+
$listener($event instanceof Event ? $event : new WrappedEvent($event), $eventName, $this);
246246
}
247247
}
248248

src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\EventDispatcher\Event;
1616
use Symfony\Component\EventDispatcher\EventDispatcher;
1717
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18+
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
1819

1920
class EventDispatcherTest extends TestCase
2021
{
@@ -128,6 +129,20 @@ public function testGetListenerPriority()
128129
}
129130

130131
public function testDispatch()
132+
{
133+
$this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
134+
$this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']);
135+
$this->dispatcher->dispatch(new ContractsEvent(), self::preFoo);
136+
$this->assertTrue($this->listener->preFooInvoked);
137+
$this->assertFalse($this->listener->postFooInvoked);
138+
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(new Event(), 'noevent'));
139+
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(new Event(), self::preFoo));
140+
$event = new Event();
141+
$return = $this->dispatcher->dispatch($event, self::preFoo);
142+
$this->assertSame($event, $return);
143+
}
144+
145+
public function testDispatchContractsEvent()
131146
{
132147
$this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
133148
$this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']);
@@ -413,12 +428,12 @@ class TestEventListener
413428

414429
/* Listener methods */
415430

416-
public function preFoo(Event $e)
431+
public function preFoo($e)
417432
{
418433
$this->preFooInvoked = true;
419434
}
420435

421-
public function postFoo(Event $e)
436+
public function postFoo($e)
422437
{
423438
$this->postFooInvoked = true;
424439

@@ -433,7 +448,7 @@ class TestWithDispatcher
433448
public $name;
434449
public $dispatcher;
435450

436-
public function foo(Event $e, $name, $dispatcher)
451+
public function foo($e, $name, $dispatcher)
437452
{
438453
$this->name = $name;
439454
$this->dispatcher = $dispatcher;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\EventDispatcher;
13+
14+
use Psr\EventDispatcher\StoppableEventInterface;
15+
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
16+
17+
/**
18+
* @internal to be removed in 5.0.
19+
*/
20+
final class WrappedEvent extends Event
21+
{
22+
private $event;
23+
24+
/**
25+
* @param object $event
26+
*/
27+
public function __construct($event)
28+
{
29+
$this->event = $event;
30+
}
31+
32+
/**
33+
* @return object $event
34+
*/
35+
public function getWrappedEvent()
36+
{
37+
return $this->event;
38+
}
39+
40+
public function isPropagationStopped()
41+
{
42+
if (!$this->event instanceof ContractsEvent && !$this->event instanceof StoppableEventInterface) {
43+
return false;
44+
}
45+
46+
return $this->event->isPropagationStopped();
47+
}
48+
49+
public function stopPropagation()
50+
{
51+
if (!$this->event instanceof ContractsEvent) {
52+
return;
53+
}
54+
55+
$this->event->stopPropagation();
56+
}
57+
}

0 commit comments

Comments
 (0)
0