mad654/php-event-store
Composer 安装命令:
composer require mad654/php-event-store
包简介
Provides classes to express object state changes as events and store them in file based eventstreams
关键字:
README 文档
README
Plain php event store implementation for easy persistence by utilizing event sourcing.
It provides classes to express object state changes as events and store them in file based eventstreams.
Installation
You can install mad654/php-event-store via composer by adding "mad654/php-event-store": "dev-master" as requirement to your composer.json.
By Example
For a full working example take a look at src/example. In your vagrant box you can use it like this:
# create storage folder mkdir -p /tmp/var/eventstore-example/ # create a new instance of LightSwitch with id 'kitchen' src/example/bin/console.php init kitchen # switch on/off on 'kitchen' src/example/bin/console.php switch kitchen --on src/example/bin/console.php switch kitchen --off # render history of 'kitchen' src/example/bin/console.php history kitchen
Step by step
Let's take this little example to get in touch with all the new stuff. Lets asume you want to control the light in your Kittchen and for this you have build some switch. All you need is an object which can control the state and keeps its current state over mutlitple requests:
class LightSwitch { private $state = false; public function isOn(): bool { return $this->state; } public function switchOn() { if ($this->state === true) return; // do some stuff which does the hard work $this->state = true; } public function switchOff() { if ($this->state === false) return; // do some stuff which does the hard work $this->state = false; } }
This is a good beginning, but now you need a way to persist the state.
EventSourcedObject
Instead of creating a database you can extend your class to implement the EventSourcedObject interface. An EventSourcedObject is simply an object which should be available in his current state in the next request and for this it can publish its events as a stream and can be build from scratch based on the events:
<?php use mad654\eventstore\Event; use mad654\eventstore\Event\StateChanged; use mad654\eventstore\EventSourcedObject; use mad654\eventstore\EventStream\AutoTrackingEventSourcedObjectTrait; use mad654\eventstore\SubjectId; class LightSwitch implements EventSourcedObject { use AutoTrackingEventSourcedObjectTrait; /** * @var bool */ private $state; public function __construct(SubjectId $id) { $this->init($id, ['state' => false]); } public function isOn(): bool { return $this->state; } public function switchOn(): void { if ($this->state) return; $this->record(new StateChanged($this->subjectId(), ['state' => true])); } public function switchOff(): void { if (!$this->state) return; $this->record(new StateChanged($this->subjectId(), ['state' => false])); } public function on(Event $event): void { $this->state = $event->get('state', $this->state); } }
So instead of changing your member variables directly, you will use events for this, like shown in switchOn. So you will record an event and you will update your state in the on function, which is automatically be called by record function.
If you want to see more details, take a look at the AutoTrackingEventSourcedObjectTrait which should be a good starting point for all your event sourced objects and reduce boilerplate code.
EventSourcedObjectStore
use mad654\eventstore\FileEventStream\FileEventStreamFactory; use mad654\eventstore\EventObjectStore; $factory = new FileEventStreamFactory("/tmp/eventstore-example"); $store = new EventSourcedObjectStore($factory);
The EventSourcedObjectStore provides a simple API which allows you to save and load objects which implements the EventEmitter interface:
$store->attach($someEventSourcedObject); unset($someEventSourcedObject); $id = StringSubjectId::fromString('id-of-some-object'); $someEventEmitter = $store->get($id);
If '$someEventSourcedObject' was implemented correctly, it should have the equal state - before and after the unset() call.
By definition an EventSourcedObjectStore can only store and retrieve objects by id. Here you can find solutions for searching ...
Event
In this example we use the StateChanged event, in production you should create subclasses of this to express more precisely what happened.
In general events are immutable.
Putting all together
use mad654\eventstore\FileEventStream\FileEventStreamFactory; use mad654\eventstore\EventObjectStore; $factory = new FileEventStreamFactory("/tmp/eventstore-example"); $store = new EventSourcedObjectStore($factory); $switch = new LightSwitch('kitchen'); $store->attach($switch);
Some times later in an other request you want to switch on the light in the kitchen:
$store->get(StringSubjectId::fromString('kitchen'))->switchOn();
And again later you will switch it off again:
$store->get(StringSubjectId::fromString('kitchen'))->switchOff();
And again ...
And again ...
And now you are wondering why your power bill is so expensive - let's take a look at the history:
$renderer = new ArrayEventStreamRenderer(); $store->get(StringSubjectId::fromString('kitchen'))->history($renderer); $data = $renderer->toArray(); // use symonfy command style to render a nice table on command line $io = new SymfonyStyle($input, $output); $io->table( ['nr', 'timestamp', 'event_type', 'id', 'property', 'new_state'], $data ); # nr | timestamp | event_type | id | property | new_state # 1 | 2018-12-01 18:10:00 | StateChanged | kittchen | state | on # 2 | 2018-12-01 18:12:00 | StateChanged | kittchen | state | off # 3 | 2018-12-01 19:30:00 | StateChanged | kittchen | state | on # 4 | 2018-12-03 18:10:00 | StateChanged | kittchen | state | off
Development
cd local_develop
vagrant up
vagrant ssh
Inside of the vagrant box:
composer install
make test
mad654/php-event-store 适用场景与选型建议
mad654/php-event-store 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「events」 「event sourcing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mad654/php-event-store 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mad654/php-event-store 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mad654/php-event-store 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Библиотека для реализаций паттерна Pub/Sub
Wireless Cross-Component Communication
Event Sourcing implementation using prooph/event-store
A powerful event calendar Tool for Laravel's Nova 5.
Infrastructure and testing helpers for creating CQRS and event sourced applications.
Symfony bundle for broadway/broadway.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-09