Skip to content

Commit

Permalink
Merge pull request #2 from sokanacademy/feature/topic
Browse files Browse the repository at this point in the history
topic exchange
  • Loading branch information
alirzaj authored Jun 22, 2022
2 parents 39a5a84 + 6a5cd6f commit 8e87ef7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Commands/ConsumeMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion src/Commands/DeclareExchanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function handle(RabbitMQ $rabbitmq)
$rabbitmq
->exchange()
->durable()
->type('fanout')
->type($this->determineExchangeType($event))
->name(class_basename($event))
->declare();

Expand Down Expand Up @@ -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();
}
}
5 changes: 5 additions & 0 deletions src/RabbitMQDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ 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) {
Expand Down
3 changes: 3 additions & 0 deletions src/RabbitMQMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 8e87ef7

Please sign in to comment.