diff --git a/src/Symfony/Component/EventDispatcher/CHANGELOG.md b/src/Symfony/Component/EventDispatcher/CHANGELOG.md index 7653cad1c0eb0..6e2360b55b831 100644 --- a/src/Symfony/Component/EventDispatcher/CHANGELOG.md +++ b/src/Symfony/Component/EventDispatcher/CHANGELOG.md @@ -1,6 +1,14 @@ 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 ----- diff --git a/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php b/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php index 7716d8663177e..1f6b0af51e57f 100644 --- a/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php @@ -14,14 +14,12 @@ use Psr\EventDispatcher\StoppableEventInterface; use Psr\Log\LoggerInterface; use Symfony\Component\BrowserKit\Request; -use Symfony\Component\EventDispatcher\Event; 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; +use Symfony\Contracts\EventDispatcher\Event; +use Symfony\Contracts\Service\ResetInterface; /** * Collects some data about event listeners. @@ -30,7 +28,7 @@ * * @author Fabien Potencier */ -class TraceableEventDispatcher implements TraceableEventDispatcherInterface +class TraceableEventDispatcher implements EventDispatcherInterface, ResetInterface { protected $logger; protected $stopwatch; @@ -44,7 +42,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface 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->logger = $logger; $this->wrappedListeners = []; @@ -130,32 +128,22 @@ public function hasListeners($eventName = null) /** * {@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) { $this->callStack = new \SplObjectStorage(); } $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)) { - $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()) { + if (null !== $this->logger && ($event instanceof Event || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) { $this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName)); } @@ -291,35 +279,15 @@ public function __call($method, $arguments) /** * 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. - * - * @param object $event - */ - protected function afterDispatch(string $eventName, $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) + protected function afterDispatch(string $eventName, object $event) { } diff --git a/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcherInterface.php b/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcherInterface.php deleted file mode 100644 index 4fedb9a413a3b..0000000000000 --- a/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcherInterface.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * 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 - */ -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 */); -} diff --git a/src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php b/src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php index e047639081c78..35905c0c05912 100644 --- a/src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php +++ b/src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php @@ -12,19 +12,15 @@ namespace Symfony\Component\EventDispatcher\Debug; use Psr\EventDispatcher\StoppableEventInterface; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\LegacyEventProxy; use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\VarDumper\Caster\ClassStub; -use Symfony\Contracts\EventDispatcher\Event as ContractsEvent; +use Symfony\Contracts\EventDispatcher\Event; /** * @author Fabien Potencier - * - * @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 $optimizedListener; @@ -81,22 +77,22 @@ public function getWrappedListener() return $this->listener; } - public function wasCalled() + public function wasCalled(): bool { return $this->called; } - public function stoppedPropagation() + public function stoppedPropagation(): bool { return $this->stoppedPropagation; } - public function getPretty() + public function getPretty(): string { return $this->pretty; } - public function getInfo($eventName) + public function getInfo(string $eventName): array { if (null === $this->stub) { $this->stub = self::$hasClassStub ? new ClassStub($this->pretty.'()', $this->listener) : $this->pretty.'()'; @@ -110,12 +106,8 @@ public function getInfo($eventName) ]; } - 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; $this->called = true; @@ -129,7 +121,7 @@ public function __invoke(Event $event, $eventName, EventDispatcherInterface $dis $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; } } diff --git a/src/Symfony/Component/EventDispatcher/Event.php b/src/Symfony/Component/EventDispatcher/Event.php deleted file mode 100644 index 307c4be5de0c2..0000000000000 --- a/src/Symfony/Component/EventDispatcher/Event.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * 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; - } -} diff --git a/src/Symfony/Component/EventDispatcher/EventDispatcher.php b/src/Symfony/Component/EventDispatcher/EventDispatcher.php index e68918c31c680..ecbb44587429f 100644 --- a/src/Symfony/Component/EventDispatcher/EventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/EventDispatcher.php @@ -13,7 +13,7 @@ use Psr\EventDispatcher\StoppableEventInterface; 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. @@ -45,26 +45,15 @@ public function __construct() /** * {@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)) { - $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 (!\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->optimized && null !== $eventName) { $listeners = $this->optimized[$eventName] ?? (empty($this->listeners[$eventName]) ? [] : $this->optimizeListeners($eventName)); } else { @@ -229,34 +218,14 @@ public function removeSubscriber(EventSubscriberInterface $subscriber) * @param string $eventName The name of the event to dispatch * @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) { - $this->doDispatch($listeners, $eventName, $event); - - return; - } - - $stoppable = $event instanceof ContractsEvent || $event instanceof StoppableEventInterface; + $stoppable = $event instanceof Event || $event instanceof StoppableEventInterface; foreach ($listeners as $listener) { if ($stoppable && $event->isPropagationStopped()) { 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); } } diff --git a/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php b/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php index ceaa62aeb0472..146ae5b393caa 100644 --- a/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php +++ b/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php @@ -22,6 +22,11 @@ */ interface EventDispatcherInterface extends ContractsEventDispatcherInterface { + /** + * {@inheritdoc} + */ + public function dispatch($event, string $eventName = null): object; + /** * Adds an event listener that listens on the specified events. * diff --git a/src/Symfony/Component/EventDispatcher/GenericEvent.php b/src/Symfony/Component/EventDispatcher/GenericEvent.php index f005e3a3db076..62bd59c903c08 100644 --- a/src/Symfony/Component/EventDispatcher/GenericEvent.php +++ b/src/Symfony/Component/EventDispatcher/GenericEvent.php @@ -11,6 +11,8 @@ namespace Symfony\Component\EventDispatcher; +use Symfony\Contracts\EventDispatcher\Event; + /** * Event encapsulation class. * diff --git a/src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php b/src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php index 082e2a05d48f9..f164e8b29800f 100644 --- a/src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php @@ -22,25 +22,14 @@ class ImmutableEventDispatcher implements EventDispatcherInterface public function __construct(EventDispatcherInterface $dispatcher) { - $this->dispatcher = LegacyEventDispatcherProxy::decorate($dispatcher); + $this->dispatcher = $dispatcher; } /** * {@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); } diff --git a/src/Symfony/Component/EventDispatcher/LegacyEventDispatcherProxy.php b/src/Symfony/Component/EventDispatcher/LegacyEventDispatcherProxy.php index 3ae3735e23e52..bde2e0b784906 100644 --- a/src/Symfony/Component/EventDispatcher/LegacyEventDispatcherProxy.php +++ b/src/Symfony/Component/EventDispatcher/LegacyEventDispatcherProxy.php @@ -11,9 +11,7 @@ namespace Symfony\Component\EventDispatcher; -use Psr\EventDispatcher\StoppableEventInterface; -use Symfony\Contracts\EventDispatcher\Event as ContractsEvent; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; /** * An helper class to provide BC/FC with the legacy signature of EventDispatcherInterface::dispatch(). @@ -22,126 +20,10 @@ * * @author Nicolas Grekas */ -final class LegacyEventDispatcherProxy implements EventDispatcherInterface +final class LegacyEventDispatcherProxy { - private $dispatcher; - - public static function decorate(?ContractsEventDispatcherInterface $dispatcher): ?ContractsEventDispatcherInterface - { - if (null === $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) + public static function decorate(?EventDispatcherInterface $dispatcher): ?EventDispatcherInterface { - return $this->dispatcher->{$method}(...$arguments); + return $dispatcher; } } diff --git a/src/Symfony/Component/EventDispatcher/LegacyEventProxy.php b/src/Symfony/Component/EventDispatcher/LegacyEventProxy.php deleted file mode 100644 index cad8cfaedb724..0000000000000 --- a/src/Symfony/Component/EventDispatcher/LegacyEventProxy.php +++ /dev/null @@ -1,62 +0,0 @@ - - * - * 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); - } -} diff --git a/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php b/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php index ea476eee04c8e..5017d91fa8b4e 100644 --- a/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php +++ b/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php @@ -13,12 +13,11 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Stopwatch\Stopwatch; -use Symfony\Contracts\EventDispatcher\Event as ContractsEvent; +use Symfony\Contracts\EventDispatcher\Event; class TraceableEventDispatcherTest extends TestCase { @@ -140,19 +139,6 @@ 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()); diff --git a/src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php b/src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php index e89f78cda8e89..c71c3f59f83dd 100644 --- a/src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php +++ b/src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php @@ -12,10 +12,9 @@ namespace Symfony\Component\EventDispatcher\Tests; use PHPUnit\Framework\TestCase; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Symfony\Contracts\EventDispatcher\Event as ContractsEvent; +use Symfony\Contracts\EventDispatcher\Event; class EventDispatcherTest extends TestCase { @@ -135,22 +134,8 @@ public function testDispatch() $this->dispatcher->dispatch(new Event(), 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(); - $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)); + $this->assertInstanceOf('Symfony\Contracts\EventDispatcher\Event', $this->dispatcher->dispatch(new Event(), 'noevent')); + $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); diff --git a/src/Symfony/Component/EventDispatcher/Tests/EventTest.php b/src/Symfony/Component/EventDispatcher/Tests/EventTest.php deleted file mode 100644 index ca8e945c649cf..0000000000000 --- a/src/Symfony/Component/EventDispatcher/Tests/EventTest.php +++ /dev/null @@ -1,55 +0,0 @@ - - * - * 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()); - } -} diff --git a/src/Symfony/Component/EventDispatcher/Tests/ImmutableEventDispatcherTest.php b/src/Symfony/Component/EventDispatcher/Tests/ImmutableEventDispatcherTest.php index f6556f0b1be75..15a40e8614673 100644 --- a/src/Symfony/Component/EventDispatcher/Tests/ImmutableEventDispatcherTest.php +++ b/src/Symfony/Component/EventDispatcher/Tests/ImmutableEventDispatcherTest.php @@ -12,8 +12,8 @@ namespace Symfony\Component\EventDispatcher\Tests; use PHPUnit\Framework\TestCase; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\ImmutableEventDispatcher; +use Symfony\Contracts\EventDispatcher\Event; /** * @author Bernhard Schussek @@ -39,13 +39,15 @@ protected function setUp() public function testDispatchDelegates() { $event = new Event(); + $expectedResult = new class() { + }; $this->innerDispatcher->expects($this->once()) ->method('dispatch') ->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() diff --git a/src/Symfony/Component/EventDispatcher/Tests/LegacyEventDispatcherTest.php b/src/Symfony/Component/EventDispatcher/Tests/LegacyEventDispatcherTest.php deleted file mode 100644 index 49aa2f9ff3f92..0000000000000 --- a/src/Symfony/Component/EventDispatcher/Tests/LegacyEventDispatcherTest.php +++ /dev/null @@ -1,35 +0,0 @@ - - * - * 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); - } -} diff --git a/src/Symfony/Component/Form/FormEvent.php b/src/Symfony/Component/Form/FormEvent.php index 3b6d484e75803..45c3f93b0c581 100644 --- a/src/Symfony/Component/Form/FormEvent.php +++ b/src/Symfony/Component/Form/FormEvent.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Form; -use Symfony\Component\EventDispatcher\Event; +use Symfony\Contracts\EventDispatcher\Event; /** * @author Bernhard Schussek diff --git a/src/Symfony/Component/HttpKernel/DataCollector/EventDataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/EventDataCollector.php index d918ddf786634..37145c4abe734 100644 --- a/src/Symfony/Component/HttpKernel/DataCollector/EventDataCollector.php +++ b/src/Symfony/Component/HttpKernel/DataCollector/EventDataCollector.php @@ -12,7 +12,6 @@ namespace Symfony\Component\HttpKernel\DataCollector; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; -use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; @@ -60,12 +59,9 @@ public function reset() public function lateCollect() { - if ($this->dispatcher instanceof TraceableEventDispatcherInterface) { + if ($this->dispatcher instanceof TraceableEventDispatcher) { $this->setCalledListeners($this->dispatcher->getCalledListeners($this->currentRequest)); $this->setNotCalledListeners($this->dispatcher->getNotCalledListeners($this->currentRequest)); - } - - if ($this->dispatcher instanceof TraceableEventDispatcher) { $this->setOrphanedEvents($this->dispatcher->getOrphanedEvents($this->currentRequest)); } diff --git a/src/Symfony/Component/HttpKernel/Event/KernelEvent.php b/src/Symfony/Component/HttpKernel/Event/KernelEvent.php index f3db8a60d9ec2..e46510a95163e 100644 --- a/src/Symfony/Component/HttpKernel/Event/KernelEvent.php +++ b/src/Symfony/Component/HttpKernel/Event/KernelEvent.php @@ -11,9 +11,9 @@ namespace Symfony\Component\HttpKernel\Event; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Contracts\EventDispatcher\Event; /** * Base class for events thrown in the HttpKernel component. diff --git a/src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php b/src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php index bd70de9c3b9c8..aa973a7ad707e 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php @@ -12,7 +12,6 @@ namespace Symfony\Component\HttpKernel\Tests\Debug; use PHPUnit\Framework\TestCase; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; @@ -20,6 +19,7 @@ use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher; use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\Stopwatch\Stopwatch; +use Symfony\Contracts\EventDispatcher\Event; class TraceableEventDispatcherTest extends TestCase { diff --git a/src/Symfony/Component/Mailer/Event/MessageEvent.php b/src/Symfony/Component/Mailer/Event/MessageEvent.php index a0891e98688ca..7b11d7221c20d 100644 --- a/src/Symfony/Component/Mailer/Event/MessageEvent.php +++ b/src/Symfony/Component/Mailer/Event/MessageEvent.php @@ -11,9 +11,9 @@ namespace Symfony\Component\Mailer\Event; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\Mailer\SmtpEnvelope; use Symfony\Component\Mime\RawMessage; +use Symfony\Contracts\EventDispatcher\Event; /** * Allows the transformation of a Message. diff --git a/src/Symfony/Component/Security/Core/Event/AuthenticationEvent.php b/src/Symfony/Component/Security/Core/Event/AuthenticationEvent.php index d99aea5084b40..4fc151960b296 100644 --- a/src/Symfony/Component/Security/Core/Event/AuthenticationEvent.php +++ b/src/Symfony/Component/Security/Core/Event/AuthenticationEvent.php @@ -11,8 +11,8 @@ namespace Symfony\Component\Security\Core\Event; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; +use Symfony\Contracts\EventDispatcher\Event; /** * This is a general purpose authentication event. diff --git a/src/Symfony/Component/Security/Core/Event/VoteEvent.php b/src/Symfony/Component/Security/Core/Event/VoteEvent.php index 433fd1d8d2a87..7fec865a004d1 100644 --- a/src/Symfony/Component/Security/Core/Event/VoteEvent.php +++ b/src/Symfony/Component/Security/Core/Event/VoteEvent.php @@ -11,8 +11,8 @@ namespace Symfony\Component\Security\Core\Event; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; +use Symfony\Contracts\EventDispatcher\Event; /** * This event is dispatched on voter vote. diff --git a/src/Symfony/Component/Security/Http/Event/InteractiveLoginEvent.php b/src/Symfony/Component/Security/Http/Event/InteractiveLoginEvent.php index 767d50a27aa1c..9caae36661562 100644 --- a/src/Symfony/Component/Security/Http/Event/InteractiveLoginEvent.php +++ b/src/Symfony/Component/Security/Http/Event/InteractiveLoginEvent.php @@ -11,9 +11,9 @@ namespace Symfony\Component\Security\Http\Event; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; +use Symfony\Contracts\EventDispatcher\Event; /** * @author Fabien Potencier diff --git a/src/Symfony/Component/Security/Http/Event/SwitchUserEvent.php b/src/Symfony/Component/Security/Http/Event/SwitchUserEvent.php index b1b24e3e83355..40d2eef33e650 100644 --- a/src/Symfony/Component/Security/Http/Event/SwitchUserEvent.php +++ b/src/Symfony/Component/Security/Http/Event/SwitchUserEvent.php @@ -11,10 +11,10 @@ namespace Symfony\Component\Security\Http\Event; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Contracts\EventDispatcher\Event; /** * SwitchUserEvent. diff --git a/src/Symfony/Component/Workflow/Event/Event.php b/src/Symfony/Component/Workflow/Event/Event.php index 09ba70bd306e3..c741d7aab7c1a 100644 --- a/src/Symfony/Component/Workflow/Event/Event.php +++ b/src/Symfony/Component/Workflow/Event/Event.php @@ -11,10 +11,10 @@ namespace Symfony\Component\Workflow\Event; -use Symfony\Component\EventDispatcher\Event as BaseEvent; use Symfony\Component\Workflow\Marking; use Symfony\Component\Workflow\Transition; use Symfony\Component\Workflow\WorkflowInterface; +use Symfony\Contracts\EventDispatcher\Event as BaseEvent; /** * @author Fabien Potencier diff --git a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php index e543836fc9482..6579921d9e642 100644 --- a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php +++ b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php @@ -584,9 +584,11 @@ class EventDispatcherMock implements \Symfony\Component\EventDispatcher\EventDis { public $dispatchedEvents = []; - public function dispatch($event, string $eventName = null) + public function dispatch($event, string $eventName = null): object { $this->dispatchedEvents[] = $eventName; + + return $event; } public function addListener($eventName, $listener, $priority = 0)