定制 solidframe/event-sourcing 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

solidframe/event-sourcing

Composer 安装命令:

composer require solidframe/event-sourcing

包简介

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

README 文档

README

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

composer require solidframe/event-sourcing

Quick Start

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

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

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

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

For testing and prototyping:

use SolidFrame\EventSourcing\Store\InMemoryEventStore;

$eventStore = new InMemoryEventStore();

Snapshots

Snapshots optimize loading for aggregates with many events.

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

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

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

Event Class Apply Method
OrderPlaced applyOrderPlaced()
MoneyDeposited applyMoneyDeposited()
AccountOpened applyAccountOpened()

These methods are protected and must not be called directly.

API Reference

Class / Interface Purpose
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 persist

Related Packages

Contributing

This repository is a read-only split of the solidframe/solidframe monorepo, auto-synced on every push to main. Issues, pull requests, and discussions belong in the monorepo.

solidframe/event-sourcing 适用场景与选型建议

solidframe/event-sourcing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 solidframe/event-sourcing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 solidframe/event-sourcing 我们能提供哪些服务?
定制开发 / 二次开发

基于 solidframe/event-sourcing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 38
  • 依赖项目数: 0
  • 推荐数: 2

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-18