phpgears/event
Composer 安装命令:
composer require phpgears/event
包简介
Event handling
关键字:
README 文档
README
Event
Event base classes and handling interfaces
This package only provides the building blocks to events
Installation
Composer
composer require phpgears/event
Usage
Require composer autoload file
require './vendor/autoload.php';
Events
Events are DTOs that carry all the information of an already happened situation
You can create your own by implementing Gears\Event\Event or extending from Gears\Event\AbstractEvent which ensures event immutability and payload and metadata is composed only of scalar values which is a very interesting capability. AbstractEvent has a private constructor forcing you to create events using occurred static method on your named constructors
use Gears\Event\AbstractEvent; class CreateUserEvent extends AbstractEvent { public static function fromPersonalData( string $name, string lastname, \DateTimeImmutable $birthDate ): self { return static::occurred([ 'name' => $name, 'lastname' => $lastname, 'birthDate' => $birthDate->format('U'), ]); } }
In case of a event without any payload you could extend Gears\Event\AbstractEmptyEvent
use Gears\Event\AbstractEvent; class CreateUserEvent extends AbstractEmptyEvent { public static function instance(): self { return self::occurred(); } }
Collection
Events can be grouped into iterables implementing Gears\Event\EventCollection objects, Gears\Event\EventArrayCollection and Gears\Event\EventIteratorCollection are provided accepting only instances of Gears\Event\Event
Async events
Having event assuring all of its payload is composed only of scalar values proves handy when you want to delegate event handling to a message queue system such as RabbitMQ, Gearman or Apache Kafka, serializing/deserializing scalar values is trivial in any format and language
Asynchronous behaviour must be implemented at EventBus level, event bus must be able to identify async events (a map of events, implementing an interface, by a payload parameter, ...) and enqueue them
If you want to have asynchronous behaviour on your EventBus have a look phpgears/event-async, there you'll find all the necessary pieces to start your async event bus
Handlers
Events are handed over to implementations of Gears\Event\EventHandler, available in this package is AbstractEventHandler which verifies the type of the event so you can focus only on implementing the handling logic
class CreateUserEventHandler extends AbstractEventHandler { protected function getSupportedEventType(): string { return CreateUserEvent::class; } protected function handleEvent(Event $event): void { /* @var CreateUserEvent $event */ $user = new User( $event->getName(), $event->getLastname(), $event->getBirthDate() ); [...] } }
Have a look at phpgears/dto fo a better understanding of how events are built out of DTOs and how they hold their payload
Event Bus
Only Gears\Event\EventBus interface is provided, you can easily use any of the good bus libraries available out there by simply adding an adapter layer
Implementations
Event bus implementations currently available
- phpgears/event-symfony-messenger uses Symfony's Messenger
- phpgears/event-symfony-event-dispatcher uses Symfony's Event Dispatcher
Contributing
Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.
See file CONTRIBUTING.md
License
See file LICENSE included with the source code for a copy of the license terms.
phpgears/event 适用场景与选型建议
phpgears/event 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 665 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 11 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「event」 「immutable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phpgears/event 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phpgears/event 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phpgears/event 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple, immutable data structures
Stk Immutable Data objects
MongoDB database access using immutables
Symfony bundle for broadway/broadway.
This is an Event Emitter equivalent to Node.js' Event Emitter.
Publish / Subscribe / Event Manager
统计信息
- 总下载量: 665
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-21