minor #31709 [EventDispatcher] Remove deprecation layer (derrabus)

This PR was merged into the 5.0-dev branch.

Discussion
----------

[EventDispatcher] Remove deprecation layer

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

This PR removes all deprecations from the event dispatcher component.

Commits
-------

678e066aa8 [EventDispatcher] Remove deprecation layer.
This commit is contained in:
Nicolas Grekas 2019-05-31 09:52:39 +02:00
commit 2a631ecb83
27 changed files with 73 additions and 519 deletions

View File

@ -1,6 +1,14 @@
CHANGELOG CHANGELOG
========= =========
5.0.0
-----
* The signature of the `EventDispatcherInterface::dispatch()` method has been changed to `dispatch($event, string $eventName = null): object`.
* The `Event` class has been removed in favor of `Symfony\Contracts\EventDispatcher\Event`.
* The `TraceableEventDispatcherInterface` has been removed.
* The `WrappedListener` class is now final.
4.3.0 4.3.0
----- -----

View File

@ -14,14 +14,12 @@ namespace Symfony\Component\EventDispatcher\Debug;
use Psr\EventDispatcher\StoppableEventInterface; use Psr\EventDispatcher\StoppableEventInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\BrowserKit\Request; use Symfony\Component\BrowserKit\Request;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\EventDispatcher\LegacyEventProxy;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent; use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Contracts\Service\ResetInterface;
/** /**
* Collects some data about event listeners. * Collects some data about event listeners.
@ -30,7 +28,7 @@ use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
*/ */
class TraceableEventDispatcher implements TraceableEventDispatcherInterface class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterface
{ {
protected $logger; protected $logger;
protected $stopwatch; protected $stopwatch;
@ -44,7 +42,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null, RequestStack $requestStack = null) public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $stopwatch, LoggerInterface $logger = null, RequestStack $requestStack = null)
{ {
$this->dispatcher = LegacyEventDispatcherProxy::decorate($dispatcher); $this->dispatcher = $dispatcher;
$this->stopwatch = $stopwatch; $this->stopwatch = $stopwatch;
$this->logger = $logger; $this->logger = $logger;
$this->wrappedListeners = []; $this->wrappedListeners = [];
@ -130,32 +128,22 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @param string|null $eventName
*/ */
public function dispatch($event/*, string $eventName = null*/) public function dispatch($event, string $eventName = null): object
{ {
if (!\is_object($event)) {
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an object, %s given.', EventDispatcherInterface::class, \gettype($event)));
}
$eventName = $eventName ?? \get_class($event);
if (null === $this->callStack) { if (null === $this->callStack) {
$this->callStack = new \SplObjectStorage(); $this->callStack = new \SplObjectStorage();
} }
$currentRequestHash = $this->currentRequestHash = $this->requestStack && ($request = $this->requestStack->getCurrentRequest()) ? spl_object_hash($request) : ''; $currentRequestHash = $this->currentRequestHash = $this->requestStack && ($request = $this->requestStack->getCurrentRequest()) ? spl_object_hash($request) : '';
$eventName = 1 < \func_num_args() ? \func_get_arg(1) : null;
if (\is_object($event)) { if (null !== $this->logger && ($event instanceof Event || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) {
$eventName = $eventName ?? \get_class($event);
} else {
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as first argument is deprecated since Symfony 4.3, pass it second and provide the event object first instead.', EventDispatcherInterface::class), E_USER_DEPRECATED);
$swap = $event;
$event = $eventName ?? new Event();
$eventName = $swap;
if (!$event instanceof Event) {
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an instance of %s, %s given.', EventDispatcherInterface::class, Event::class, \is_object($event) ? \get_class($event) : \gettype($event)));
}
}
if (null !== $this->logger && ($event instanceof Event || $event instanceof ContractsEvent || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) {
$this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName)); $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
} }
@ -291,35 +279,15 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
/** /**
* Called before dispatching the event. * Called before dispatching the event.
*
* @param object $event
*/ */
protected function beforeDispatch(string $eventName, $event) protected function beforeDispatch(string $eventName, object $event)
{ {
$this->preDispatch($eventName, $event instanceof Event ? $event : new LegacyEventProxy($event));
} }
/** /**
* Called after dispatching the event. * Called after dispatching the event.
*
* @param object $event
*/ */
protected function afterDispatch(string $eventName, $event) protected function afterDispatch(string $eventName, object $event)
{
$this->postDispatch($eventName, $event instanceof Event ? $event : new LegacyEventProxy($event));
}
/**
* @deprecated since Symfony 4.3, will be removed in 5.0, use beforeDispatch instead
*/
protected function preDispatch($eventName, Event $event)
{
}
/**
* @deprecated since Symfony 4.3, will be removed in 5.0, use afterDispatch instead
*/
protected function postDispatch($eventName, Event $event)
{ {
} }

View File

@ -1,42 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\EventDispatcher\Debug;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\Service\ResetInterface;
/**
* @deprecated since Symfony 4.1
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface TraceableEventDispatcherInterface extends EventDispatcherInterface, ResetInterface
{
/**
* Gets the called listeners.
*
* @param Request|null $request The request to get listeners for
*
* @return array An array of called listeners
*/
public function getCalledListeners(/* Request $request = null */);
/**
* Gets the not called listeners.
*
* @param Request|null $request The request to get listeners for
*
* @return array An array of not called listeners
*/
public function getNotCalledListeners(/* Request $request = null */);
}

View File

@ -12,19 +12,15 @@
namespace Symfony\Component\EventDispatcher\Debug; namespace Symfony\Component\EventDispatcher\Debug;
use Psr\EventDispatcher\StoppableEventInterface; use Psr\EventDispatcher\StoppableEventInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventProxy;
use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\VarDumper\Caster\ClassStub; use Symfony\Component\VarDumper\Caster\ClassStub;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent; use Symfony\Contracts\EventDispatcher\Event;
/** /**
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
*
* @final since Symfony 4.3: the "Event" type-hint on __invoke() will be replaced by "object" in 5.0
*/ */
class WrappedListener final class WrappedListener
{ {
private $listener; private $listener;
private $optimizedListener; private $optimizedListener;
@ -81,22 +77,22 @@ class WrappedListener
return $this->listener; return $this->listener;
} }
public function wasCalled() public function wasCalled(): bool
{ {
return $this->called; return $this->called;
} }
public function stoppedPropagation() public function stoppedPropagation(): bool
{ {
return $this->stoppedPropagation; return $this->stoppedPropagation;
} }
public function getPretty() public function getPretty(): string
{ {
return $this->pretty; return $this->pretty;
} }
public function getInfo($eventName) public function getInfo(string $eventName): array
{ {
if (null === $this->stub) { if (null === $this->stub) {
$this->stub = self::$hasClassStub ? new ClassStub($this->pretty.'()', $this->listener) : $this->pretty.'()'; $this->stub = self::$hasClassStub ? new ClassStub($this->pretty.'()', $this->listener) : $this->pretty.'()';
@ -110,12 +106,8 @@ class WrappedListener
]; ];
} }
public function __invoke(Event $event, $eventName, EventDispatcherInterface $dispatcher) public function __invoke(object $event, string $eventName, EventDispatcherInterface $dispatcher): void
{ {
if ($event instanceof LegacyEventProxy) {
$event = $event->getEvent();
}
$dispatcher = $this->dispatcher ?: $dispatcher; $dispatcher = $this->dispatcher ?: $dispatcher;
$this->called = true; $this->called = true;
@ -129,7 +121,7 @@ class WrappedListener
$e->stop(); $e->stop();
} }
if (($event instanceof Event || $event instanceof ContractsEvent || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) { if (($event instanceof Event || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) {
$this->stoppedPropagation = true; $this->stoppedPropagation = true;
} }
} }

View File

@ -1,38 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\EventDispatcher;
/**
* @deprecated since Symfony 4.3, use "Symfony\Contracts\EventDispatcher\Event" instead
*/
class Event
{
private $propagationStopped = false;
/**
* @return bool Whether propagation was already stopped for this event
*
* @deprecated since Symfony 4.3, use "Symfony\Contracts\EventDispatcher\Event" instead
*/
public function isPropagationStopped()
{
return $this->propagationStopped;
}
/**
* @deprecated since Symfony 4.3, use "Symfony\Contracts\EventDispatcher\Event" instead
*/
public function stopPropagation()
{
$this->propagationStopped = true;
}
}

View File

@ -13,7 +13,7 @@ namespace Symfony\Component\EventDispatcher;
use Psr\EventDispatcher\StoppableEventInterface; use Psr\EventDispatcher\StoppableEventInterface;
use Symfony\Component\EventDispatcher\Debug\WrappedListener; use Symfony\Component\EventDispatcher\Debug\WrappedListener;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent; use Symfony\Contracts\EventDispatcher\Event;
/** /**
* The EventDispatcherInterface is the central point of Symfony's event listener system. * The EventDispatcherInterface is the central point of Symfony's event listener system.
@ -45,26 +45,15 @@ class EventDispatcher implements EventDispatcherInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @param string|null $eventName
*/ */
public function dispatch($event/*, string $eventName = null*/) public function dispatch($event, string $eventName = null): object
{ {
$eventName = 1 < \func_num_args() ? \func_get_arg(1) : null; if (!\is_object($event)) {
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an object, %s given.', EventDispatcherInterface::class, \gettype($event)));
if (\is_object($event)) {
$eventName = $eventName ?? \get_class($event);
} else {
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as first argument is deprecated since Symfony 4.3, pass it second and provide the event object first instead.', EventDispatcherInterface::class), E_USER_DEPRECATED);
$swap = $event;
$event = $eventName ?? new Event();
$eventName = $swap;
if (!$event instanceof Event) {
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an instance of %s, %s given.', EventDispatcherInterface::class, Event::class, \is_object($event) ? \get_class($event) : \gettype($event)));
}
} }
$eventName = $eventName ?? \get_class($event);
if (null !== $this->optimized && null !== $eventName) { if (null !== $this->optimized && null !== $eventName) {
$listeners = $this->optimized[$eventName] ?? (empty($this->listeners[$eventName]) ? [] : $this->optimizeListeners($eventName)); $listeners = $this->optimized[$eventName] ?? (empty($this->listeners[$eventName]) ? [] : $this->optimizeListeners($eventName));
} else { } else {
@ -229,34 +218,14 @@ class EventDispatcher implements EventDispatcherInterface
* @param string $eventName The name of the event to dispatch * @param string $eventName The name of the event to dispatch
* @param object $event The event object to pass to the event handlers/listeners * @param object $event The event object to pass to the event handlers/listeners
*/ */
protected function callListeners(iterable $listeners, string $eventName, $event) protected function callListeners(iterable $listeners, string $eventName, object $event)
{ {
if ($event instanceof Event) { $stoppable = $event instanceof Event || $event instanceof StoppableEventInterface;
$this->doDispatch($listeners, $eventName, $event);
return;
}
$stoppable = $event instanceof ContractsEvent || $event instanceof StoppableEventInterface;
foreach ($listeners as $listener) { foreach ($listeners as $listener) {
if ($stoppable && $event->isPropagationStopped()) { if ($stoppable && $event->isPropagationStopped()) {
break; break;
} }
// @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);
}
}
/**
* @deprecated since Symfony 4.3, use callListeners() instead
*/
protected function doDispatch($listeners, $eventName, Event $event)
{
foreach ($listeners as $listener) {
if ($event->isPropagationStopped()) {
break;
}
$listener($event, $eventName, $this); $listener($event, $eventName, $this);
} }
} }

View File

@ -22,6 +22,11 @@ use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEvent
*/ */
interface EventDispatcherInterface extends ContractsEventDispatcherInterface interface EventDispatcherInterface extends ContractsEventDispatcherInterface
{ {
/**
* {@inheritdoc}
*/
public function dispatch($event, string $eventName = null): object;
/** /**
* Adds an event listener that listens on the specified events. * Adds an event listener that listens on the specified events.
* *

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\EventDispatcher; namespace Symfony\Component\EventDispatcher;
use Symfony\Contracts\EventDispatcher\Event;
/** /**
* Event encapsulation class. * Event encapsulation class.
* *

View File

@ -22,25 +22,14 @@ class ImmutableEventDispatcher implements EventDispatcherInterface
public function __construct(EventDispatcherInterface $dispatcher) public function __construct(EventDispatcherInterface $dispatcher)
{ {
$this->dispatcher = LegacyEventDispatcherProxy::decorate($dispatcher); $this->dispatcher = $dispatcher;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @param string|null $eventName
*/ */
public function dispatch($event/*, string $eventName = null*/) public function dispatch($event, string $eventName = null): object
{ {
$eventName = 1 < \func_num_args() ? \func_get_arg(1) : null;
if (\is_scalar($event)) {
// deprecated
$swap = $event;
$event = $eventName ?? new Event();
$eventName = $swap;
}
return $this->dispatcher->dispatch($event, $eventName); return $this->dispatcher->dispatch($event, $eventName);
} }

View File

@ -11,9 +11,7 @@
namespace Symfony\Component\EventDispatcher; namespace Symfony\Component\EventDispatcher;
use Psr\EventDispatcher\StoppableEventInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
/** /**
* An helper class to provide BC/FC with the legacy signature of EventDispatcherInterface::dispatch(). * An helper class to provide BC/FC with the legacy signature of EventDispatcherInterface::dispatch().
@ -22,126 +20,10 @@ use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEvent
* *
* @author Nicolas Grekas <p@tchwork.com> * @author Nicolas Grekas <p@tchwork.com>
*/ */
final class LegacyEventDispatcherProxy implements EventDispatcherInterface final class LegacyEventDispatcherProxy
{ {
private $dispatcher; public static function decorate(?EventDispatcherInterface $dispatcher): ?EventDispatcherInterface
public static function decorate(?ContractsEventDispatcherInterface $dispatcher): ?ContractsEventDispatcherInterface
{ {
if (null === $dispatcher) { return $dispatcher;
return null;
}
$r = new \ReflectionMethod($dispatcher, 'dispatch');
$param2 = $r->getParameters()[1] ?? null;
if (!$param2 || !$param2->hasType() || $param2->getType()->isBuiltin()) {
return $dispatcher;
}
@trigger_error(sprintf('The signature of the "%s::dispatch()" method should be updated to "dispatch($event, string $eventName = null)", not doing so is deprecated since Symfony 4.3.', $r->class), E_USER_DEPRECATED);
$self = new self();
$self->dispatcher = $dispatcher;
return $self;
}
/**
* {@inheritdoc}
*
* @param string|null $eventName
*/
public function dispatch($event/*, string $eventName = null*/)
{
$eventName = 1 < \func_num_args() ? \func_get_arg(1) : null;
if (\is_object($event)) {
$eventName = $eventName ?? \get_class($event);
} else {
@trigger_error(sprintf('Calling the "%s::dispatch()" method with the event name as first argument is deprecated since Symfony 4.3, pass it second and provide the event object first instead.', ContractsEventDispatcherInterface::class), E_USER_DEPRECATED);
$swap = $event;
$event = $eventName ?? new Event();
$eventName = $swap;
if (!$event instanceof Event) {
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an instance of %s, %s given.', ContractsEventDispatcherInterface::class, Event::class, \is_object($event) ? \get_class($event) : \gettype($event)));
}
}
$listeners = $this->getListeners($eventName);
$stoppable = $event instanceof Event || $event instanceof ContractsEvent || $event instanceof StoppableEventInterface;
foreach ($listeners as $listener) {
if ($stoppable && $event->isPropagationStopped()) {
break;
}
$listener($event, $eventName, $this);
}
return $event;
}
/**
* {@inheritdoc}
*/
public function addListener($eventName, $listener, $priority = 0)
{
return $this->dispatcher->addListener($eventName, $listener, $priority);
}
/**
* {@inheritdoc}
*/
public function addSubscriber(EventSubscriberInterface $subscriber)
{
return $this->dispatcher->addSubscriber($subscriber);
}
/**
* {@inheritdoc}
*/
public function removeListener($eventName, $listener)
{
return $this->dispatcher->removeListener($eventName, $listener);
}
/**
* {@inheritdoc}
*/
public function removeSubscriber(EventSubscriberInterface $subscriber)
{
return $this->dispatcher->removeSubscriber($subscriber);
}
/**
* {@inheritdoc}
*/
public function getListeners($eventName = null)
{
return $this->dispatcher->getListeners($eventName);
}
/**
* {@inheritdoc}
*/
public function getListenerPriority($eventName, $listener)
{
return $this->dispatcher->getListenerPriority($eventName, $listener);
}
/**
* {@inheritdoc}
*/
public function hasListeners($eventName = null)
{
return $this->dispatcher->hasListeners($eventName);
}
/**
* Proxies all method calls to the original event dispatcher.
*/
public function __call($method, $arguments)
{
return $this->dispatcher->{$method}(...$arguments);
} }
} }

View File

@ -1,62 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\EventDispatcher;
use Psr\EventDispatcher\StoppableEventInterface;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
/**
* @internal to be removed in 5.0.
*/
final class LegacyEventProxy extends Event
{
private $event;
/**
* @param object $event
*/
public function __construct($event)
{
$this->event = $event;
}
/**
* @return object $event
*/
public function getEvent()
{
return $this->event;
}
public function isPropagationStopped()
{
if (!$this->event instanceof ContractsEvent && !$this->event instanceof StoppableEventInterface) {
return false;
}
return $this->event->isPropagationStopped();
}
public function stopPropagation()
{
if (!$this->event instanceof ContractsEvent) {
return;
}
$this->event->stopPropagation();
}
public function __call($name, $args)
{
return $this->event->{$name}(...$args);
}
}

View File

@ -13,12 +13,11 @@ namespace Symfony\Component\EventDispatcher\Tests\Debug;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent; use Symfony\Contracts\EventDispatcher\Event;
class TraceableEventDispatcherTest extends TestCase class TraceableEventDispatcherTest extends TestCase
{ {
@ -140,19 +139,6 @@ class TraceableEventDispatcherTest extends TestCase
$this->assertEquals([['event' => 'foo', 'pretty' => 'closure', 'priority' => 5]], $listeners); $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() public function testDispatchAfterReset()
{ {
$tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());

View File

@ -12,10 +12,9 @@
namespace Symfony\Component\EventDispatcher\Tests; namespace Symfony\Component\EventDispatcher\Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent; use Symfony\Contracts\EventDispatcher\Event;
class EventDispatcherTest extends TestCase class EventDispatcherTest extends TestCase
{ {
@ -135,22 +134,8 @@ class EventDispatcherTest extends TestCase
$this->dispatcher->dispatch(new Event(), self::preFoo); $this->dispatcher->dispatch(new Event(), self::preFoo);
$this->assertTrue($this->listener->preFooInvoked); $this->assertTrue($this->listener->preFooInvoked);
$this->assertFalse($this->listener->postFooInvoked); $this->assertFalse($this->listener->postFooInvoked);
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(new Event(), 'noevent')); $this->assertInstanceOf('Symfony\Contracts\EventDispatcher\Event', $this->dispatcher->dispatch(new Event(), 'noevent'));
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(new Event(), self::preFoo)); $this->assertInstanceOf('Symfony\Contracts\EventDispatcher\Event', $this->dispatcher->dispatch(new Event(), self::preFoo));
$event = new Event();
$return = $this->dispatcher->dispatch($event, self::preFoo);
$this->assertSame($event, $return);
}
public function testDispatchContractsEvent()
{
$this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
$this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']);
$this->dispatcher->dispatch(new ContractsEvent(), self::preFoo);
$this->assertTrue($this->listener->preFooInvoked);
$this->assertFalse($this->listener->postFooInvoked);
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(new Event(), 'noevent'));
$this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(new Event(), self::preFoo));
$event = new Event(); $event = new Event();
$return = $this->dispatcher->dispatch($event, self::preFoo); $return = $this->dispatcher->dispatch($event, self::preFoo);
$this->assertSame($event, $return); $this->assertSame($event, $return);

View File

@ -1,55 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\EventDispatcher\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Event;
/**
* @group legacy
*/
class EventTest extends TestCase
{
/**
* @var \Symfony\Component\EventDispatcher\Event
*/
protected $event;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->event = new Event();
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
$this->event = null;
}
public function testIsPropagationStopped()
{
$this->assertFalse($this->event->isPropagationStopped());
}
public function testStopPropagationAndIsPropagationStopped()
{
$this->event->stopPropagation();
$this->assertTrue($this->event->isPropagationStopped());
}
}

View File

@ -12,8 +12,8 @@
namespace Symfony\Component\EventDispatcher\Tests; namespace Symfony\Component\EventDispatcher\Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\ImmutableEventDispatcher; use Symfony\Component\EventDispatcher\ImmutableEventDispatcher;
use Symfony\Contracts\EventDispatcher\Event;
/** /**
* @author Bernhard Schussek <bschussek@gmail.com> * @author Bernhard Schussek <bschussek@gmail.com>
@ -39,13 +39,15 @@ class ImmutableEventDispatcherTest extends TestCase
public function testDispatchDelegates() public function testDispatchDelegates()
{ {
$event = new Event(); $event = new Event();
$expectedResult = new class() {
};
$this->innerDispatcher->expects($this->once()) $this->innerDispatcher->expects($this->once())
->method('dispatch') ->method('dispatch')
->with($event, 'event') ->with($event, 'event')
->willReturn('result'); ->willReturn($expectedResult);
$this->assertSame('result', $this->dispatcher->dispatch($event, 'event')); $this->assertSame($expectedResult, $this->dispatcher->dispatch($event, 'event'));
} }
public function testGetListenersDelegates() public function testGetListenersDelegates()

View File

@ -1,35 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\EventDispatcher\Tests;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
/**
* @group legacy
*/
class LegacyEventDispatcherTest extends EventDispatcherTest
{
protected function createEventDispatcher()
{
return LegacyEventDispatcherProxy::decorate(new TestLegacyEventDispatcher());
}
}
class TestLegacyEventDispatcher extends EventDispatcher
{
public function dispatch($eventName, Event $event = null)
{
return parent::dispatch($event, $eventName);
}
}

View File

@ -11,7 +11,7 @@
namespace Symfony\Component\Form; namespace Symfony\Component\Form;
use Symfony\Component\EventDispatcher\Event; use Symfony\Contracts\EventDispatcher\Event;
/** /**
* @author Bernhard Schussek <bschussek@gmail.com> * @author Bernhard Schussek <bschussek@gmail.com>

View File

@ -12,7 +12,6 @@
namespace Symfony\Component\HttpKernel\DataCollector; namespace Symfony\Component\HttpKernel\DataCollector;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -60,12 +59,9 @@ class EventDataCollector extends DataCollector implements LateDataCollectorInter
public function lateCollect() public function lateCollect()
{ {
if ($this->dispatcher instanceof TraceableEventDispatcherInterface) { if ($this->dispatcher instanceof TraceableEventDispatcher) {
$this->setCalledListeners($this->dispatcher->getCalledListeners($this->currentRequest)); $this->setCalledListeners($this->dispatcher->getCalledListeners($this->currentRequest));
$this->setNotCalledListeners($this->dispatcher->getNotCalledListeners($this->currentRequest)); $this->setNotCalledListeners($this->dispatcher->getNotCalledListeners($this->currentRequest));
}
if ($this->dispatcher instanceof TraceableEventDispatcher) {
$this->setOrphanedEvents($this->dispatcher->getOrphanedEvents($this->currentRequest)); $this->setOrphanedEvents($this->dispatcher->getOrphanedEvents($this->currentRequest));
} }

View File

@ -11,9 +11,9 @@
namespace Symfony\Component\HttpKernel\Event; namespace Symfony\Component\HttpKernel\Event;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Contracts\EventDispatcher\Event;
/** /**
* Base class for events thrown in the HttpKernel component. * Base class for events thrown in the HttpKernel component.

View File

@ -12,7 +12,6 @@
namespace Symfony\Component\HttpKernel\Tests\Debug; namespace Symfony\Component\HttpKernel\Tests\Debug;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
@ -20,6 +19,7 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher; use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Contracts\EventDispatcher\Event;
class TraceableEventDispatcherTest extends TestCase class TraceableEventDispatcherTest extends TestCase
{ {

View File

@ -11,9 +11,9 @@
namespace Symfony\Component\Mailer\Event; namespace Symfony\Component\Mailer\Event;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Mailer\SmtpEnvelope; use Symfony\Component\Mailer\SmtpEnvelope;
use Symfony\Component\Mime\RawMessage; use Symfony\Component\Mime\RawMessage;
use Symfony\Contracts\EventDispatcher\Event;
/** /**
* Allows the transformation of a Message. * Allows the transformation of a Message.

View File

@ -11,8 +11,8 @@
namespace Symfony\Component\Security\Core\Event; namespace Symfony\Component\Security\Core\Event;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Contracts\EventDispatcher\Event;
/** /**
* This is a general purpose authentication event. * This is a general purpose authentication event.

View File

@ -11,8 +11,8 @@
namespace Symfony\Component\Security\Core\Event; namespace Symfony\Component\Security\Core\Event;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Contracts\EventDispatcher\Event;
/** /**
* This event is dispatched on voter vote. * This event is dispatched on voter vote.

View File

@ -11,9 +11,9 @@
namespace Symfony\Component\Security\Http\Event; namespace Symfony\Component\Security\Http\Event;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Contracts\EventDispatcher\Event;
/** /**
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>

View File

@ -11,10 +11,10 @@
namespace Symfony\Component\Security\Http\Event; namespace Symfony\Component\Security\Http\Event;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\EventDispatcher\Event;
/** /**
* SwitchUserEvent. * SwitchUserEvent.

View File

@ -11,10 +11,10 @@
namespace Symfony\Component\Workflow\Event; namespace Symfony\Component\Workflow\Event;
use Symfony\Component\EventDispatcher\Event as BaseEvent;
use Symfony\Component\Workflow\Marking; use Symfony\Component\Workflow\Marking;
use Symfony\Component\Workflow\Transition; use Symfony\Component\Workflow\Transition;
use Symfony\Component\Workflow\WorkflowInterface; use Symfony\Component\Workflow\WorkflowInterface;
use Symfony\Contracts\EventDispatcher\Event as BaseEvent;
/** /**
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>

View File

@ -584,9 +584,11 @@ class EventDispatcherMock implements \Symfony\Component\EventDispatcher\EventDis
{ {
public $dispatchedEvents = []; public $dispatchedEvents = [];
public function dispatch($event, string $eventName = null) public function dispatch($event, string $eventName = null): object
{ {
$this->dispatchedEvents[] = $eventName; $this->dispatchedEvents[] = $eventName;
return $event;
} }
public function addListener($eventName, $listener, $priority = 0) public function addListener($eventName, $listener, $priority = 0)