proteins/event
Composer 安装命令:
composer require proteins/event
包简介
Event emitter-listener global class and behaviour trait
README 文档
README
Protein | Events
Event emitter-listener global class and behaviour trait
Install
composer require proteins/event
Require the global class via :
use Proteins\Event;
or the include the trait in your classes via :
use Proteins\Events; class MyClass { use Events; }
Attach an handler
You can attach an handler to a named event via the on method.
Event::on('my.event',function(){ echo 'Hello, Friend!'; });
Multiple handlers can be attached to the event, they will be sequentially executed when the event will be triggered.
Event::on('my.event',function(){ echo 'First!'; }); Event::on('my.event',function(){ echo 'Second!'; });
You can attach handlers to any event name.
Trigger an event
You can trigger an event via the trigger method.
Event::trigger('my.event');
The trigger method will return an array containing the return values of all the handler attached to the event.
Example
Event::on('my.event',function(){ return 'Hello!'; }); Event::on('my.event',function(){ return time(); }); $results = Event::trigger('my.event');
The $results variable contains :
array(2) { [0] => string(6) "Hello!" [1] => int(1389115191) }
NULL will be returned if no handlers are attached to the event.
You can run a trigger only one time with the triggerOnce method.
Passing parameters to event handlers
You can pass a variable number of parameter to event handlers appending them after the event name in the trigger method.
Event::on('eat',function($who,$what,$where){ echo "$who ate a $what, in the $where."; }); Event::trigger('eat','Simon','Burrito','Kitchen'); // Result : Simon ate a Burrito, in the Kitchen
Using the Events trait
You can augment any existing class adding the Events trait to them. The Events trait has the same identically signature of the standard Event module (in fact the Event Module is a simple wrapper of the Events trait).
class Game { use Events; public static function loadLevel($name){ ... self::trigger("level.loaded", $name); } } Game::on("level.start",function($level_name){ echo "Starting {$level_name}, BRING'EM'ON!!!\n"; }); Game::on("level.loaded",function($level_name){ echo "Reticulating splines for {$level_name}...\n"; Game::trigger("level.start", $level_name); }); Game::on("level.start",function($level_name){ echo "Replenish ammonitions...\n"; }); Game::loadLevel("E1M1");
Reticulating splines for E1M1...
Starting E1M1, BRING'EM'ON!!!
Replenish ammonitions...
proteins/event 适用场景与选型建议
proteins/event 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 proteins/event 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 proteins/event 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 50
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-28
