webino/event-emitter
Composer 安装命令:
composer require webino/event-emitter
包简介
Event Emitter implementation.
README 文档
README
Event Emitter implementation.
Recommended Usage
Use event emitter to decouple routine algorithm from an extended logic.
Setup
composer require webino\event-emitter
Quick Use
Emitting an event:
use Webino\EventEmitter; $emitter = new EventEmitter; // registering closure event handler $emitter->on('example', function () { return 'Hello'; }); // emitting custom event $event = $emitter->emit('example'); /** @var \Webino\EventResults $results */ $results = $event->getResults(); echo $results; // => Hello
Removing an event handler:
use Webino\EventEmitter; $emitter = new EventEmitter; $handler = function () { return 'Hello'; }; $emitter->on('example', $handler); // remove handler for all events $emitter->off($handler); // remove all handlers for an event $emitter->off(null, 'example'); // remove all handlers for all events $emitter->off();
Emitting an event until:
use Webino\EventEmitter; $emitter = new EventEmitter; $emitter->on('example', function () { return 'Special'; }); $event = $emitter->emit('example', function ($result) { // when result meets required condition if ('Special' === $result) { // stop propagation return false; } // or continue return true; });
Event handling priority:
use Webino\EventEmitter; $emitter = new EventEmitter; $emitter->on('example', function () { return 'Begin'; }, $event::BEGIN); $emitter->on('example', function () { return 'Before'; }, $event::BEFORE); $emitter->on('example', function (Event $event) { return 'Main'; }, $event::MAIN); $emitter->on('example', function () { return 'After'; }, $event::AFTER); $emitter->on('example', function () { return 'Finish'; }, $event::FINISH); // emitting custom event $event = $emitter->emit('example'); /** @var \Webino\EventResults $results */ $results = $event->getResults(); echo $results; // => BeginBeforeMainAfterFinish
Event handler:
use Webino\EventEmitter; use Webino\EventHandlerInterface; use Webino\EventHandlerTrait; class ExampleEventHandler implements EventHandlerInterface { use EventHandlerTrait; protected function initEvents(): void { $this->on('example', function () { return 'Foo'; }); $this->on('example', function () { return 'Bar'; }); } } // emitting custom event $event = $emitter->emit('example'); /** @var \Webino\EventResults $results */ $results = $event->getResults(); echo $results; // => FooBar
API
Event
-
const BEGIN
The beginning priority of the event. -
const BEFORE
Priority before main event. -
const MAIN
Main event priority. -
const AFTER
Priority after main event. -
const FINISH
Priority at the end of the event. -
const OFFSET
Event priority offset. -
string getName()
Get event name. -
EventEmitterInterface getTarget()
Get target object from which event was emitted. -
mixed getValue(string $name, mixed $default = null)
Get event value by name. -
void setValues(iterable $values)
Set event values. -
EventResults getResults()
Returns event results. -
void stop(bool $stop = true)
Indicate whether or not to stop this event. -
bool isStopped()
Indicates should stop.
EventEmitter
-
void setEventDispatcher(EventDispatcherInterface $dispatcher)
Inject event dispatcher. -
void on(
string|EventInterface|EventHandlerInterface $event,
string|array<int, string>|callable $callback = null,
int $priority = 1)
Set event handler. -
void off(callable|EventHandlerInterface $callback = null, string|EventInterface $event = null)
Remove event handler. -
EventInterface emit(string|EventInterface $event, callable $until = null)
Invoke handlers.
EventResults
-
mixed|null first()
Returns first response. -
mixed|null last()
Returns last response.
EventHandler
-
void attachEventEmitter(EventDispatcherInterface $emitter)
Attach event emitter to handler. -
void detachEventEmitter(EventDispatcherInterface $emitter)
Detach event emitter from handler.
Architecture
It is possible to have a global dispatcher to attach event handlers to.
Event Lifecycle
The basic idea around events is that we just trigger an event and every action happens in handlers, even the main action. Then we can listen to that event using priorities, if we want to act like a middleware. The event propagation could be stopped at any time.
Using events like someEvent.pre and someEvent.post or someEvent.before, someEvent.after, it doesn't matter, is messy and not recommended, don't do that. Give an event a unique name then attach handlers, main action including, using priorities. Convenient way to do that is to use an event priority constants.
Development
Static analysis:
composer analyse
Coding style check:
composer check
Coding style fix:
composer fix
Testing:
composer test
Git pre-commit setup:
ln -s ../../pre-commit .git/hooks/pre-commit
Addendum
Please, if you are interested in this library report any issues and don't hesitate to contribute. We will appreciate any contributions on development of this library.
webino/event-emitter 适用场景与选型建议
webino/event-emitter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 138 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 webino/event-emitter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webino/event-emitter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 138
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2019-03-20

