bystro / domain-events-publisher
Publisher of domain events. Publishes events into RabbitMQ. It can be used to Bounded Context integrations.
1.0.3
2019-06-25 10:37 UTC
Requires
- php: ^7.1
- jms/serializer: ^1.2
- php-amqplib/php-amqplib: ^2.1
- ramsey/uuid: ^3.0
Requires (Dev)
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2025-04-25 23:42:51 UTC
README
About
Publisher of domain events into RabbitMQ. It can be used to Bounded Context integrations.
Inspired and based on https://github.com/dddinphp/ddd
Thanks to: Carlos(https://github.com/carlosbuenosvinos), Christian(https://github.com/theUniC) nd Keyvan(https://github.com/keyvanakbary).
Requirements
- PHP 7.1
- ext-bcmath
- ext-sockets(optionaly)
Usage
- Create and subscribe RabbitMQ aware subscriber.
<?php use Bystro\DomainEventPublisher\Domain\DomainEventPublisher; use Bystro\DomainEventPublisher\Infrastructure\MessageProducerDomainEventSubscriber; use Bystro\DomainEventPublisher\Infrastructure\RabbitMqMessageProducer; use PhpAmqpLib\Connection\AMQPStreamConnection; $messageProducer = new RabbitMqMessageProducer( new AMQPStreamConnection('127.0.0.1', 5672, 'guest', 'guest') ); $messageProducer->open('example-exchange-name'); DomainEventPublisher::instance()->subscribe( new MessageProducerDomainEventSubscriber( $messageProducer ) );
- Create Domain Event
<?php namespace yourApp/namespace; use Bystro\DomainEventPublisher\Domain\DomainEvent; class FileSavedEvent implements DomainEvent { private /* string */ $filename; private /* string */ $destinationPath; private /* \DateTimeImmutable */ $occurredOn; public function __construct(string $filename, string $destinationPath) { $this->filename = $filename; $this->destinationPath = $destinationPath; $this->occurredOn = new \DateTimeImmutable(); } public function filename(): string { return $this->filename; } public function destinationPath(): string { return $this->destinationPath; } public function occurredOn(): \DateTimeImmutable { return $this->occurredOn; } }
- Publish your FileSavedEvent Domain Event.
<?php namespace yourApp/namespace; use Bystro\DomainEventPublisher\Domain\DomainEventPublisher; class File { private /* string */ $filename; private /* string */ $contents; public function __construct(string $filename, string $contents) { $this->filename = $filename; $this->contents = $contents; } public function save(string $destinationPath): void { file_put_contents($destinationPath . $this->filename, $this->contents); DomainEventPublisher::instance()->publish( new FileSavedEvent($this->name, $destinationPath) ); } }
FileSavedEvent is published in example-exchange-name queue.
Install
Composer
In command line
composer require bystro/domain-events-publisher
or composer.json
"require": {
"bystro/domain-events-publisher": "^1.0"
}