From 96d9a557c797b70aaeb57d9c8c3ec5e361b5dd3c Mon Sep 17 00:00:00 2001 From: alireza Date: Wed, 22 Jun 2022 09:34:43 +0430 Subject: [PATCH 1/4] ability to declare topic exchanges --- src/Commands/DeclareExchanges.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Commands/DeclareExchanges.php b/src/Commands/DeclareExchanges.php index e3c5729..7fac628 100644 --- a/src/Commands/DeclareExchanges.php +++ b/src/Commands/DeclareExchanges.php @@ -39,7 +39,7 @@ public function handle(RabbitMQ $rabbitmq) $rabbitmq ->exchange() ->durable() - ->type('fanout') + ->type($this->determineExchangeType($event)) ->name(class_basename($event)) ->declare(); @@ -67,4 +67,19 @@ private function getEvents(): Collection return collect($events)->keys(); } + + private function determineExchangeType(string $event): string + { + $reflection = (new ReflectionClass($event)); + + if (!$reflection->hasProperty('exchangeType')) { + return 'fanout'; + } + + $property = $reflection->getProperty('exchangeType'); + + $property->setAccessible(true); + + return $property->getValue(); + } } From d87f8d04d4a42f056879d01acd12f108fe9d2b0d Mon Sep 17 00:00:00 2001 From: alireza Date: Wed, 22 Jun 2022 09:53:54 +0430 Subject: [PATCH 2/4] ability to declare routing key on event level --- src/RabbitMQDispatcher.php | 5 ++++- src/RabbitMQMessage.php | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/RabbitMQDispatcher.php b/src/RabbitMQDispatcher.php index 1608794..fca0f90 100644 --- a/src/RabbitMQDispatcher.php +++ b/src/RabbitMQDispatcher.php @@ -18,7 +18,7 @@ class RabbitMQDispatcher extends Dispatcher */ public function dispatch($event, $payload = [], $halt = false) { - if (! $event instanceof ShouldPublish) { + if (!$event instanceof ShouldPublish) { return parent::dispatch($event, $payload, $halt); } @@ -29,6 +29,9 @@ public function dispatch($event, $payload = [], $halt = false) ->message() ->persistent() ->viaExchange(class_basename($event)) + ->when(method_exists($event, 'routingKey'), fn(RabbitMQMessage $message) => $message + ->route($event->routingKey()) + ) ->withPayload( array_map( function ($property) { diff --git a/src/RabbitMQMessage.php b/src/RabbitMQMessage.php index 3d62d05..c3fdb07 100644 --- a/src/RabbitMQMessage.php +++ b/src/RabbitMQMessage.php @@ -2,11 +2,14 @@ namespace Sokanacademy\RabbitMQ; +use Illuminate\Support\Traits\Conditionable; use PhpAmqpLib\Channel\AMQPChannel; use PhpAmqpLib\Message\AMQPMessage; class RabbitMQMessage { + use Conditionable; + /** * @var AMQPChannel */ From 58579b770376318f05569d6f6d815112fd33bd91 Mon Sep 17 00:00:00 2001 From: alireza Date: Wed, 22 Jun 2022 09:54:06 +0430 Subject: [PATCH 3/4] ability to consume with routing key --- src/Commands/ConsumeMessages.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Commands/ConsumeMessages.php b/src/Commands/ConsumeMessages.php index b24b868..8c794f6 100644 --- a/src/Commands/ConsumeMessages.php +++ b/src/Commands/ConsumeMessages.php @@ -46,7 +46,13 @@ public function handle(RabbitMQ $rabbitmq) ->declare(); foreach (config('rabbitmq.consumers') as $event) { - $queue->bindTo(class_basename($event)); + if (is_string($event)) { + $queue->bindTo(class_basename($event)); + } + + if (is_array($event)) { + $queue->bindTo(class_basename($event[0]), $event[1]); + } } $rabbitmq From 6a5cd6f7b430c7826412ff073ee32ecec2a1375a Mon Sep 17 00:00:00 2001 From: alirzaj Date: Wed, 22 Jun 2022 05:24:21 +0000 Subject: [PATCH 4/4] Fix styling --- src/Commands/DeclareExchanges.php | 2 +- src/RabbitMQDispatcher.php | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Commands/DeclareExchanges.php b/src/Commands/DeclareExchanges.php index 7fac628..ca9d808 100644 --- a/src/Commands/DeclareExchanges.php +++ b/src/Commands/DeclareExchanges.php @@ -72,7 +72,7 @@ private function determineExchangeType(string $event): string { $reflection = (new ReflectionClass($event)); - if (!$reflection->hasProperty('exchangeType')) { + if (! $reflection->hasProperty('exchangeType')) { return 'fanout'; } diff --git a/src/RabbitMQDispatcher.php b/src/RabbitMQDispatcher.php index fca0f90..764d628 100644 --- a/src/RabbitMQDispatcher.php +++ b/src/RabbitMQDispatcher.php @@ -18,7 +18,7 @@ class RabbitMQDispatcher extends Dispatcher */ public function dispatch($event, $payload = [], $halt = false) { - if (!$event instanceof ShouldPublish) { + if (! $event instanceof ShouldPublish) { return parent::dispatch($event, $payload, $halt); } @@ -29,7 +29,9 @@ public function dispatch($event, $payload = [], $halt = false) ->message() ->persistent() ->viaExchange(class_basename($event)) - ->when(method_exists($event, 'routingKey'), fn(RabbitMQMessage $message) => $message + ->when( + method_exists($event, 'routingKey'), + fn (RabbitMQMessage $message) => $message ->route($event->routingKey()) ) ->withPayload(