Make sure you have Symfony Security installed:
composer require security
composer require chgst/chgst-bundle
Set your event repository service for persisting events to data store
# config/packages/chgst.yaml
chgst:
enable_listeners: true
# Make sure you disable listeners in test/dev environment
when@dev:
chgst:
enable_listeners: false
when@test:
chgst:
enable_listeners: false
Add repository service to your services configuration
# config/services.yaml
services:
Chgst\Event\RepositoryInterface:
public: true
class: Chgst\Event\ObjectRepository
arguments: [ '@doctrine_mongodb.odm.document_manager', 'App\Document\DefaultEvent' ] # or '@doctrine.orm.entity_manager'
Create Doctrine model class for your events
<?php
// src/Document/DefaultEvent.php
namespace App\Document;
use Chgst\Event\Event;
class DefaultEvent extends Event
{
protected string $id;
public function getId(): string
{
return $this->id;
}
public function setId(string $id): self
{
$this->id = $id;
return $this;
}
}
And add XML mapping
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
https://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
<document name="App\Document\DefaultEvent">
<id />
<field field-name="name" type="string" nullable="false" />
<field field-name="aggregateType" type="string" nullable="false" />
<field field-name="aggregateId" type="string" nullable="false" />
<field field-name="createdAt" type="date" nullable="false" />
<field field-name="createdBy" type="string" nullable="false" />
<field field-name="payload" type="hash" nullable="false" />
</document>
</doctrine-mongo-mapping>