From 22c1fb46ddb035ba8fe1027cc0854eae0cc81735 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Wed, 12 Feb 2020 01:28:49 +0100 Subject: [PATCH] Fix event dispatcher usage in symfony 4.4 --- composer.json | 1 + src/Backend/AMQPBackend.php | 2 +- src/Backend/MessageManagerBackend.php | 2 +- src/Backend/RuntimeBackend.php | 2 +- src/Command/ConsumerHandlerCommand.php | 5 +++-- src/Command/RestartCommand.php | 4 +++- src/Consumer/ConsumerEvent.php | 2 +- src/Event/IterateEvent.php | 2 +- tests/Backend/PostponeRuntimeBackendTest.php | 5 +++-- 9 files changed, 15 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index 92e993f7..0ba6975d 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,7 @@ "symfony/console": "^4.4", "symfony/dependency-injection": "^4.4", "symfony/event-dispatcher": "^4.4", + "symfony/event-dispatcher-contracts": "^1.1", "symfony/form": "^4.4", "symfony/http-foundation": "^4.4", "symfony/http-kernel": "^4.4", diff --git a/src/Backend/AMQPBackend.php b/src/Backend/AMQPBackend.php index e6089b2a..b96883bb 100644 --- a/src/Backend/AMQPBackend.php +++ b/src/Backend/AMQPBackend.php @@ -226,7 +226,7 @@ public function handle(MessageInterface $message, EventDispatcherInterface $disp $amqpMessage = $message->getValue('interopMessage'); try { - $dispatcher->dispatch($message->getType(), $event); + $dispatcher->dispatch($event, $message->getType()); $this->consumer->acknowledge($amqpMessage); diff --git a/src/Backend/MessageManagerBackend.php b/src/Backend/MessageManagerBackend.php index 0d6f04f3..335bbce9 100644 --- a/src/Backend/MessageManagerBackend.php +++ b/src/Backend/MessageManagerBackend.php @@ -146,7 +146,7 @@ public function handle(MessageInterface $message, EventDispatcherInterface $disp $message->setState(MessageInterface::STATE_IN_PROGRESS); $this->messageManager->save($message); - $dispatcher->dispatch($message->getType(), $event); + $dispatcher->dispatch($event, $message->getType()); $message->setCompletedAt(new \DateTime()); $message->setState(MessageInterface::STATE_DONE); diff --git a/src/Backend/RuntimeBackend.php b/src/Backend/RuntimeBackend.php index a73a96d3..5c21f54e 100644 --- a/src/Backend/RuntimeBackend.php +++ b/src/Backend/RuntimeBackend.php @@ -93,7 +93,7 @@ public function handle(MessageInterface $message, EventDispatcherInterface $disp $event = new ConsumerEvent($message); try { - $dispatcher->dispatch($message->getType(), $event); + $dispatcher->dispatch($event, $message->getType()); $message->setCompletedAt(new \DateTime()); $message->setState(MessageInterface::STATE_DONE); diff --git a/src/Command/ConsumerHandlerCommand.php b/src/Command/ConsumerHandlerCommand.php index 7d6d0679..eb3bb44a 100644 --- a/src/Command/ConsumerHandlerCommand.php +++ b/src/Command/ConsumerHandlerCommand.php @@ -23,6 +23,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; class ConsumerHandlerCommand extends ContainerAwareCommand { @@ -135,8 +136,8 @@ public function execute(InputInterface $input, OutputInterface $output) } $this->getEventDispatcher()->dispatch( - IterateEvent::EVENT_NAME, - new IterateEvent($iterator, $backend, $message) + new IterateEvent($iterator, $backend, $message), + IterateEvent::EVENT_NAME ); if ($input->getOption('iteration') && $i >= (int) $input->getOption('iteration')) { diff --git a/src/Command/RestartCommand.php b/src/Command/RestartCommand.php index de1cb3f3..8db1d54a 100644 --- a/src/Command/RestartCommand.php +++ b/src/Command/RestartCommand.php @@ -22,6 +22,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; class RestartCommand extends ContainerAwareCommand { @@ -80,6 +81,7 @@ public function execute(InputInterface $input, OutputInterface $output) } } + /** @var EventDispatcherInterface $eventDispatcher */ $eventDispatcher = $this->getContainer()->get('event_dispatcher'); foreach ($messages as $message) { @@ -98,7 +100,7 @@ public function execute(InputInterface $input, OutputInterface $output) )); if ($pullMode) { - $eventDispatcher->dispatch(IterateEvent::EVENT_NAME, new IterateEvent($messages, null, $newMessage)); + $eventDispatcher->dispatch(new IterateEvent($messages, null, $newMessage), IterateEvent::EVENT_NAME); } } diff --git a/src/Consumer/ConsumerEvent.php b/src/Consumer/ConsumerEvent.php index 15cae097..d453ee41 100644 --- a/src/Consumer/ConsumerEvent.php +++ b/src/Consumer/ConsumerEvent.php @@ -14,7 +14,7 @@ namespace Sonata\NotificationBundle\Consumer; use Sonata\NotificationBundle\Model\MessageInterface; -use Symfony\Component\EventDispatcher\Event; +use Symfony\Contracts\EventDispatcher\Event; class ConsumerEvent extends Event implements ConsumerEventInterface { diff --git a/src/Event/IterateEvent.php b/src/Event/IterateEvent.php index 7738a1b3..429aad3c 100644 --- a/src/Event/IterateEvent.php +++ b/src/Event/IterateEvent.php @@ -16,7 +16,7 @@ use Sonata\NotificationBundle\Backend\BackendInterface; use Sonata\NotificationBundle\Iterator\MessageIteratorInterface; use Sonata\NotificationBundle\Model\MessageInterface; -use Symfony\Component\EventDispatcher\Event; +use Symfony\Contracts\EventDispatcher\Event; /** * Event for ConsumerHandlerCommand iterations event. diff --git a/tests/Backend/PostponeRuntimeBackendTest.php b/tests/Backend/PostponeRuntimeBackendTest.php index b1cce756..a7d67368 100644 --- a/tests/Backend/PostponeRuntimeBackendTest.php +++ b/tests/Backend/PostponeRuntimeBackendTest.php @@ -19,6 +19,7 @@ use Sonata\NotificationBundle\Model\MessageInterface; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\GenericEvent; use ZendDiagnostics\Result\Success; /** @@ -90,7 +91,7 @@ public function testLiveEnvironment() $phpunit->passed = true; }); - $dispatcher->dispatch('kernel.terminate'); + $dispatcher->dispatch(new GenericEvent(), 'kernel.terminate'); $this->assertTrue($phpunit->passed); $this->assertSame(MessageInterface::STATE_DONE, $message->getState()); @@ -125,7 +126,7 @@ public function testRecursiveMessage() $phpunit->passed2 = true; }); - $dispatcher->dispatch('kernel.terminate'); + $dispatcher->dispatch(new GenericEvent(), 'kernel.terminate'); $this->assertTrue($phpunit->passed1); $this->assertTrue($phpunit->passed2);