2017 © Pedro Peláez
 

library domain-event-dispatcher

Dispatch or defer domain events from a singleton. Handy if you're using DDD and need a singleton domain dispatcher for models.

image

ashleydawson/domain-event-dispatcher

Dispatch or defer domain events from a singleton. Handy if you're using DDD and need a singleton domain dispatcher for models.

  • Saturday, January 28, 2017
  • by AshleyDawson
  • Repository
  • 1 Watchers
  • 1 Stars
  • 9,994 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 8 % Grown

The README.md

Domain Event Dispatcher

Build Status, (*1)

Singleton domain event dispatcher to be used during DDD. The domain event dispatcher is a singleton so it can be easily used from within your model., (*2)

Installation

Install via Composer using the following command:, (*3)

$ composer require ashleydawson/domain-event-dispatcher

Basic Usage

Define an event to be dispatched. Event classes can be anything, and should be easily serialized., (*4)

<?php

namespace Acme\Event;

class MyEvent
{
}

Define a listener for the event, (*5)

<?php

namespace Acme\EventListener;

use Acme\Event\MyEvent;

class MyTypedEventListener
{
    // Domain event listeners must implement the __invoke() magic method with only one argument
    public function __invoke(MyEvent $event)
    {
        // Do something useful here...
    }
}

Put it all together, add your listeners and dispatch., (*6)

<?php

require __DIR__.'/vendor/autoload.php';

use AshleyDawson\DomainEventDispatcher\DomainEventDispatcher;
use Acme\Event\MyEvent;
use Acme\EventListener\MyTypedEventListener;

// Add your listener to the dispatcher
DomainEventDispatcher::getInstance()->addListener(
    new MyTypedEventListener()
);

// Dispatch the event from within your domain, e.g. from your domain model, etc.
DomainEventDispatcher::getInstance()->dispatch(
    new MyEvent()
);

Listener Types

There are two types of listeners:, (*7)

  • General - invoked by any domain event.
  • Typed - invoked by events of a corresponding type.

General Listener

In a previous example, we saw the event dispatcher used for typed events where the event argument is type hinted in the __invoke() method of the event listener. General listeners are invoked by all events and don't have a type hint. For example:, (*8)

<?php

namespace Acme\EventListener;

class MyEventGeneralListener
{
    // Event is not type hinted as this is a general event listener - it will be invoked by all events
    public function __invoke($event)
    {
        // Do something useful here...
    }
}

Typed Listener

A typed listener only responds to a particular type of domain event:, (*9)

<?php

namespace Acme\EventListener;

use Acme\Event\MyEvent;

class MyEventGeneralListener
{
    // This listener will only respond to MyEvent events
    public function __invoke(MyEvent $event)
    {
        // Do something useful here...
    }
}

Deferred Events

Sometimes it might be useful to dispatch events later in the application flow. To do this, events may be deferred and dispatched later., (*10)

<?php

require __DIR__.'/vendor/autoload.php';

use AshleyDawson\DomainEventDispatcher\DomainEventDispatcher;
use Acme\Event\MyEvent;
use Acme\EventListener\MyTypedEventListener;

// Add your listener to the dispatcher
DomainEventDispatcher::getInstance()->addListener(
    new MyTypedEventListener()
);

// Dispatch the event from within your domain, e.g. from your domain model, etc.
DomainEventDispatcher::getInstance()->defer(
    new MyEvent()
);

// From a different level of the application, dispatch the deferred events
DomainEventDispatcher::getInstance()->dispatchDeferred();

Domain Event Storage

For event publication, notification or auditing purposes you may need to store events in an event store. To do this, simply implement the EventStoreInterface:, (*11)

<?php

namespace Acme\DomainEventStorage;

use AshleyDawson\DomainEventDispatcher\EventStorage\EventStoreInterface;

class InMemoryEventStore implements EventStoreInterface
{
    private $events = [];

    public function append($event)
    {
        $this->events[] = $event;
    }
}

Then configure the event dispatcher to use the event store:, (*12)

<?php

use AshleyDawson\DomainEventDispatcher\DomainEventDispatcher;
use Acme\DomainEventStorage\InMemoryEventStore;

DomainEventDispatcher::getInstance()->setEventStore(
    new InMemoryEventStore()
);

Important: If configured with an event store, the event dispatcher will attempt to store every event. If you don't want the event dispatcher to store a particular event, implement DisposableEventInterface on the event:, (*13)

<?php

namespace Acme\Event;

use AshleyDawson\DomainEventDispatcher\EventStorage\DisposableEventInterface;

class MyDisposableEvent implements DisposableEventInterface
{
}

Tests

To run the test suite, run the following:, (*14)

$ php bin/phpunit

The Versions

28/01 2017

dev-master

9999999-dev

Dispatch or defer domain events from a singleton. Handy if you're using DDD and need a singleton domain dispatcher for models.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Ashley Dawson

domain dispatcher subscriber event listener ddd

23/12 2016

1.0.6

1.0.6.0

Dispatch or defer domain events from a singleton. Handy if you're using DDD and need a singleton domain dispatcher for models.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Ashley Dawson

domain dispatcher subscriber event listener ddd

22/12 2016

1.0.5

1.0.5.0

Dispatch or defer domain events from a singleton. Handy if you're using DDD and need a singleton domain dispatcher for models.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Ashley Dawson

domain dispatcher subscriber event listener ddd

22/12 2016

1.0.4

1.0.4.0

Dispatch or defer domain events from a singleton. Handy if you're using DDD and need a singleton domain dispatcher for models.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Ashley Dawson

domain dispatcher subscriber event listener ddd

22/12 2016

1.0.3

1.0.3.0

Dispatch or defer domain events from a singleton. Handy if you're using DDD and need a singleton domain dispatcher for models.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Ashley Dawson

domain dispatcher subscriber event listener ddd

22/12 2016

1.0.2

1.0.2.0

Dispatch or defer domain events from a singleton. Handy if you're using DDD and need a singleton domain dispatcher for models.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Ashley Dawson

domain dispatcher subscriber event listener ddd

22/12 2016

1.0.1

1.0.1.0

Dispatch or defer domain events from a singleton. Handy if you're using DDD and need a singleton domain dispatcher for models.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Ashley Dawson

domain dispatcher subscriber event listener ddd

21/12 2016

1.0.0

1.0.0.0

Dispatch or defer domain events from a singleton. Handy if you're using DDD and need a singleton domain dispatcher for models.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Ashley Dawson

domain dispatcher subscriber event listener ddd