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

[EventDispatcher] Fix TraceableEventDispatcher FC/BC layer#31254

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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
[EventDispatcher] Fix TraceableEventDispatcher FC/BC layer
  • Loading branch information
Robin Chalas committedApr 27, 2019
commitc5b3b34b51a07e361d0eb3cf44fa8a2e01224329
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,6 +18,7 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\EventDispatcher\LegacyEventProxy;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
Expand DownExpand Up@@ -295,7 +296,7 @@ public function __call($method, $arguments)
*/
protected function beforeDispatch(string $eventName, $event)
{
$this->preDispatch($eventName, $event);
$this->preDispatch($eventName, $event instanceof Event ? $event : new LegacyEventProxy($event));
}

/**
Expand All@@ -305,7 +306,7 @@ protected function beforeDispatch(string $eventName, $event)
*/
protected function afterDispatch(string $eventName, $event)
{
$this->postDispatch($eventName, $event);
$this->postDispatch($eventName, $event instanceof Event ? $event : new LegacyEventProxy($event));
}

/**
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,7 @@
use Psr\EventDispatcher\StoppableEventInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\WrappedEvent;
use Symfony\Component\EventDispatcher\LegacyEventProxy;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\VarDumper\Caster\ClassStub;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
Expand DownExpand Up@@ -112,8 +112,8 @@ public function getInfo($eventName)

public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher)
{
if ($event instanceofWrappedEvent) {
$event = $event->getWrappedEvent();
if ($event instanceofLegacyEventProxy) {
$event = $event->getEvent();
}

$dispatcher = $this->dispatcher ?: $dispatcher;
Expand Down
6 changes: 4 additions & 2 deletionssrc/Symfony/Component/EventDispatcher/EventDispatcher.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,7 @@
namespace Symfony\Component\EventDispatcher;

use Psr\EventDispatcher\StoppableEventInterface;
use Symfony\Component\EventDispatcher\Debug\WrappedListener;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;

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

Expand DownExpand Up@@ -296,7 +298,7 @@ private function optimizeListeners(string $eventName): array
($closure = \Closure::fromCallable($listener))(...$args);
};
} else {
$closure = $listener instanceof \Closure ? $listener : \Closure::fromCallable($listener);
$closure = $listener instanceof \Closure|| $listener instanceof WrappedListener? $listener : \Closure::fromCallable($listener);
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

WrappedListener handles this optimization internally

}
}
}
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,7 +17,7 @@
/**
* @internal to be removed in 5.0.
*/
final classWrappedEvent extends Event
final classLegacyEventProxy extends Event
{
private $event;

Expand All@@ -32,7 +32,7 @@ public function __construct($event)
/**
* @return object $event
*/
public functiongetWrappedEvent()
public functiongetEvent()
{
return $this->event;
}
Expand All@@ -54,4 +54,9 @@ public function stopPropagation()

$this->event->stopPropagation();
}

public function __call($name, $args)
{
return $this->event->{$name}(...$args);
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,6 +18,7 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;

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

public function testDispatchContractsEvent()
{
$expectedEvent = new ContractsEvent();
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
$tdispatcher->addListener('foo', function ($event) use ($expectedEvent) {
$this->assertSame($event, $expectedEvent);
}, 5);
$tdispatcher->dispatch($expectedEvent, 'foo');

$listeners = $tdispatcher->getCalledListeners();
$this->assertArrayHasKey('stub', $listeners[0]);
}

public function testDispatchAfterReset()
{
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp