Configuration
Info
You can find out more about event sourcing in the library documentation. This documentation is limited to bundle integration and configuration.
Tip
We provide a default configuration that should work for most projects.
Aggregate
A path must be specified for Event Sourcing to know where to look for your aggregates. If you want you can use glob patterns to specify multiple paths.
Or use an array to specify multiple paths.patchlevel_event_sourcing:
aggregates:
- '%kernel.project_dir%/src/Hotel/Domain'
- '%kernel.project_dir%/src/Room/Domain'
Note
The library will automatically register all classes marked with the #[Aggregate]
attribute in the specified paths.
Tip
If you want to learn more about aggregates, read the library documentation.
Events
A path must be specified for Event Sourcing to know where to look for your events. If you want you can use glob patterns to specify multiple paths.
Or use an array to specify multiple paths.patchlevel_event_sourcing:
events:
- '%kernel.project_dir%/src/Hotel/Domain/Event'
- '%kernel.project_dir%/src/Room/Domain/Event'
Tip
If you want to learn more about events, read the library documentation.
Custom Headers
If you want to implement custom headers for your application, you must specify the paths to look for those headers. If you want you can use glob patterns to specify multiple paths.
Or use an array to specify multiple paths.patchlevel_event_sourcing:
headers:
- '%kernel.project_dir%/src/Hotel/Domain/Header'
- '%kernel.project_dir%/src/Room/Domain/Header'
Tip
If you want to learn more about custom headers, read the library documentation.
Connection
You have to specify the connection url to the event store.
Note
You can find out more about how to create a connection here
Connection for Projections
Per default, our event sourcing connection is not available to use in your application. But you can create a dedicated connection that you can use for your projections.
patchlevel_event_sourcing:
connection:
url: '%env(EVENTSTORE_URL)%'
provide_dedicated_connection: true
Warning
If you use doctrine migrations, you should exclude you projection tables from the schema generation. The schema is managed by the subscription engine and should not be managed by doctrine.
Tip
You can autowire the connection in your services like this:
Doctrine Bundle
If you have installed the doctrine bundle, you can also define the connection via doctrine and then use it in the store.
doctrine:
dbal:
connections:
eventstore:
url: '%env(EVENTSTORE_URL)%'
patchlevel_event_sourcing:
connection:
service: doctrine.dbal.eventstore_connection
Danger
Do not use the same connection for event sourcing and your projections, otherwise you may run into transaction problems.
Warning
If you want to use the same connection as doctrine orm, then you have to set the flag merge_orm_schema
.
Otherwise you should avoid using the same connection as other tools.
Note
You can find out more about the dbal configuration here.
If you are using Doctrine for your projections too, you need to create a dedicated connection for this.
You can do this by defining a new connection named projection
in the doctrine.yaml
file
and use the same connection url as for the event store.
doctrine:
dbal:
connections:
eventstore:
url: '%env(EVENTSTORE_URL)%'
projection:
url: '%env(EVENTSTORE_URL)%'
patchlevel_event_sourcing:
connection:
service: doctrine.dbal.eventstore_connection
Warning
You should exclude your projection tables from the schema generation.
Then you can use this connection in your projections.
If you are using autowiring you can inject the right connection Connection $projectionConnection
parameter name.
The prefix projection
is used to identify the connection.
namespace App\Projection;
use Doctrine\DBAL\Connection;
use Patchlevel\EventSourcing\Attribute\Projector;
#[Projector('my_projection')]
class MyProjection
{
public function __construct(
private readonly Connection $projectionConnection,
) {
}
}
Store
The store and schema is configurable.
Change Store type
You can change the store type of the event store.
Following store types are available:dbal_aggregate
defaultdbal_stream
experimentalin_memory
custom
Note
If you use custom
store type, you need to set the service id under patchlevel_event_sourcing.store.service
.
Change table Name
You can change the table name of the event store.
Read Only Mode
For dbal_aggregate
and dbal_stream
store types you can activate the read only mode.
Readings are possible, but if you try to write, an exception StoreIsReadOnly
is thrown.
Tip
This is useful if you have maintenance work on the event store and you want to avoid side effects.
Merge ORM Schema
You can also merge the schema with doctrine orm. You have to set the following flag for this:
Warning
If you want to merge the schema, then the same doctrine connection must be used as with the doctrine orm. Otherwise errors may occur!
Note
All schema relevant commands are removed if you activate this option. You should use the doctrine commands then.
Tip
If you want to learn more about store, read the library documentation.
Kernel Reset
Only available in in_memory
store. If you want to reset the store after each kernel request, you can activate this option.
So you can avoid side effects between the tests.
Data Migration
If you want to migrate from your current store to a new store, you can use the following configuration.
This register a new store and a new cli command event-sourcing:store:migrate
.
You can define translators to translate the old events to the new store.
Here is an example for a migration from dbal_aggregate
to dbal_stream
.
patchlevel_event_sourcing:
store:
migrate_to_new_store:
type: 'dbal_stream'
options:
table_name: 'my_stream_store'
translators:
- Patchlevel\EventSourcing\Message\Translator\AggregateToStreamHeaderTranslator
Danger
Make sure that you use different table names for the old and new store. Otherwise your event store will be destroyed.
Tip
Set the read_only
flag to true
for the old store to avoid side effects
and missing events during the migration.
Migration
You can use doctrine migrations to manage the schema.
patchlevel_event_sourcing:
migration:
namespace: EventSourcingMigrations
path: "%kernel.project_dir%/migrations"
Subscription
Tip
You can find out more about subscriptions in the library documentation.
Store
You can change where the subscription engine stores its necessary information about the subscription.
Default is dbal
, which means it stores it in the same DB that is used by the dbal event store.
Otherwise you can choose between the following stores:
dbal
defaultin_memory
static_in_memory
custom
patchlevel_event_sourcing:
subscription:
store:
type: 'custom' # default is 'dbal'
service: 'my_subscription_store'
Tip
If you are using the doctrine-test-bundle,
you can use the static_in_memory
store for testing.
Catch Up
If aggregates are used in the processors and new events are generated there,
then they are not part of the current subscription engine run
and will only be processed during the next run or boot.
This is usually not a problem in dev or prod environment because a worker is used
and these events will be processed at some point. But in testing it is not so easy.
For this reason, you can activate the catch_up
option.
Throw on Error
You can activate the throw_on_error
option to throw an exception if a subscription engine run has an error.
This is useful for testing or development to get directly feedback if something is wrong.
Warning
This option should not be used in production. The normal behavior is to log the error and continue.
Run After Aggregate Save
If you want to run the subscription engine after an aggregate is saved, you can activate this option. This is useful for testing or development, so you don't have run a worker to process the events.
Auto Setup
If you want to automatically setup the subscription engine, you can activate this option. This is useful for development, so you don't have to setup the subscription engine manually.
Note
This works only before each http requests and not if you use the console commands.
Rebuild After File Change
If you want to rebuild the subscription engine after a file change, you can activate this option. This is also useful for development, so you don't have to rebuild the projections manually.
Note
This works only before each http requests and not if you use the console commands.
Command Bus
You can also enable and register our handlers in symfony messenger. These handlers allow you to use your aggregates as command handlers.
Note
You can find out more about the command bus integration here.
Default the handlers will be registered for all buses. You can also specify a specific bus. Before you have to define the bus in the messenger configuration.
Tip
This is also useful if you have a event bus and a command bus.
And then you can specify the bus in the configuration.
Event Bus
You can enable the event bus to listen for events and messages synchronously. But you should consider using the subscription engine for this.
Note
Default is the patchlevel event bus.
Patchlevel (Default) Event Bus
First of all we have our own default event bus.
This works best with the library, as the #[Subscribe]
attribute is used there, among other things.
Note
You don't have to specify this as it is the default value.
Symfony Event Bus
But you can also use Symfony Messenger. To do this, you first have to define a suitable message bus. This must be "allow_no_handlers" so that this messenger can be an event bus according to the definition.
We can then use this messenger or event bus in event sourcing: Since the event bus was replaced, event sourcing own attributes no longer work. You use the Symfony attributes instead.use Patchlevel\EventSourcing\EventBus\Message;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler('event.bus')]
class SmsNotificationHandler
{
public function __invoke(Message $message): void
{
if (!$message instanceof GuestIsCheckedIn) {
return;
}
// ... do some work - like sending an SMS message!
}
}
PSR-14 Event Bus
You can also use any other event bus that implements the PSR-14 standard.
Note
Like the Symfony event bus, the event sourcing attributes no longer work here. You have to use the system that comes with the respective psr14 implementation.
Custom Event Bus
You can also use your own event bus that implements the Patchlevel\EventSourcing\EventBus\EventBus
interface.
Note
Like the Symfony event bus, the event sourcing attributes no longer work here. You have to use the system that comes with the respective custom implementation.
Snapshot
You can use symfony cache to define the target of the snapshot store.
framework:
cache:
default_redis_provider: 'redis://localhost'
pools:
event_sourcing.cache:
adapter: cache.adapter.redis
namespace App\Profile\Domain;
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot;
use Patchlevel\EventSourcing\Attribute\Aggregate;
use Patchlevel\EventSourcing\Attribute\Snapshot;
#[Aggregate(name: 'profile')]
#[Snapshot('default')]
final class Profile extends BasicAggregateRoot
{
// ...
}
Note
You can find out more about snapshots here.
Cryptography
You can use the library to encrypt and decrypt personal data. For this you need to enable the crypto shredding.
You can change the algorithm if you want.Note
You can find out more about personal data here.
Clock
The clock is used to return the current time as DateTimeImmutable.
Freeze Clock
You can freeze the clock for testing purposes:
Note
If freeze is not set, then the system clock is used.
Symfony Clock
Since symfony 6.2 there is a clock implementation based on psr-20 that you can use.
PSR-20
You can also use your own implementation of your choice. They only have to implement the interface of the psr-20. You can then specify this service here: