From cb486731ba0c9d49d0dacd75c095fd27f6be3b88 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Fri, 2 Feb 2024 10:06:33 +0800 Subject: [PATCH] Added amqpMessage property to events for `amqp` (#6518) --- src/Consumer.php | 4 ++-- src/Event/AfterConsume.php | 8 +++++++- src/Event/BeforeConsume.php | 12 ++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Consumer.php b/src/Consumer.php index c48413d..cf4248b 100644 --- a/src/Consumer.php +++ b/src/Consumer.php @@ -171,9 +171,9 @@ protected function getCallback(ConsumerMessageInterface $consumerMessage, AMQPMe try { $data = $consumerMessage->unserialize($message->getBody()); - $this->eventDispatcher?->dispatch(new BeforeConsume($consumerMessage)); + $this->eventDispatcher?->dispatch(new BeforeConsume($consumerMessage, $message)); $result = $consumerMessage->consumeMessage($data, $message); - $this->eventDispatcher?->dispatch(new AfterConsume($consumerMessage, $result)); + $this->eventDispatcher?->dispatch(new AfterConsume($consumerMessage, $result, $message)); } catch (Throwable $exception) { $this->eventDispatcher?->dispatch(new FailToConsume($consumerMessage, $exception, $message)); if ($this->container->has(FormatterInterface::class)) { diff --git a/src/Event/AfterConsume.php b/src/Event/AfterConsume.php index 3fc1048..d5bbeca 100644 --- a/src/Event/AfterConsume.php +++ b/src/Event/AfterConsume.php @@ -13,10 +13,11 @@ use Hyperf\Amqp\Message\ConsumerMessageInterface; use Hyperf\Amqp\Result; +use PhpAmqpLib\Message\AMQPMessage; class AfterConsume extends ConsumeEvent { - public function __construct(ConsumerMessageInterface $message, protected Result $result) + public function __construct(ConsumerMessageInterface $message, protected Result $result, protected AMQPMessage $amqpMessage) { parent::__construct($message); } @@ -25,4 +26,9 @@ public function getResult(): Result { return $this->result; } + + public function getAMQPMessage(): AMQPMessage + { + return $this->amqpMessage; + } } diff --git a/src/Event/BeforeConsume.php b/src/Event/BeforeConsume.php index b5e9aef..4f5d874 100644 --- a/src/Event/BeforeConsume.php +++ b/src/Event/BeforeConsume.php @@ -11,6 +11,18 @@ */ namespace Hyperf\Amqp\Event; +use Hyperf\Amqp\Message\ConsumerMessageInterface; +use PhpAmqpLib\Message\AMQPMessage; + class BeforeConsume extends ConsumeEvent { + public function __construct(ConsumerMessageInterface $message, protected AMQPMessage $amqpMessage) + { + parent::__construct($message); + } + + public function getAMQPMessage(): AMQPMessage + { + return $this->amqpMessage; + } }