nekoo/eventemitter
Composer 安装命令:
composer require nekoo/eventemitter
包简介
Port of NodeJSs EventEmitter to PHP 5.4 using traits
README 文档
README
This is a direct port of the EventEmitter class from node.js using PHP 5.4 traits.
Why PHP 5.4?
Due to the nature of the EventEmitter functionnality, using simple extends sounds like the wrong way to deal with it as it is a set of functionnality to add to an existing class instead of your class extending the functionnalities of EventEmitter. As such traits are the best way to implement this kind of functionnality and those are only available to PHP 5.4+.
How to Use
<?php // if using directly require_once('EventEmitter.php'); // if using through composer just use the autoload require_once('vendor\.composer\autoload.php'); // test class class Item { use \Nekoo\EventEmitter; public function register($infos) { // do something // fire the event $this->emit('register', $infos); } } // allows you to check if a class uses the event emitter through // $class instanceof \Nekoo\EventEmitterInterface class AnotherItem implements \Nekoo\EventEmitterInterface { use \Nekoo\EventEmitter; } // create an instance of our object $i = new Item(); // register an observer for the register event $i->on('register', function($infos) { echo "Item registered!\n"; var_dump($infos); }); // call the method $i->register(array('key' => 'value'));
API
- setMaxListeners(int)
Set the maximum listeners allowed by events, default is 10. Adding too many listeners to a same event on the same object will make fireing events slower and slower, just up this limit if needed.
<?php $object->setMaxListeners(20);
- emit(string[, mixed arg1, ...])
Emit an event, all arguments provided after the event name are sent to the callback as is.
<?php $object->emit('event', $foo, $bar);
- on(string, callable)
Register a callback for en event, every forms of callbacks are accepted (strings, arrays or closures).
<?php $object->on('event', function() { var_dump(func_get_args()); });
- addListener(string, callable)
Alias for on() - all(callable)
Register a callback for en event, every forms of callbacks are accepted (strings, arrays or closures).
<?php $object->all(function($event, $arg1) { var_dump($event, $arg1); });
- once(string, callable)
Same thing as on() but the listener will only be called once. - off(string, callable)
Removes a listener for this event. You need to provide the very same array or string, or if using a closure the same instance.
<?php $fun = function($arg1) { echo "event: $arg1\n"; }; $object->on('event', $fun); // adds the event $object->off('event', $fun); // remove the event
- removeListener(string, callable)
Alias for off() - removeAllListeners([string])
Removes all listeners from a given event, or all listeners from all events if no event provided.
<?php $object->removeAllListeners('event'); // only for the 'event' event $object->removeAllListeners(); // for every events on the object
- getListeners(string)
Returns an array with the listeners for this event
<?php foreach ($object->getListeners('event') as $listener) { var_dump($listener); }
nekoo/eventemitter 适用场景与选型建议
nekoo/eventemitter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 103 次下载、GitHub Stars 达 15, 最近一次更新时间为 2012 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「event」 「listener」 「observer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nekoo/eventemitter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nekoo/eventemitter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nekoo/eventemitter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Timestamp parameter in public file uri
The Listener component of the RabbitEvents library.
A collection of observer classes that can be use in your Laravel application.
Event dispatcher package
Symfony bundle for broadway/broadway.
Listener to change layout for different module
统计信息
- 总下载量: 103
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-04-05