PHPackages                             neos/event-sourcing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. neos/event-sourcing

ActiveNeos-package[Framework](/categories/framework)

neos/event-sourcing
===================

Lean and opinionated way to integrate Event Sourcing and CQRS pattern in your Flow framework package

2.7.2(6mo ago)45289.4k↑80.3%30[9 issues](https://github.com/neos/Neos.EventSourcing/issues)[4 PRs](https://github.com/neos/Neos.EventSourcing/pulls)4MITPHPPHP &gt;=8.0

Since Jan 30Pushed 6mo ago10 watchersCompare

[ Source](https://github.com/neos/Neos.EventSourcing)[ Packagist](https://packagist.org/packages/neos/event-sourcing)[ GitHub Sponsors](https://github.com/bwaidelich)[ GitHub Sponsors](https://github.com/robertlemke)[ RSS](/packages/neos-event-sourcing/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (34)Used By (4)

Event Sourcing and CQRS for Flow Framework and Symfony
======================================================

[](#event-sourcing-and-cqrs-for-flow-framework-and-symfony)

Library providing interfaces and implementations for event-sourced applications.

- **For Symfony, use [Neos.EventSourcingSymfonyBridge](https://github.com/neos/Neos.EventSourcingSymfonyBridge) which adapts this package to the Symfony World.**
- **For Neos/Flow, use this package directly.**

Getting started
---------------

[](#getting-started)

Install this package via composer:

```
composer require neos/event-sourcing
```

### Setting up an Event Store

[](#setting-up-an-event-store)

Since there could be multiple Event Stores simultaneously in one application, this package no longer comes with a pre-configured "default" store. It is just a matter of a couple of lines of YAML to configure a custom store:

*Configuration/Settings.yaml:*

```
Neos:
  EventSourcing:
    EventStore:
      stores:
        'Some.Package:EventStore':
          storage: 'Neos\EventSourcing\EventStore\Storage\Doctrine\DoctrineEventStorage'
```

This registers an Event Store, identified as "Some.Package:EventStore"[1](#f1), that uses the provided Doctrine storage adapter that persists events in a database table[2](#f2).

To make use of the newly configured Event Store one more step is required in order to finish the setup (in this case to create the corresponding database table):

```
./flow eventstore:setup Some.Package:EventStore
```

ℹ️ **Note...**> By default, the Event Store persists events in the same database that is used for Flow persistence. But because that can be configured otherwise, this table is not generated via Doctrine migrations. If your application relies on the events table to exist, you can of course still add a Doctrine migration for it.

### Obtaining the Event Store

[](#obtaining-the-event-store)

To get hold of an instance of a specific Event Store the `EventStoreFactory` can be used:

```
use Neos\EventSourcing\EventStore\EventStoreFactory;
use Neos\Flow\Annotations as Flow;

class SomeClass {

    /**
     * @Flow\Inject
     * @var EventStoreFactory
     */
    protected $eventStoreFactory;

    function someMethod() {
        $eventStore = $this->eventStoreFactory->create('Some.Package:EventStore');
    }
}
```

ℹ️ **Alternative ways...**Alternatively you can inject the Event Store directly:

```
use Neos\EventSourcing\EventStore\EventStore;
use Neos\Flow\Annotations as Flow;

class SomeClass {

    /**
     * @Flow\Inject
     * @var EventStore
     */
    protected $eventStore;

    function someMethod() {
        // $this->eventStore->...
    }
}
```

But in this case you have to specify *which* event store to be injected. This can be done easily using Flow's [Object Framework](https://flowframework.readthedocs.io/en/stable/TheDefinitiveGuide/PartIII/ObjectManagement.html#object-framework):

*Configuration/Objects.yaml:*

```
Some\Package\SomeClass:
  properties:
    'eventStore':
      object:
        factoryObjectName: Neos\EventSourcing\EventStore\EventStoreFactory
        arguments:
          1:
            value: 'Some.Package:EventStore'
```

If you use Flow 6.2 or newer, you can make use of the [virtual object configuration](https://flowframework.readthedocs.io/en/stable/TheDefinitiveGuide/PartIII/ObjectManagement.html#virtual-objects)to simplify the process:

*Configuration/Objects.yaml:*

```
'Some.Package:EventStore':
  className: Neos\EventSourcing\EventStore\EventStore
  factoryObjectName: Neos\EventSourcing\EventStore\EventStoreFactory
  arguments:
    1:
      value: 'Some.Package:EventStore'
```

```
use Neos\EventSourcing\EventStore\EventStore;
use Neos\Flow\Annotations as Flow;

class SomeClass {

    /**
     * @Flow\Inject(name="Some.Package:EventStore")
     * @var EventStore
     */
    protected $eventStore;
}
```

And, finally, if you happen to use the event store from many classes, you could of course create a custom Event Store facade like:

*Classes/CustomEventStore.php*

```
