All Projects → gpslab → domain-event-bundle

gpslab / domain-event-bundle

Licence: MIT License
Library to create the domain layer of your DDD application

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to domain-event-bundle

Event Store Symfony Bundle
Event Store Symfony Bundle
Stars: ✭ 93 (+564.29%)
Mutual labels:  symfony-bundle, ddd
Service Bus Symfony Bundle
Symfony Bundle - PHP Lightweight Message Bus supporting CQRS
Stars: ✭ 90 (+542.86%)
Mutual labels:  symfony-bundle, ddd
coreddd
A set of open-source .NET libraries helping with domain-driven design (DDD) and CQRS
Stars: ✭ 68 (+385.71%)
Mutual labels:  ddd
sre-playground
🎯 A set of Site Reliability Engineering notes & challenges
Stars: ✭ 24 (+71.43%)
Mutual labels:  infrastructure
c3
𝗖𝟯 provides compliant AWS CDK components to various security standards.
Stars: ✭ 24 (+71.43%)
Mutual labels:  infrastructure
iam-ddd-cqrs-es-nestjs
Identity and Access Management
Stars: ✭ 34 (+142.86%)
Mutual labels:  ddd
EzCoreExtraBundle
Extra features for eZ Platform (v1.x compatible with eZ Publish 5.4)
Stars: ✭ 29 (+107.14%)
Mutual labels:  symfony-bundle
DscWorkshop
Blueprint for a full featured DSC project for Push / Pull with or without CI/CD
Stars: ✭ 151 (+978.57%)
Mutual labels:  infrastructure
domain
A collection of entities and helpers for creating domain objects and events
Stars: ✭ 18 (+28.57%)
Mutual labels:  ddd
acl-bundle
Integrates the ACL Security component into Symfony applications.
Stars: ✭ 91 (+550%)
Mutual labels:  symfony-bundle
Red-Baron
Automate creating resilient, disposable, secure and agile infrastructure for Red Teams
Stars: ✭ 326 (+2228.57%)
Mutual labels:  infrastructure
buchu
Use Cases - Uniform, auditable and secure use case library
Stars: ✭ 23 (+64.29%)
Mutual labels:  ddd
tinycore-kernel
TinyCore Linux kernel and module compile scripts. Download pre-built kernels and modules here: https://bintray.com/on-prem/tinycore-kernels/linux
Stars: ✭ 22 (+57.14%)
Mutual labels:  infrastructure
ddd-for-python
A domain-driven design framework for Python.
Stars: ✭ 30 (+114.29%)
Mutual labels:  ddd
app-from-scratch
Book about Clean Architecture and Clojure
Stars: ✭ 83 (+492.86%)
Mutual labels:  ddd
attribute-events
🔥 Fire events on attribute changes of your Eloquent model
Stars: ✭ 198 (+1314.29%)
Mutual labels:  ddd
clean-ddd-php-poc-contacts
A simple contact manager API to demonstrate the concepts of Clean Architecture and DDD with PHP 7.4+.
Stars: ✭ 31 (+121.43%)
Mutual labels:  ddd
planvelo-carte
Observatoire du Plan Vélo
Stars: ✭ 28 (+100%)
Mutual labels:  infrastructure
httpmate
Non-invasive, flexible and ultra-extendable http framework that offers you 3 modes of handling http requests - UseCase driven, low-level http and event-driven request handling, as well as a mix of those modes
Stars: ✭ 15 (+7.14%)
Mutual labels:  ddd
DDD
Domain-Driven Design example
Stars: ✭ 116 (+728.57%)
Mutual labels:  ddd

Latest Stable Version PHP Version Support Total Downloads Build Status Coverage Status Scrutinizer Code Quality License

Domain event bundle

Bundle to create the domain layer of your Domain-driven design (DDD) application.

This Symfony bundle is a wrapper for gpslab/domain-event, look it for more details.

Installation

Pretty simple with Composer, run:

composer req gpslab/domain-event-bundle

Configuration

Example configuration

gpslab_domain_event:
    # Event bus service
    # Support 'listener_located', 'queue' or a custom service
    # As a default used 'listener_located'
    bus: 'listener_located'

    # Event queue service
    # Support 'pull_memory', 'subscribe_executing' or a custom service
    # As a default used 'pull_memory'
    queue: 'pull_memory'

    # Event listener locator
    # Support 'symfony', 'container', 'direct_binding' or custom service
    # As a default used 'symfony'
    locator: 'symfony'

    # Publish domain events post a Doctrine flush event
    # As a default used 'false'
    publish_on_flush: true

Usage

Create a domain event

use GpsLab\Domain\Event\Event

class PurchaseOrderCreatedEvent implements Event
{
    private $customer_id;
    private $create_at;

    public function __construct(CustomerId $customer_id, \DateTimeImmutable $create_at)
    {
        $this->customer_id = $customer_id;
        $this->create_at = $create_at;
    }

    public function customerId(): CustomerId
    {
        return $this->customer_id;
    }

    public function createAt(): \DateTimeImmutable
    {
        return $this->create_at;
    }
}

Raise your event

use GpsLab\Domain\Event\Aggregator\AbstractAggregateEvents;

final class PurchaseOrder extends AbstractAggregateEvents
{
    private $customer_id;
    private $create_at;

    public function __construct(CustomerId $customer_id)
    {
        $this->customer_id = $customer_id;
        $this->create_at = new \DateTimeImmutable();

        $this->raise(new PurchaseOrderCreatedEvent($customer_id, $this->create_at));
    }
}

Create listener

use GpsLab\Domain\Event\Event;

class SendEmailOnPurchaseOrderCreated
{
    private $mailer;

    public function __construct(\Swift_Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function onPurchaseOrderCreated(PurchaseOrderCreatedEvent $event): void
    {
        $message = $this->mailer
            ->createMessage()
            ->setTo('[email protected]')
            ->setBody(sprintf(
                'Purchase order created at %s for customer #%s',
                $event->createAt()->format('Y-m-d'),
                $event->customerId()
            ));

        $this->mailer->send($message);
    }
}

Register event listener

services:
    SendEmailOnPurchaseOrderCreated:
        arguments: [ '@mailer' ]
        tags:
            - { name: domain_event.listener, event: PurchaseOrderCreatedEvent, method: onPurchaseOrderCreated }

Publish events in listener

use GpsLab\Domain\Event\Bus\EventBus;

// get event bus from DI container
$bus = $this->get(EventBus::class);

// do what you need to do on your Domain
$purchase_order = new PurchaseOrder(new CustomerId(1));

// this will clear the list of event in your AggregateEvents so an Event is trigger only once
$events = $purchase_order->pullEvents();

// You can have more than one event at a time.
foreach($events as $event) {
    $bus->publish($event);
}

// You can use one method
//$bus->pullAndPublish($purchase_order);

Listener method name

You do not need to specify the name of the event handler method. By default, the __invoke method is used.

use GpsLab\Domain\Event\Event;

class SendEmailOnPurchaseOrderCreated
{
    private $mailer;

    public function __construct(\Swift_Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function __invoke(PurchaseOrderCreatedEvent $event): void
    {
        $message = $this->mailer
            ->createMessage()
            ->setTo('[email protected]')
            ->setBody(sprintf(
                'Purchase order created at %s for customer #%s',
                $event->createAt()->format('Y-m-d'),
                $event->customerId()
            ));

        $this->mailer->send($message);
    }
}

Register event listener

services:
    SendEmailOnPurchaseOrderCreated:
        arguments: [ '@mailer' ]
        tags:
            - { name: domain_event.listener, event: PurchaseOrderCreatedEvent }

Event subscribers

Create subscriber

use GpsLab\Domain\Event\Event;
use GpsLab\Domain\Event\Listener\Subscriber;

class SendEmailOnPurchaseOrderCreated implements Subscriber
{
    private $mailer;

    public function __construct(\Swift_Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public static function subscribedEvents(): array
    {
        return [
            PurchaseOrderCreatedEvent::class => ['onPurchaseOrderCreated'],
        ];
    }

    public function onPurchaseOrderCreated(PurchaseOrderCreatedEvent $event): void
    {
        $message = $this->mailer
            ->createMessage()
            ->setTo('[email protected]')
            ->setBody(sprintf(
                'Purchase order created at %s for customer #%s',
                $event->createAt()->format('Y-m-d'),
                $event->customerId()
            ));

        $this->mailer->send($message);
    }
}

Register event subscriber

services:
    SendEmailOnPurchaseOrderCreated:
        arguments: [ '@mailer' ]
        tags:
            - { name: domain_event.subscriber }

Use pull Predis queue

Install Predis with Composer, run:

composer require predis/predis

Register services:

services:
    # Predis
    Predis\Client:
        arguments: [ '127.0.0.1' ]

    # Events serializer for queue
    GpsLab\Domain\Event\Queue\Serializer\SymfonySerializer:
        arguments: [ '@serializer', 'json' ]

    # Predis event queue
    GpsLab\Domain\Event\Queue\Pull\PredisPullEventQueue:
        arguments:
            - '@Predis\Client'
            - '@GpsLab\Domain\Event\Queue\Serializer\SymfonySerializer'
            - '@logger'
            - 'event_queue_name'

Change config for use custom queue:

gpslab_domain_event:
    queue: 'GpsLab\Domain\Event\Queue\Pull\PredisPullEventQueue'

And now you can use custom queue:

use GpsLab\Domain\Event\Queue\EventQueue;

$container->get(EventQueue::class)->publish($domain_event);

In latter pull events from queue:

use GpsLab\Domain\Event\Queue\EventQueue;

$queue = $container->get(EventQueue::class);
$bus = $container->get(EventQueue::class);

while ($event = $queue->pull()) {
    $bus->publish($event);
}

Use Predis subscribe queue

Install Predis PubSub adapter with Composer, run:

composer require superbalist/php-pubsub-redis

Register services:

services:
    # Predis
    Predis\Client:
        arguments: [ '127.0.0.1' ]

    # Predis PubSub adapter
    Superbalist\PubSub\Redis\RedisPubSubAdapter:
        arguments: [ '@Predis\Client' ]

    # Events serializer for queue
    GpsLab\Domain\Event\Queue\Serializer\SymfonySerializer:
        arguments: [ '@serializer', 'json' ]

    # Predis event queue
    GpsLab\Domain\Event\Queue\Subscribe\PredisSubscribeEventQueue:
        arguments:
            - '@Superbalist\PubSub\Redis\RedisPubSubAdapter'
            - '@GpsLab\Domain\Event\Queue\Serializer\SymfonySerializer'
            - '@logger'
            - 'event_queue_name'

Change config for use custom queue:

gpslab_domain_event:
    queue: 'GpsLab\Domain\Event\Queue\Subscribe\PredisSubscribeEventQueue'

And now you can use custom queue:

use GpsLab\Domain\Event\Queue\EventQueue;

$container->get(EventQueue::class)->publish($domain_event);

Subscribe on the queue:

use GpsLab\Domain\Event\Queue\EventQueue;

$container->get(EventQueue::class)->subscribe(function (Event $event) {
    // do somthing
});

Note

You can use subscribe handlers as a services and tag it for optimize register.

Many queues

You can use many queues for separation the flows. For example, you want to handle events of different Bounded Contexts separately from each other.

services:
    acme.domain.purchase_order.event.queue:
        class: GpsLab\Domain\Event\Queue\Pull\PredisPullEventQueue
        arguments:
            - '@Superbalist\PubSub\Redis\RedisPubSubAdapter'
            - '@GpsLab\Domain\Event\Queue\Serializer\SymfonySerializer'
            - '@logger'
            - 'purchase_order_event_queue'

    acme.domain.article_comment.event.queue:
        class: GpsLab\Domain\Event\Queue\Pull\PredisPullEventQueue
        arguments:
            - '@Superbalist\PubSub\Redis\RedisPubSubAdapter'
            - '@GpsLab\Domain\Event\Queue\Serializer\SymfonySerializer'
            - '@logger'
            - 'article_comment_event_queue'

And now you can use a different queues.

In Purchase order Bounded Contexts.

$event = new PurchaseOrderCreatedEvent(
    new CustomerId(1),
    new \DateTimeImmutable()
);

$container->get('acme.domain.purchase_order.event.queue')->publish($event);

In Article comment Bounded Contexts.

$event = new ArticleCommentedEvent(
    new ArticleId(1),
    new AuthorId(1),
    $comment
    new \DateTimeImmutable()
);

$container->get('acme.domain.article_comment.event.queue')->publish($event);

Note

Similarly, you can split the subscribe queues.

License

This bundle is under the MIT license. See the complete license in the file: LICENSE

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].