Stars: 120
Forks: 17
Pull Requests: 13
Issues: 6
Watchers: 8
Last Updated: 2022-03-21 13:05:13
EventSauce is an easy way to introduce event sourcing into PHP projects. This package allows EventSauce to make use of Laravel's migrations, models and jobs. It can also help with generating code for commands and events. If you want to use EventSauce in a Laravel app, this package is the way to go!
Before using laravel-eventsauce you should already know how to work with EventSauce.
Here's a quick example of how to create a new aggregate root and matching repository. Let's run this command:
php artisan make:aggregate-root "MyDomain\MyAggregateRoot"
The App\MyDomain\MyAggregateRoot
and App\MyDomain\MyAggregateRootRepository
classes will have been created. A migration to create my_aggregate_root_domain_messages
will have been added to your application too. This is how MyAggregateRootRepository
looks like:
namespace App\MyDomain;
use App\Domain\Account\Projectors\AccountProjector;
use App\Domain\Account\Projectors\TransactionCountProjector;
use Spatie\LaravelEventSauce\AggregateRootRepository;
/** @method \App\MyDomain\MyAggregateRoot retrieve */
class MyAggregateRootRepository extends AggregateRootRepository
{
/** @var string */
protected $aggregateRoot = MyAggregateRoot::class;
/** @var string */
protected $tableName = 'my_aggregate_root_domain_messages';
/** @var array */
protected $consumers = [
];
/** @var array */
protected $queuedConsumers = [
];
}
You can put classnames of consumers in the $consumers
array. Consumers in the $queuedConsumers
array will called and be passed their messages using a queued job.
The MyAggregateRootRepository
can be injected and used in any class. In this example we assume you've manually created a performMySpecialCommand
method on MyAggregateRoot
:
namespace App\MyDomain;
class CommandHandler
{
/** @var \EventSauce\EventSourcing\AggregateRootRepository */
private $repository;
public function __construct(MyAggregateRootRepository $repository)
{
$this->repository = $repository;
}
public function handle(object $command)
{
$aggregateRootId = $command->identifier();
$aggregateRoot = $this->repository->retrieve($aggregateRootId);
try {
if ($command instanceof MySpecialCommand) {
$aggregateRoot->performMySpecialCommand($command);
}
} finally {
$this->repository->persist($aggregateRoot);
}
}
}
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
composer require spatie/laravel-eventsauce
Next you must publish the eventsauce
config file.
php artisan vendor:publish --provider="Spatie\LaravelEventSauce\EventSauceServiceProvider" --tag="config"
This is the contents of the file that will be publish to config/eventsauce.php
:
return [
/*
* Types, commands and events can be generated starting from a yaml file.
* Here you can specify the input and the output of the code generation.
*
* More info on code generation here:
* https://eventsauce.io/docs/getting-started/create-events-and-commands
*/
'code_generation' => [
[
'input_yaml_file' => null,
'output_file' => null,
],
],
/*
* This connection name will be used to store messages. When
* set to null the default connection will be used.
*/
'database_connection' => null,
/*
* This class will be used to store messages.
*
* You may change this to any class that implements
* \EventSauce\EventSourcing\MessageRepository
*/
'message_repository' => \Spatie\LaravelEventSauce\MessageRepository::class,
/*
* This class will be used to put EventSauce messages on the queue.
*
* You may change this to any class that extends
* \Spatie\LaravelEventSauce\QueuedMessageJob::class
*/
'queued_message_job' => \Spatie\LaravelEventSauce\QueuedMessageJob::class,
];
An aggregate root and matching repository can be generated used this command
php artisan make:aggregate-root "MyDomain\MyAggregateRoot"
This command will create App\MyDomain\MyAggregateRoot
and App\MyDomain\MyAggregateRootRepository
.
This is how MyAggregateRootRepository
looks like:
namespace App\MyDomain;
use App\Domain\Account\Projectors\AccountProjector;
use App\Domain\Account\Projectors\TransactionCountProjector;
use Spatie\LaravelEventSauce\AggregateRootRepository;
/** @method \App\MyDomain\MyAggregateRoot retrieve */
class MyAggregateRootRepository extends AggregateRootRepository
{
/** @var string */
protected $aggregateRoot = MyAggregateRoot::class;
/** @var string */
protected $tableName = 'my_aggregate_root_domain_messages';
/** @var array */
protected $consumers = [
];
/** @var array */
protected $queuedConsumers = [
];
}
If you repository doesn't need consumers or queued consumers, you can safely remove those variables. The only required variable is $aggregateRoot
.
Of course you can also manually create an aggregate root repository. Just create a class, let it extendSpatie\LaravelEventSauce\AggregateRootRepository
. Next, put the fully qualified classname of your aggregate root in a protected $aggregateRoot
property. Finally add a $tableName
property containing the name of the table where you want to store domain messages.
The $aggregateRoot
property should contain the fully qualied class name of an aggregate root. A valid aggregate root is any class that implements EventSauce\EventSourcing\AggregateRoot
Consumers are classes that receive all events and do something with them, for example creation a projection. The $consumers
property should be an array that contains class names of consumers. A valid consumer is any class that implements EventSauce\EventSourcing\Consumer
.
Unless you need the result of a consumer in the same request as your command or event is fired, it's recommanded to let a consumer to perform it's work on a queue. The $queuedConsumers
property should be an array that contains class names of consumers. A valid consumer is any class that implements EventSauce\EventSourcing\Consumer
.
If there are any message that needs to be sent to any of these consumers, the package will dispatch a Spatie\LaravelEventSauce\QueuedMessageJob
by default.
By default Spatie\LaravelEventSauce\QueuedMessageJob
is used to pass messages to queued consumers. You can customized this job by setting the queued_message_job
entry in the eventsauce
config file to the class of your custom job. A valid job is any class that extends Spatie\LaravelEventSauce\QueuedMessageJob
.
Changing the queued_message_job
entry will change the default job of all aggregate root repositories. If you want to change the job class for a specific repository add a $queuedMessageJob
property to that repository.
Here is an example:
// ...
class MyAggregateRootRepository extends AggregateRootRepository
{
// ...
protected $queuedMessageJob = MyCustomJob::class;
}
You can use that custom job to add properties to control the timeout, max attempts and the queue to be used. You can read more on how to configure a job in the Laravel docs on queueing.
Here's an example of a custom job.
use Spatie\LaravelEventSauce\QueuedMessageJob;
class MyCustomJob extends QueuedMessageJob
{
/*
* The name of the connection the job should be sent to.
*/
public $connection = 'my-custom-connection';
/*
* The name of the queue the job should be sent to.
*/
public $queue = 'my-custom-queue';
/*
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 5;
/*
* The number of seconds the job can run before timing out.
*
* @var int
*/
public $timeout = 120;
/*
* The number of seconds before the job should be made available.
*
* @var int|null
*/
public $delay;
}
The $tableName
property on your aggregate root repository determines where messages are being stored. You can change this to any name you want as long as you've created a a table with that name that has the following columns:
Schema::create('custom_table_name', function (Blueprint $table) {
$table->increments('id');
$table->string('event_id', 36);
$table->string('event_type', 100);
$table->string('aggregate_root_id', 36)->nullable()->index();
$table->dateTime('recorded_at', 6)->index();
$table->text('payload');
});
Laravel has support for multiple database connections. By default the aggregate root will use Laravel's default connection. If you want all your aggregate roots to use a the same alternative connection then specify that connection name in the connection
property of the eventsauce
config file.
If you want let a specific repository use an alternative connection, you can just specify it's name in the $connection
property
// ...
class MyAggregateRootRepository extends AggregateRootRepository
{
// ...
protected $connection = 'connection-name';
}
We can generate types, events and commands from you starting from a yaml file. You can read more on the contents of the yaml file and the generated output in the "Defining command and events using Yaml" section of the EventSauce docs.
To generate code, fill in the keys in the code_generation
parts of the eventsauce
config file and execute this command.
php artisan eventsauce:generate
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
The initial version of this package was based upon a development version of LaravelEventSauce.
The MIT License (MIT). Please see License File for more information.