• Stars
    star
    123
  • Rank 280,089 (Top 6 %)
  • Language
    PHP
  • License
    Other
  • Created over 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CQRS/Event Sourcing for laravel. Laravel adapter for Broadway.

Laravel Broadway

SensioLabsInsight Scrutinizer Code Quality Latest Stable Version Total Downloads Latest Unstable Version License

Laravel Broadway is an adapter for the Broadway package.

It binds all needed interfaces for Broadway.

For reference, I've built a demo laravel application that uses this package and some event sourcing techniques.

Laravel 5 compatible package

Laravel version Package version
~4.2 ~0.2
~5.0 ~0.3
~5.1+ ~2.0

Installation

Install via composer

composer require nwidart/laravel-broadway=~1.0

Service Providers

Laravel 5.5 (Auto discovery)

If you are using Laravel 5.5, this package provides auto discovery, meaning you can start coding โ€“ the Global Service Provider is already added. In case you need just some Service Providers, please add the following section to your applications composer.json:

 "extra": {
    "laravel": {
      "dont-discover": [
        "nWidart/Laravel-broadway"
      ]
    }
  }

Laravel 5.4

To finish the installation you need to add the service providers.

You have a choice here, you can either use the main Service Provider which will load the following:

Or choose to use only the Service providers you need. Don't know what you need ? Use the Global Service Provider provided.

Global Service Provider

   Nwidart\LaravelBroadway\LaravelBroadwayServiceProvider::class

Separate Service Providers

  • CommandBus

    Nwidart\LaravelBroadway\Broadway\CommandServiceProvider::class
  • EventBus

    Nwidart\LaravelBroadway\Broadway\EventServiceProvider::class
  • Serializers

    Nwidart\LaravelBroadway\Broadway\SerializersServiceProvider::class
  • EventStorage

    Nwidart\LaravelBroadway\Broadway\EventStorageServiceProvider::class
  • ReadModel

    Nwidart\LaravelBroadway\Broadway\ReadModelServiceProvider::class
  • MetadataEnricher

    Nwidart\LaravelBroadway\Broadway\MetadataEnricherServiceProvider::class
  • Support

    Nwidart\LaravelBroadway\Broadway\SupportServiceProvider::class

(Optional) Publish configuration file and migration

php artisan vendor:publish

This will publish a config/broadway.php file and a database/migrations/create_event_store_table.php file.

(Optional) Run migration

Last step, run the migration that was published in the last step to create the event_store table.

If you haven't published the vendor files, you can use the command explained below:

php artisan broadway:event-store:migrate table_name

Configuration

Event Store

To create the event store you can call the following command:

php artisan broadway:event-store:migrate table_name

In the configuration file, you can choose which driver to use as an event store and which connection to use.

'event-store' => [
    'table' => 'event_store',
    'driver' => 'dbal',
    'connection' => 'default',
],

Once done, you can bind your EventStoreRepositories in a Service Provider like so:

$this->app->bind(\Modules\Parts\Repositories\EventStorePartRepository::class, function ($app) {
    $eventStore = $app[\Broadway\EventStore\EventStore::class];
    $eventBus = $app[\Broadway\EventHandling\EventBus::class];
    return new MysqlEventStorePartRepository($eventStore, $eventBus);
});

For an in memory Event Store, all you need to do is change the driver in the configuration file and probably add a new event store repository implementation with an adequate name.

Read Model

To set a read model in your application you first need to set the wanted read model in the package configuration.

Once that's done you can bind your ReadModelRepositories in a Service Provider like so:

$this->app->bind(\Modules\Parts\Repositories\ReadModelPartRepository::class, function ($app) {
    $serializer = $app[\Broadway\Serializer\Serializer::class];
    return new ElasticSearchReadModelPartRepository($app['Elasticsearch'], $serializer);
});

For an In Memory read model as an example:

$this->app->bind(\Modules\Parts\Repositories\ReadModelPartRepository::class, function ($app) {
    $serializer = $app[\Broadway\Serializer\Serializer::class];
    return new InMemoryReadModelPartRepository($app['Inmemory'], $serializer);
});

See the demo laravel application and specifically the Service Provider for a working example.

Registering subscribers

Command handlers

To let broadway know which handlers are available you need to bind in the Laravel IoC container a key named broadway.command-subscribers as a singleton.

It's important to know Command Handlers classes in broadway need to get a Event Store repository injected.

Now just pass either an array of command handlers to the laravelbroadway.command.registry key out the IoC Container or just one class, like so:

$partCommandHandler = new PartCommandHandler($this->app[\Modules\Parts\Repositories\EventStorePartRepository::class]);
$someOtherCommandHandler = new SomeOtherCommandHandler($this->app[\Modules\Things\Repositories\EventStoreSomeRepository::class]);

$this->app['laravelbroadway.command.registry']->subscribe([
    $partCommandHandler,
    $someOtherCommandHandler
]);

// OR

$this->app['laravelbroadway.command.registry']->subscribe($partCommandHandler);
$this->app['laravelbroadway.command.registry']->subscribe($someOtherCommandHandler);

Event subscribers

This is pretty much the same as the command handlers, except that the event subscriber (or listener) needs an Read Model repository.

Example:

$partsThatWereManfacturedProjector = new PartsThatWereManufacturedProjector($this->app[\Modules\Parts\Repositories\ReadModelPartRepository::class]);
$someOtherProjector = new SomeOtherProjector($this->app[\Modules\Things\Repositories\ReadModelSomeRepository::class]);

$this->app['laravelbroadway.event.registry']->subscribe([
    $partsThatWereManfacturedProjector,
    $someOtherProjector
]);

// OR

$this->app['laravelbroadway.event.registry']->subscribe($partsThatWereManfacturedProjector);
$this->app['laravelbroadway.event.registry']->subscribe($someOtherProjector);

Metadata Enricher

Broadways event store table comes with a field called "metadata". Here we can store all kind of stuff which should be saved together with the particular event, but which is does not fit to the domain aka the payload.

For example you like to store the ID of the current logged-in user or the IP or ...

Broadway uses Decorators to manipulate the event stream. A decorator consumes one or more Enrichers, which provide the actual data (user ID, IP). Right before saving the event to the stream, the decorator will loop through the registered enrichers and apply the data.

The following example assumes you added the global ServiceProvider of this package or at least the Nwidart\LaravelBroadway\Broadway\MetadataEnricherServiceProvider.

First we create the enricher. In this example lets assume we are interested in the logged-in user. The enricher will add the user ID to the metadata and returns the modified metadata object. However, in some cases โ€“ like in Unit Tests - there is no logged-in user available. To tackle this, the user ID can injected via constructor.

// CreatorEnricher.php

class CreatorEnricher implements MetadataEnricher
{
    /** @var int $creatorId */
    private $creatorId;


    /**
     * The constructor
     *
     * @param int $creatorId
     */
    public function __construct($creatorId = null)
    {
        $this->creatorId = $creatorId;
    }


    /**
     * @param Metadata $metadata
     * @return Metadata
     */
    public function enrich(Metadata $metadata)
    {
        if ($this->creatorId !== null) {
            $id = $this->creatorId;
        } else {
            $id = Auth::user()->id;
        }

        return $metadata->merge(Metadata::kv('createorId', $id));
    }
}

Second you need to register the Enricher to the decorator and pass the decorator to your repository

// YourServiceProvider.php

/**
 * Register the Metadata enrichers
 */
private function registerEnrichers()
{
    $enricher = new CreatorEnricher();
    $this->app['laravelbroadway.enricher.registry']->subscribe([$enricher]);
}

$this->app->bind(\Modules\Parts\Repositories\EventStorePartRepository::class, function ($app) {
    $eventStore = $app[\Broadway\EventStore\EventStore::class];
    $eventBus = $app[\Broadway\EventHandling\EventBus::class];
    
    $this->registerEnrichers();
    
    return new MysqlEventStorePartRepository($eventStore, $eventBus, $app[Connection::class], [$app[EventStreamDecorator::class]);
});

To retrieve the metadata you need to pass the DomainMessage as the 2nd parameter to an apply*-method in your projector.

// PartsThatWhereCreatedProjector.php

public function applyPartWasRenamedEvent(PartWasRenamedEvent $event, DomainMessage $domainMessage)
{
	$metaData = $domainMessage->getMetadata()->serialize();
	$creator = User::find($metaData['creatorId']);    
	
	// Do something with the user
}

All the rest are conventions from the Broadway package.


Changelog

License (MIT)

More Repositories

1

laravel-modules

Module Management In Laravel
PHP
5,236
star
2

DbExporter

Export your database as a Laravel Migration. And export your DB data as a seed file. Finally sync all this with your remote server.
PHP
372
star
3

laravel-menus

Laravel Menu Management
PHP
150
star
4

Laravel-Broadway-Demo

This is a test application using https://github.com/qandidate-labs/broadway
CSS
36
star
5

nwidart-docs

HTML
32
star
6

laravel-videoable

Attach Videos to your Eloquent Models
PHP
17
star
7

HttpStatus-l4-package

Laravel 4 Packages. Adds 403,404,500 and 503 customizable error pages.
PHP
14
star
8

laravel-actuator

Add health endpoints to your applications
PHP
13
star
9

laravel-normalizer

Laravel package to normalize your data before saving into the database.
PHP
12
star
10

portfolio

Source code of my personal portfolio.
CSS
10
star
11

projectLister

Laravel package to list repositories on Github, Bitbucket and Sourceforge.
PHP
9
star
12

MoneyFormatter

A package used for money formatting
PHP
9
star
13

phpstorm-vue-snippets

PHPStorm VueJS Live templates. Based of https://github.com/sdras/vue-vscode-snippets
9
star
14

vue-symfony4

Vue
6
star
15

tech-videos

Organise learning sessions in your company to watch tech videos.
Java
4
star
16

Themify-l5

PHP
4
star
17

composer-generator

Laravel 4 Composer.json generator service.
PHP
3
star
18

cubesNcircles

cubesNcircles
JavaScript
3
star
19

laravel-modules-custom-service-provider

PHP
2
star
20

Laravel-forecast

Laravel-forecast provides a service provider and a facade arround the Forecast-php wrapper.
PHP
2
star
21

forecast-php

PHP API Wrapper for Forecast.io
PHP
2
star
22

rematch-typescript

Using rematch in typescript projects (https://github.com/rematch/rematch)
TypeScript
2
star
23

activity

Activity lets you list your current activity on Github. Soon from Bitbucket as well.
PHP
2
star
24

MoneyFormatter-Laravel

Laravel integration for my MoneyFormatter Package
PHP
2
star
25

Alys-Google-Maps-Organiser

Laravel App, to manage clients and their corresponding markers for Google Maps.
PHP
2
star
26

pda

Personal Diablo Assistant
PHP
1
star
27

spring-modular-tests

Java
1
star
28

modules-routes-tests

https://github.com/nWidart/laravel-modules/issues/755
PHP
1
star
29

BugTrack

Simple bug tracking app
PHP
1
star
30

nw-cleanV2

PHP
1
star
31

trivago-cqrs-es-workshop-2017

Workshop from https://github.com/ShittySoft/trivago-cqrs-es-workshop-2017
PHP
1
star
32

Grimdawntrade

PHP
1
star
33

twitter-es

Twitter clone, in event sourcing with Prooph
PHP
1
star
34

spring-boot-bean-overrides

Trying to reproduce a bean overriding issue
Java
1
star
35

heaj_trashsquare

PHP
1
star
36

export-wrapper

A PHP wrapper around Sonata-Exporter, for ease of use
PHP
1
star
37

axon-webinar

cqrs/event sourcing in java with Axon framework
Java
1
star
38

PhilipBrownBasket

This is a test ecommerce application using philipbrown/basket package.
PHP
1
star
39

spring-boot-starter-jwt

Java
1
star
40

spring-data-multi-datasource

Java
1
star
41

Product-Custom-Post-Type---Taxonomy

A Wordpress Custom Post Type and Taxonomy.
PHP
1
star
42

Alys-Zonetrial-Wordpress-theme

PHP
1
star
43

ci_trashsquare

Trashsquare made on CodeIgniter
PHP
1
star
44

FAQcms

Multilang FAQ management system.
JavaScript
1
star