Compare commits
2 Commits
main
...
8320fb4a84
| Author | SHA1 | Date | |
|---|---|---|---|
| 8320fb4a84 | |||
| c481c961fb |
10
README.md
10
README.md
@@ -33,9 +33,8 @@ return [
|
|||||||
|
|
||||||
### 1. Creating regular events for publishing to a RabbitMQ
|
### 1. Creating regular events for publishing to a RabbitMQ
|
||||||
|
|
||||||
Create an event that implements the `Broadcast` interface.
|
Create an event that implements the `Broadcast` interface. `Broadcast` interface
|
||||||
Your event should implements `JsonSerializable` interface if
|
extends `JsonSerializable` then your event should implements `jsonSerialize` method.
|
||||||
default serializer is used.
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
namespace App\Events;
|
namespace App\Events;
|
||||||
@@ -43,9 +42,8 @@ namespace App\Events;
|
|||||||
use Diffhead\PHP\LaravelRabbitMQ\Event\Broadcast;
|
use Diffhead\PHP\LaravelRabbitMQ\Event\Broadcast;
|
||||||
use Diffhead\PHP\LaravelRabbitMQ\Trait\BroadcastEvent;
|
use Diffhead\PHP\LaravelRabbitMQ\Trait\BroadcastEvent;
|
||||||
use Illuminate\Foundation\Events\Dispatchable;
|
use Illuminate\Foundation\Events\Dispatchable;
|
||||||
use JsonSerializable;
|
|
||||||
|
|
||||||
class UserCreated implements Broadcast, JsonSerializable
|
class UserCreated implements Broadcast
|
||||||
{
|
{
|
||||||
use Dispatchable, BroadcastEvent;
|
use Dispatchable, BroadcastEvent;
|
||||||
|
|
||||||
@@ -207,7 +205,6 @@ Consumer will listen rabbitmq bus and emit mapped event from as regular laravel
|
|||||||
#
|
#
|
||||||
# --connection=default - Connection name from config
|
# --connection=default - Connection name from config
|
||||||
# --queue=default - Queue
|
# --queue=default - Queue
|
||||||
# --queue-auto-delete - Queue auto-delete config
|
|
||||||
# --exchange=amq.direct - Exchange name
|
# --exchange=amq.direct - Exchange name
|
||||||
# --exchange-type=direct - Exchange type
|
# --exchange-type=direct - Exchange type
|
||||||
# --exchange-is-default - Exchange is default, required for default exchanges
|
# --exchange-is-default - Exchange is default, required for default exchanges
|
||||||
@@ -224,7 +221,6 @@ php artisan rabbitmq:consume
|
|||||||
php artisan rabbitmq:consume \
|
php artisan rabbitmq:consume \
|
||||||
--connection=default \
|
--connection=default \
|
||||||
--queue=service.users \
|
--queue=service.users \
|
||||||
--queue-auto-delete \
|
|
||||||
--exchange=amq.direct \
|
--exchange=amq.direct \
|
||||||
--exchange-type=direct \
|
--exchange-type=direct \
|
||||||
--routing-key=user.* \
|
--routing-key=user.* \
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"description": "A laravel package for events emitting between services using RabbitMQ as message broker.",
|
"description": "A laravel package for events emitting between services using RabbitMQ as message broker.",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"version": "1.2.1",
|
"version": "1.2.0",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"laravel", "rabbitmq", "event", "emit", "microservice",
|
"laravel", "rabbitmq", "event", "emit", "microservice",
|
||||||
"pipeline", "data exchanging", "message", "broker", "php8",
|
"pipeline", "data exchanging", "message", "broker", "php8",
|
||||||
|
|||||||
686
composer.lock
generated
686
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -24,7 +24,7 @@ class Consume extends Command
|
|||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $signature = 'rabbitmq:consume {--connection=} {--queue=} {--queue-auto-delete} {--exchange=} {--exchange-type=} {--exchange-is-default} {--routing-key=} {--tag=}';
|
protected $signature = 'rabbitmq:consume {--connection=} {--queue=} {--exchange=} {--exchange-type=} {--exchange-is-default} {--routing-key=} {--tag=}';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
@@ -99,9 +99,7 @@ class Consume extends Command
|
|||||||
{
|
{
|
||||||
return new Queue(
|
return new Queue(
|
||||||
name: $params->queue->value() ?? 'default',
|
name: $params->queue->value() ?? 'default',
|
||||||
declaration: new QueueDeclaration(
|
declaration: new QueueDeclaration(),
|
||||||
autoDelete: $params->queueAutoDelete->toBool()
|
|
||||||
),
|
|
||||||
bindings: new QueueBindings($params->routingKey->toString()),
|
bindings: new QueueBindings($params->routingKey->toString()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -109,8 +107,8 @@ class Consume extends Command
|
|||||||
private function getExchange(ConsumerParameters $params): Exchange
|
private function getExchange(ConsumerParameters $params): Exchange
|
||||||
{
|
{
|
||||||
return new Exchange(
|
return new Exchange(
|
||||||
name: $params->exchange->toString() ?: 'amq.direct',
|
name: $params->exchange->toString() ?? 'amq.direct',
|
||||||
type: $params->exchangeType->toString() ?: 'direct',
|
type: $params->exchangeType->toString() ?? 'direct',
|
||||||
isDefault: $params->exchangeIsDefault->toBool(),
|
isDefault: $params->exchangeIsDefault->toBool(),
|
||||||
declaration: new ExchangeDeclaration()
|
declaration: new ExchangeDeclaration()
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ use Diffhead\PHP\Dto\Property;
|
|||||||
/**
|
/**
|
||||||
* @property \Diffhead\PHP\Dto\Property<string|null> $connection
|
* @property \Diffhead\PHP\Dto\Property<string|null> $connection
|
||||||
* @property \Diffhead\PHP\Dto\Property<string|null> $queue
|
* @property \Diffhead\PHP\Dto\Property<string|null> $queue
|
||||||
* @property \Diffhead\PHP\Dto\Property<bool> $queueAutoDelete
|
|
||||||
* @property \Diffhead\PHP\Dto\Property<string|null> $exchange
|
* @property \Diffhead\PHP\Dto\Property<string|null> $exchange
|
||||||
* @property \Diffhead\PHP\Dto\Property<string|null> $routingKey
|
* @property \Diffhead\PHP\Dto\Property<string|null> $routingKey
|
||||||
* @property \Diffhead\PHP\Dto\Property<string|null> $exchangeType
|
* @property \Diffhead\PHP\Dto\Property<string|null> $exchangeType
|
||||||
@@ -21,7 +20,6 @@ class ConsumerParameters extends Dto
|
|||||||
{
|
{
|
||||||
protected Property $connection;
|
protected Property $connection;
|
||||||
protected Property $queue;
|
protected Property $queue;
|
||||||
protected Property $queueAutoDelete;
|
|
||||||
protected Property $exchange;
|
protected Property $exchange;
|
||||||
protected Property $routingKey;
|
protected Property $routingKey;
|
||||||
protected Property $exchangeType;
|
protected Property $exchangeType;
|
||||||
|
|||||||
@@ -33,21 +33,21 @@ class PublishEvent implements ShouldQueue
|
|||||||
$config = $this->configuration->getConnection($event->getConnection());
|
$config = $this->configuration->getConnection($event->getConnection());
|
||||||
|
|
||||||
$exchange = new Exchange(
|
$exchange = new Exchange(
|
||||||
declaration: new ExchangeDeclaration(),
|
|
||||||
name: $event->getExchange(),
|
name: $event->getExchange(),
|
||||||
type: $event->getExchangeType(),
|
type: $event->getExchangeType(),
|
||||||
isDefault: $event->getExchangeIsDefault(),
|
isDefault: $event->getExchangeIsDefault(),
|
||||||
|
declaration: new ExchangeDeclaration()
|
||||||
);
|
);
|
||||||
|
|
||||||
$queue = null;
|
$queue = null;
|
||||||
|
|
||||||
if ($event->getQueue()) {
|
if ($event->getQueue()) {
|
||||||
$queue = new Queue(
|
$queue = new Queue(
|
||||||
|
name: $event->getQueue(),
|
||||||
declaration: new QueueDeclaration(),
|
declaration: new QueueDeclaration(),
|
||||||
bindings: new QueueBindings(
|
bindings: new QueueBindings(
|
||||||
routingKey: $event->getRoutingKey()
|
routingKey: $event->getRoutingKey()
|
||||||
),
|
),
|
||||||
name: $event->getQueue(),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ namespace Diffhead\PHP\LaravelRabbitMQ\Object;
|
|||||||
class Exchange
|
class Exchange
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private ExchangeDeclaration $declaration,
|
|
||||||
private string $name = 'amq.direct',
|
private string $name = 'amq.direct',
|
||||||
private string $type = 'direct',
|
private string $type = 'direct',
|
||||||
private bool $isDefault = true,
|
private bool $isDefault = true,
|
||||||
|
private ExchangeDeclaration $declaration,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function name(): string
|
public function name(): string
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ namespace Diffhead\PHP\LaravelRabbitMQ\Object;
|
|||||||
class Queue
|
class Queue
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
private string $name = 'default',
|
||||||
private QueueDeclaration $declaration,
|
private QueueDeclaration $declaration,
|
||||||
private QueueBindings $bindings,
|
private QueueBindings $bindings,
|
||||||
private string $name = 'default',
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function name(): string
|
public function name(): string
|
||||||
|
|||||||
@@ -45,16 +45,6 @@ class EventMapper implements EventMapperInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$error = <<<ERR
|
throw new AssociatedEventNotFound(json_encode($payload));
|
||||||
Not found event.
|
|
||||||
|
|
||||||
Queue: %s
|
|
||||||
Routing key: %s
|
|
||||||
Payload: %s
|
|
||||||
ERR;
|
|
||||||
|
|
||||||
throw new AssociatedEventNotFound(
|
|
||||||
sprintf($error, $queueName, $routingKey, json_encode($payload))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user