Download the PHP package brueggemann/esf without Composer
On this page you can find all versions of the php package brueggemann/esf. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package esf
EventSourcingFoundation
Installing :
Using Composer - execute
Setup :
Register the ESFoundationServiceProvider in the bootstrap/app.php
For the ESF Facade uncomment:
For Redis the RedisServiceProvider is needed:
and the correct configuration has to be loaded:
In config/database.php a configuration similar to this is needed:
.env
The QueryRepository, EventStore, AggregateProjectionRepository and EventBus have both in memory and redis implementations.
Artisan :
There are a few Artisan Commands to help create the basic classes needed.
Usage :
Commands :
The entrypoint to the Event Sourced part of your application should be a CommandHandler.
If an AggregateProjectionRepository is needed for the CommandHandler add
If a CommandBus is in use, the CommandHandler has to be registered in the AppServiceProvider:
In the handleCOMMAND methods of the created CommandHandler an AggregateProjection can be loaded either by:
or if the aggregateProjectionRepository is injected in the constructor
Events can be either stored with:
or if the eventStore is injected in the constructor
In the created Command classes rules may be defined in the standart laravel validation way:
A Commands constructor takes any form of payload and validates it against the requirements of the defined rules() method.
- If no rules are defined any payload is acceped.
- If keys in the payload are not defined in the rules they are thrown out.
- If values in the payload are not passing validation an Exception is thrown.
Best is to define a named array as payload:
It is possible to either retrieve the payload as a whole:
or by key:
A CommandHandler is either called direcly:
or using the COMMANDBUS:
Aggregates :
Aggregates are split into three parts:
- logic: AggregateRoot
- representation: AggregateRootProjection
- validation: AggregateRootValidator
If a Validator is needed for the AggregateRoot add:
In the applyThatEVENT methods of the created AggregateRoot class the data from the $EVENT can be applied to the $AGGREGATEROOTPROJECTION
The created Event class works similar to the Command class. Rules may be defined, that determine the events payload validation.
If similar Rules are valid both for a Command and an Event it is recommended to use a ValueObject to represent both the rules and a value per instance.
These rules then can be imported into the COMMAND or/and EVENT:
If created the AggregateRootValidator class can prevent an Event on being applied to an AggregateRootProjection by returning false when a given requirement of the AggregateProjection is not fulfilled. When the validation fails a FailedValidation exception is thrown.
The created AggregateRootProjection represents a state of an Aggregate. It contains multiple instantiated ValueObjects.
To apply an Event to an AggregateRootProjection using the AggregateRoots logic there are multiple options:
In 1 and 2 it is possible to choose the AggregateRoots logic, where in 3 the default logic is used.
Every Event that is applied to an AggregateRootProjection is saved and can be retrieved
These Events then are commitable via the EventBus or if needed directly via the EventStore
Queries :
Since there are no means of retrieving Events or even Aggregates based on a specific pattern, like the where clause in SQL the accumulation of data is done via Queries
A Query should represent a page or view. To manage Queries the QueryRepository is needed.
The add method saves a key-value pair. If the value is updated, a new entry under the same key is inserted.
To retrieve a query the get method is used. If only the key is provided, the most recent query is returned. If additionally an index is provided, the corresponding older query is returned.
The QueryRepository can be used to save data of long lasting calculations, crawlers or only indirectly persisted data.
EventListener :
The EventListener is a basic implementation of the listener pattern. Any EventListener needs to be subscribed to the EventBus it is supposed to be listening on. A good place to do this is in the AppServiceProvider:
If synchronus handling of eventdata is required, but cannot be done in an AggregateRoot, this is a good place to create and update a query.