Skip to content
This repository has been archived by the owner on Jul 20, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from madewithlove/develop
Browse files Browse the repository at this point in the history
First version
  • Loading branch information
bramdevries committed Apr 22, 2016
2 parents 9511ccc + e3f009e commit 057051d
Show file tree
Hide file tree
Showing 19 changed files with 666 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
composer.lock
34 changes: 34 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
use Symfony\CS\Config\Config;
use Symfony\CS\Finder\DefaultFinder;
use Symfony\CS\Fixer\Contrib\HeaderCommentFixer;
use Symfony\CS\FixerInterface;

$finder = DefaultFinder::create()->in(['src', 'tests']);
$header = <<< EOF
This file is part of LaravelCqrsEs
(c) Madewithlove <[email protected]>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;

HeaderCommentFixer::setHeader($header);

return Config::create()
->level(FixerInterface::SYMFONY_LEVEL)
->fixers([
'ereg_to_preg',
'header_comment',
'multiline_spaces_before_semicolon',
'ordered_use',
'php4_constructor',
'phpdoc_order',
'short_array_syntax',
'short_echo_tag',
'strict',
'strict_param',
])
->setUsingCache(true)
->finder($finder);
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Use Docker environment
sudo: false

# Setup build matrix
language: php
php:
- 5.6
- 7

env:
matrix:
- PREFER_LOWEST="--prefer-lowest"
- PREFER_LOWEST=""

# Dependencies
before_install:
- composer self-update

install:
- travis_retry composer update --no-interaction --prefer-source --dev $PREFER_LOWEST

script: composer test
20 changes: 20 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2015 Madewithlove

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
# laravel-cqrs-es

A package to kickstart your CQRS/ES development in Laravel using the Broadway event store.

## Installation

```
$ composer require madewithlove/laravel-cqrs-es
```

## Configuration

Add the service provider to config/app.php:

```
Madewithlove\LaravelCqrsEs\ServiceProvider::class
```


## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
42 changes: 42 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "madewithlove/laravel-cqrs-es",
"description": "A Laravel package to kick start CQRS\ES projects using Broadway and Tactician.",
"license": "MIT",
"keywords": [
"laravel",
"broadway",
"event sourcing",
"ES",
"CQRS",
"Domain Driven Design",
"DDD"
],
"authors": [
{
"name": "Madewithlove",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.6.0",
"doctrine/dbal": "^2.5",
"broadway/broadway": "^0.9",
"illuminate/console": "^5.1",
"illuminate/database": "^5.1",
"illuminate/support": "^5.1",
},
"require-dev": {
"fabpot/php-cs-fixer": "2.0.*@dev",
"phpunit/phpunit": "^4.7"
},
"autoload": {
"psr-4": {
"Madewithlove\\LaravelCqrsEs\\": "src/"
}
},
"suggest": {
"madewithlove/tactician-laravel": "Laravel wrapper for a configurable command bus",
},
"minimum-stability": "dev",
"prefer-stable": true
}
20 changes: 20 additions & 0 deletions config/broadway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Event Store configuration
|--------------------------------------------------------------------------
| You can choose a driver, possible options are:
|
| dbal, inmemory
|
*/
'event-store' => [
'driver' => 'dbal',
'dbal' => [
'connection' => 'mysql',
'table' => 'event_store',
],
],
];
40 changes: 40 additions & 0 deletions database/migrations/2016_04_22_151617_init_event_store.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;

class InitEventStore extends Migration
{
/**
* @var string
*/
private $table;

public function __construct()
{
$this->table = Config::get('broadway.event-store.dbal.table', 'event_store');
}

public function up()
{
Schema::create($this->table, function (Blueprint $table) {
$table->increments('id');

$table->string('uuid', 36);
$table->integer('playhead')->unsigned();
$table->text('metadata');
$table->text('payload');
$table->string('recorded_on', 32);
$table->text('type');

$table->unique(['uuid', 'playhead']);
});
}

public function down()
{
Schema::drop($this->table);
}
}
62 changes: 62 additions & 0 deletions src/EventStore/Console/Replay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Madewithlove\LaravelCqrsEs\EventStore\Console;

use Illuminate\Console\Command;
use Illuminate\Contracts\Container\Container;
use Madewithlove\LaravelCqrsEs\EventStore\Services\Replay as ReplayService;

class Replay extends Command
{
/**
* The console command name.
* @var string
*/
protected $signature = 'event-store:replay {id?} {--types=}';

/**
* The console command description.
* @var string
*/
protected $description = 'Replay projections for an entity';

/**
* @var ReplayService
*/
protected $replayService;

/**
* @var Container
*/
protected $container;

/**
* @param ReplayService $replayService
* @param Container $container
*/
public function __construct(ReplayService $replayService, Container $container)
{
parent::__construct();

$this->replayService = $replayService;
$this->container = $container;
}

/**
* @return void
*/
public function handle()
{
$params = [];

if ($id = $this->argument('id')) {
$params['id'] = explode(',', $id);
}

if ($types = $this->option('types')) {
$params['types'] = explode(',', $types);
}

$this->replayService->replay($params);
}
}
56 changes: 56 additions & 0 deletions src/EventStore/EventStoreManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Madewithlove\LaravelCqrsEs\EventStore;

use Broadway\EventStore\DBALEventStore;
use Broadway\EventStore\InMemoryEventStore;
use Broadway\Serializer\SerializerInterface;
use Doctrine\DBAL\DriverManager;
use Illuminate\Support\Manager;

class EventStoreManager extends Manager
{
/**
* Get the default driver name.
*
* @return string
*/
public function getDefaultDriver()
{
return $this->app['config']->get('broadway.event-store.driver');
}

/**
* @return DBALEventStore
*/
protected function createDbalDriver()
{
$config = $this->app['config']->get('broadway.event-store.dbal');
$driver = $config['connection'];

$params = $this->app['config']->get("database.connections.{$driver}");
$params['dbname'] = $params['database'];
$params['user'] = $params['username'];
$params['driver'] = "pdo_$driver";

unset($params['database'], $params['username']);

$connection = DriverManager::getConnection($params);


return new DBALEventStore(
$connection,
$this->app->make(SerializerInterface::class),
$this->app->make(SerializerInterface::class),
array_get($config, 'table', 'event_store')
);
}

/**
* @return InMemoryEventStore
*/
protected function createInMemory()
{
return new InMemoryEventStore();
}
}
43 changes: 43 additions & 0 deletions src/EventStore/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Madewithlove\LaravelCqrsEs\EventStore;

use Broadway\EventStore\EventStoreInterface;
use Broadway\EventStore\Management\EventStoreManagementInterface;
use Madewithlove\LaravelCqrsEs\EventStore\Console\Replay;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
/**
* @var bool
*/
protected $defer = true;

/**
* @return null
*/
public function register()
{
$this->commands([
Replay::class,
]);

$this->app->singleton('event_store.driver', function () {
return (new EventStoreManager($this->app))->driver();
});

$this->app->alias(EventStoreInterface::class, 'event_store.driver');
$this->app->alias(EventStoreManagementInterface::class, 'event_store.driver');
}

/**
* @return array
*/
public function provides()
{
return [
EventStoreManagementInterface::class,
EventStoreInterface::class,
];
}
}
Loading

0 comments on commit 057051d

Please sign in to comment.