jbzoo/event
Composer 安装命令:
composer require jbzoo/event
包简介
Library for event-based development
README 文档
README
A lightweight PHP event manager library that provides a simple yet powerful pattern for event-driven development. Create objects that emit events and register listeners to handle them with support for priorities, namespaces, and wildcard matching.
Features
- Priority-based event handling - Control execution order with built-in priority levels
- Namespace support with wildcards - Listen to
item.*,*.save, or*.*patterns - Multiple callback types - Closures, functions, static methods, and object methods
- Event propagation control - Stop event chains with
ExceptionStop - One-time listeners - Auto-removing listeners with
once() - Reference parameter passing - Communicate between listeners
- High performance - Optimized for speed with comprehensive benchmarks
- PHP 8.2+ with strict types - Modern PHP with full type safety
Installation
composer require jbzoo/event
Usage
Quick Start
use JBZoo\Event\EventManager; $eManager = new EventManager(); // Simple $eManager->on('create', function () { echo "Something action"; }); // Just do it! $eManager->trigger('create');
Priority-Based Execution
By supplying a priority, you are ensured that subscribers handle in a specific order. The default priority is EventManager::MID. Anything below that will be triggered earlier, anything higher later. If there are two subscribers with the same priority, they will execute in an undefined, but deterministic order.
// Run it first $eManager->on('create', function () { echo "Something high priority action"; }, EventManager::HIGH); // Run it latest $eManager->on('create', function () { echo "Something another action"; }, EventManager::LOW); // Custom index $eManager->on('create', function () { echo "Something action"; }, 42); // Don't care... $eManager->on('create', function () { echo "Something action"; });
Callback Types
All standard PHP callable types are supported:
$eManager->on('create', function(){ /* ... */ }); // Custom function $eManager->on('create', 'myFunction'); // Custom function name $eManager->on('create', ['myClass', 'myMethod']); // Static function $eManager->on('create', [$object, 'Method']); // Method of instance
Event Propagation Control
use JBZoo\Event\ExceptionStop; $eManager->on('create', function () { throw new ExceptionStop('Some reason'); // Special exception for JBZoo/Event }); $eManager->trigger('create'); // Returns count of executed listeners
Passing Arguments
Event data can be passed to listeners as function arguments:
$eManager->on('create', function ($entityId) { echo "An entity with id ", $entityId, " just got created.\n"; }); $entityId = 5; $eManager->trigger('create', [$entityId]);
Because you cannot really do anything with the return value of a listener, you can pass arguments by reference to communicate between listeners and back to the emitter.
$eManager->on('create', function ($entityId, &$warnings) { echo "An entity with id ", $entityId, " just got created.\n"; $warnings[] = "Something bad may or may not have happened.\n"; }); $warnings = []; $eManager->trigger('create', [$entityId, &$warnings]);
Namespace Wildcards
Use wildcards to listen to multiple related events:
$eManager->on('item.*', function () { // item.init // item.save echo "Any actions with item"; }); $eManager->on('*.init', function () { // tag.init // item.init echo "Init any entity"; }); $eManager->on('*.save', function () { // tag.save // item.save echo "Saving any entity in system"; }); $eManager->on('*.save.after', function () { // tag.save.after // item.save.after echo "Any entity on after save"; }); $eManager->trigger('tag.init'); $eManager->trigger('tag.save.before'); $eManager->trigger('tag.save'); $eManager->trigger('tag.save.after'); $eManager->trigger('item.init'); $eManager->trigger('item.save.before'); $eManager->trigger('item.save'); $eManager->trigger('item.save.after');
One-Time Listeners
For events that should only be handled once:
$eManager->once('app.init', function () { echo "This will only run once, then auto-remove itself"; }); $eManager->trigger('app.init'); // Executes $eManager->trigger('app.init'); // Does nothing - listener was removed
Advanced Usage
use JBZoo\Event\EventManager; // Create a global event manager EventManager::setDefault(new EventManager()); $globalManager = EventManager::getDefault(); // Get summary of registered events $summary = $eManager->getSummeryInfo(); // Returns: ['user.create' => 3, 'user.update' => 1, ...] // Remove specific listeners $callback = function() { echo "test"; }; $eManager->on('test', $callback); $eManager->removeListener('test', $callback); // Remove all listeners for an event $eManager->removeListeners('test'); // Remove ALL listeners $eManager->removeListeners();
Performance Benchmarks
Extensive performance testing with 100,000 iterations shows excellent performance characteristics.
Benchmark tests use phpbench/phpbench - see detailed results in tests/phpbench.
Note:
1μs = 1/1,000,000 of a second- these are microseconds!
benchmark: ManyCallbacks
| subject | groups | its | revs | mean | stdev | rstdev | mem_real | diff |
|---|---|---|---|---|---|---|---|---|
| benchOneUndefined | undefined | 10 | 100000 | 0.65μs | 0.01μs | 1.00% | 6,291,456b | 1.00x |
| benchOneWithStarBegin | *.bar | 10 | 100000 | 0.67μs | 0.01μs | 1.44% | 6,291,456b | 1.04x |
| benchOneWithAllStars | *.* | 10 | 100000 | 0.68μs | 0.03μs | 4.18% | 6,291,456b | 1.04x |
| benchOneWithStarEnd | foo.* | 10 | 100000 | 0.68μs | 0.01μs | 1.24% | 6,291,456b | 1.04x |
| benchOneNested | foo.bar | 10 | 100000 | 43.23μs | 0.46μs | 1.07% | 6,291,456b | 66.56x |
| benchOneSimple | foo | 10 | 100000 | 45.07μs | 2.63μs | 5.83% | 6,291,456b | 69.39x |
benchmark: ManyCallbacksWithPriority
| subject | groups | its | revs | mean | stdev | rstdev | mem_real | diff |
|---|---|---|---|---|---|---|---|---|
| benchOneUndefined | undefined | 10 | 100000 | 0.65μs | 0.01μs | 1.35% | 6,291,456b | 1.00x |
| benchOneNestedStarAll | *.* | 10 | 100000 | 0.67μs | 0.01μs | 1.34% | 6,291,456b | 1.03x |
| benchOneWithStarBegin | *.bar | 10 | 100000 | 0.67μs | 0.01μs | 1.10% | 6,291,456b | 1.04x |
| benchOneWithStarEnd | foo.* | 10 | 100000 | 0.68μs | 0.01μs | 1.13% | 6,291,456b | 1.05x |
| benchOneSimple | foo | 10 | 100000 | 4.54μs | 0.02μs | 0.35% | 6,291,456b | 7.03x |
| benchOneNested | foo.bar | 10 | 100000 | 4.58μs | 0.04μs | 0.81% | 6,291,456b | 7.10x |
benchmark: OneCallback
| subject | groups | its | revs | mean | stdev | rstdev | mem_real | diff |
|---|---|---|---|---|---|---|---|---|
| benchOneWithStarBegin | *.bar | 10 | 100000 | 0.69μs | 0.03μs | 4.00% | 6,291,456b | 1.00x |
| benchOneWithStarEnd | foo.* | 10 | 100000 | 0.70μs | 0.03μs | 4.22% | 6,291,456b | 1.00x |
| benchOneNestedStarAll | *.* | 10 | 100000 | 0.70μs | 0.04μs | 6.02% | 6,291,456b | 1.01x |
| benchOneUndefined | undefined | 10 | 100000 | 0.71μs | 0.05μs | 7.44% | 6,291,456b | 1.02x |
| benchOneSimple | foo | 10 | 100000 | 1.18μs | 0.03μs | 2.27% | 6,291,456b | 1.70x |
| benchOneNested | foo.bar | 10 | 100000 | 1.25μs | 0.03μs | 2.46% | 6,291,456b | 1.81x |
benchmark: Random
| subject | groups | its | revs | mean | stdev | rstdev | mem_real | diff |
|---|---|---|---|---|---|---|---|---|
| benchOneSimple | random.*.triggers | 10 | 100000 | 4.29μs | 0.33μs | 7.69% | 6,291,456b | 1.00x |
Development
Setup
make update # Install dependencies
Testing
make test # Run PHPUnit tests make test-all # Run tests + code style checks make codestyle # Run all linters
License
MIT
jbzoo/event 适用场景与选型建议
jbzoo/event 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 783.92k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2016 年 01 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「eventmanager」 「signal」 「events」 「listener」 「observer」 「HOOK」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jbzoo/event 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jbzoo/event 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jbzoo/event 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library to ease the use of signal handling.
Dotkernel event component extending and customizing laminas-eventmanager
Zend Framework 1 EventManager package
Unix signal handler that allow registering multiples callback on the same signal
Библиотека для реализаций паттерна Pub/Sub
Wireless Cross-Component Communication
统计信息
- 总下载量: 783.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 29
- 点击次数: 30
- 依赖项目数: 6
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-28