PHPackages                             solidframe/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. solidframe/event-sourcing

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

solidframe/event-sourcing
=========================

Event Sourcing building blocks: EventStore, Snapshot, event-sourced AggregateRoot for SolidFrame

v0.1.0(1mo ago)03MITPHPPHP ^8.2

Since Apr 11Pushed 1mo agoCompare

[ Source](https://github.com/solidframe/event-sourcing)[ Packagist](https://packagist.org/packages/solidframe/event-sourcing)[ RSS](/packages/solidframe-event-sourcing/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

SolidFrame Event Sourcing
=========================

[](#solidframe-event-sourcing)

Event Sourcing building blocks: EventStore, Snapshot, and event-sourced AggregateRoot.

Store every state change as a domain event. Rebuild aggregate state by replaying events. Optimize with snapshots.

Installation
------------

[](#installation)

```
composer require solidframe/event-sourcing
```

Quick Start
-----------

[](#quick-start)

### Define an Event-Sourced Aggregate

[](#define-an-event-sourced-aggregate)

```
use SolidFrame\EventSourcing\Aggregate\AbstractEventSourcedAggregateRoot;
use SolidFrame\Core\Event\DomainEventInterface;

final class BankAccount extends AbstractEventSourcedAggregateRoot
{
    private int $balance = 0;

    public static function open(AccountId $id, int $initialDeposit): self
    {
        $account = new self($id);
        $account->recordThat(new AccountOpened($id->value(), $initialDeposit));

        return $account;
    }

    public function deposit(int $amount): void
    {
        $this->recordThat(new MoneyDeposited($this->identity()->value(), $amount));
    }

    public function withdraw(int $amount): void
    {
        ($this->balance >= $amount) or throw InsufficientFunds::forAccount($this->identity()->value());

        $this->recordThat(new MoneyWithdrawn($this->identity()->value(), $amount));
    }

    // Event apply methods — called automatically during reconstitution
    protected function applyAccountOpened(AccountOpened $event): void
    {
        $this->balance = $event->initialDeposit;
    }

    protected function applyMoneyDeposited(MoneyDeposited $event): void
    {
        $this->balance += $event->amount;
    }

    protected function applyMoneyWithdrawn(MoneyWithdrawn $event): void
    {
        $this->balance -= $event->amount;
    }
}
```

### Persist and Load

[](#persist-and-load)

```
use SolidFrame\EventSourcing\Repository\AggregateRootRepository;

$repository = new AggregateRootRepository(BankAccount::class, $eventStore);

// Save
$account = BankAccount::open(AccountId::generate(), 1000);
$account->deposit(500);
$repository->save($account);

// Load — replays all events to rebuild state
$account = $repository->load($accountId);
```

Event Store
-----------

[](#event-store)

The `EventStoreInterface` defines how events are stored and loaded.

```
use SolidFrame\EventSourcing\Store\EventStoreInterface;

// Persist events with optimistic concurrency control
$eventStore->persist($aggregateId, expectedVersion: 0, events: [
    new AccountOpened($aggregateId->value(), 1000),
]);

// Load all events
$events = $eventStore->load($aggregateId);

// Load from a specific version
$events = $eventStore->loadFromVersion($aggregateId, fromVersion: 5);
```

### Concurrency Control

[](#concurrency-control)

The event store uses optimistic locking. If another process has written events since you loaded, a `ConcurrencyException` is thrown:

```
use SolidFrame\EventSourcing\Exception\ConcurrencyException;

try {
    $eventStore->persist($aggregateId, expectedVersion: 3, events: $newEvents);
} catch (ConcurrencyException $e) {
    // Version conflict — reload and retry
}
```

### In-Memory Store

[](#in-memory-store)

For testing and prototyping:

```
use SolidFrame\EventSourcing\Store\InMemoryEventStore;

$eventStore = new InMemoryEventStore();
```

Snapshots
---------

[](#snapshots)

Snapshots optimize loading for aggregates with many events.

### Make an Aggregate Snapshotable

[](#make-an-aggregate-snapshotable)

```
use SolidFrame\EventSourcing\Snapshot\SnapshotableAggregateRootInterface;

final class BankAccount extends AbstractEventSourcedAggregateRoot
    implements SnapshotableAggregateRootInterface
{
    private int $balance = 0;

    public function createSnapshotState(): mixed
    {
        return ['balance' => $this->balance];
    }

    public static function reconstituteFromSnapshot(
        IdentityInterface $id,
        int $version,
        mixed $state,
        iterable $remainingEvents,
    ): static {
        $account = new self($id);
        $account->balance = $state['balance'];
        $account->aggregateRootVersion = $version;

        foreach ($remainingEvents as $event) {
            $account->applyEvent($event);
        }

        return $account;
    }

    // ... rest of the aggregate
}
```

### Snapshot Repository

[](#snapshot-repository)

```
use SolidFrame\EventSourcing\Snapshot\SnapshotAggregateRootRepository;
use SolidFrame\EventSourcing\Snapshot\Snapshot;

$repository = new SnapshotAggregateRootRepository(
    BankAccount::class,
    $eventStore,
    $snapshotStore,
);

// Load: uses snapshot + remaining events (faster than full replay)
$account = $repository->load($accountId);

// Save a snapshot manually
$snapshotStore->save(new Snapshot(
    aggregateId: $accountId->value(),
    aggregateType: BankAccount::class,
    version: $account->aggregateRootVersion(),
    state: $account->createSnapshotState(),
));
```

Event Apply Convention
----------------------

[](#event-apply-convention)

When replaying events, the aggregate calls `apply{EventShortName}()` automatically:

Event ClassApply Method`OrderPlaced``applyOrderPlaced()``MoneyDeposited``applyMoneyDeposited()``AccountOpened``applyAccountOpened()`These methods are `protected` and must not be called directly.

API Reference
-------------

[](#api-reference)

Class / InterfacePurpose`EventSourcedAggregateRootInterface`Contract for event-sourced aggregates`AbstractEventSourcedAggregateRoot`Base aggregate with `recordThat()` and replay`EventStoreInterface`Event persistence contract`InMemoryEventStore`In-memory event store`AggregateRootRepositoryInterface`Aggregate persistence contract`AggregateRootRepository`Standard repository (full replay)`SnapshotableAggregateRootInterface`Contract for snapshotable aggregates`Snapshot`Snapshot value object`SnapshotStoreInterface`Snapshot persistence contract`InMemorySnapshotStore`In-memory snapshot store`SnapshotAggregateRootRepository`Repository with snapshot optimization`AggregateNotFoundException`Aggregate not found in event store`ConcurrencyException`Version conflict during persistRelated Packages
----------------

[](#related-packages)

- [solidframe/core](../core) — DomainEventInterface, Identity
- [solidframe/ddd](../ddd) — Entity, AggregateRoot base classes
- [solidframe/cqrs](../cqrs) — Command/Query handlers that use event-sourced aggregates
- [solidframe/laravel](../laravel) — Database EventStore/SnapshotStore, migrations
- [solidframe/symfony](../symfony) — DBAL EventStore/SnapshotStore, schema SQL

Contributing
------------

[](#contributing)

This repository is a read-only split of the [solidframe/solidframe](https://github.com/solidframe/solidframe) monorepo, auto-synced on every push to `main`. Issues, pull requests, and discussions belong in the monorepo.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance89

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

59d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/97de2a466719393a21a8ce5caef247a1afb8aec5a8974248da0583f4197765b2?d=identicon)[abdulkadir-posul](/maintainers/abdulkadir-posul)

---

Top Contributors

[![abdulkadir-posul](https://avatars.githubusercontent.com/u/88670954?v=4)](https://github.com/abdulkadir-posul "abdulkadir-posul (1 commits)")

### Embed Badge

![Health badge](/badges/solidframe-event-sourcing/health.svg)

```
[![Health](https://phpackages.com/badges/solidframe-event-sourcing/health.svg)](https://phpackages.com/packages/solidframe-event-sourcing)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
