New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

robin-malfait/event-sourcing

An EventSourcing Package
136 7
Install
composer require robin-malfait/event-sourcing
Latest Version:v3.0.3
PHP:>=5.5.0
Laravel:5
License:MIT
Last Updated:Oct 30, 2016
Links: GitHub  ·  Packagist
Maintainer: RobinMalfait

EventSourcing

Software License

This is a simple EventSourcing package that you can use in your projects. This project is written using PSR2

Install

Via Composer

$ composer require robin-malfait/event-sourcing

Usage

Register the service provider

'providers' => [
    ...
    \EventSourcing\Laravel\EventSourcingServiceProvider::class,
]

Publish the configuration file

php artisan vendor:publish --provider="EventSourcing\Laravel\EventSourcingServiceProvider"

The config file looks like this:

Config File

You can now tweak some configurations

Last but not least make the event store table:

php artisan event-sourcing:table

Update v1 to v2

If you are still using the first version you better update to version 2. You will have less problems in the future, I promise.

In Version 2 we give each DomainEvent the responsibility to give data and receive data. Those methods are

serialize(); Which returns an array of serialized data

deserialize(array $data); Which has a parameter with the data that basically comes from the serialize method. This method should also return an instance of the current event.

For Example:

<?php namespace App\Users\Events;

use EventSourcing\Domain\DomainEvent;

class UserWasRegistered implements DomainEvent
{
    private $user_id;

    private $email;

    private $password; // Yes, this is encrypted

    public function __construct($user_id, $email, $password)
    {
        $this->user_id = $user_id;
        $this->email = $email;
        $this->password = $password;
    }

    /**
     * @return UserId
     */
    public function getAggregateId()
    {
        return $this->user_id;
    }

    public function getMetaData() 
    {
        return []; // Could be for example the logged in user, ...
    }

    /**
     * @return array
     */
    public function serialize()
    {
        return [
            'user_id' => $this->user_id,
            'email' => $this->email,
            'password' => $this->password
        ];
    }

    /**
     * @param array $data
     * @return mixed
     */
    public static function deserialize(array $data)
    {
        return new static(
            $data['user_id'],
            $data['email'],
            $data['password']
        );
    }
}

Once you have defined every serialize / deserialize method in your events you can start the migration process.

In your database rename eventstore to eventstore_backup

Now you can run the following command in your terminal:

php artisan event-sourcing:table

This will create the eventstore, now you should see 2 tables in your database

  1. eventstore_backup => Your old table with all data in
  2. eventstore => Your new empty table

I also have written a helper method to do the migration now.

php artisan event-sourcing:1to2 eventstore_backup eventstore

Or you can also just run the following command because eventstore_backup and eventstore are the defaults.

php artisan event-sourcing:1to2

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email malfait.robin@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Related Packages

smoothphp/cqrs-es-framework-laravel

Laravel adapter for the SmoothPhp CQRS-ES-Framework

46,153 15
eduostia/laravel-cqrs-es

Simple Cqrs and Event Sourcing for Laravel 5.

34 6
softonic/laravel-transactional-event-publisher

Softonic Laravel Transactional Event Publisher

20,782 4
elber/laravel-clean-architecture

DDD + Clean Architecture scaffolding for Laravel

1,315 3
arseto/lumencqrs

CQRS Template for Laravel/Lumen

200 3

Version History

Version Released PHP Laravel License
v3.0.3 >=5.5.0 ^5.3 MIT
v3.0.2 >=5.5.0 ^5.3 MIT
v3.0.1 >=5.5.0 ^5.3 MIT
v3.0.0 >=5.5.0 ^5.3 MIT
v2.0.4 >=5.5.0 ^5.1 MIT