myth/events
Composer 安装命令:
composer require myth/events
包简介
A simple, fast, and flexible pub/sub events system for PHP with event prioritization.
README 文档
README
This library provides a very simple, though quite powerful events system in PHP. It follows a publish/subscribe type of pattern, and uses a configuration file to store all of the event listeners in to reduce any potential coupling.
Setup
This library requires that a file exist someplace on the server that holds the configuration of the event listeners. The path to this file must be passed in as the only argument when instantiating the class. The definition of the event listeners is described below.
$events = new \Myth\Events\Events( 'path/to/config.php' );
This file will only be read in once during the lifetime of that instantiated class, and is not loaded until one of the classes methods are called.
Triggering Events
You can trigger an event to happen by simply calling the trigger() method on the class. The first parameter is the name of the event to trigger. This name can be pretty much anything you desire. The only requirement is that the listeners listen for the same name.
$events->trigger('new_user');
If you want to pass data along to the listeners so they have something to work with, you can pass an array of data items as the second parameter.
$events->trigger('new_user', [$user, $role] );
Canceling Listener Execution
Once the event is triggered, the listeners will be sorted by priority and ran one after another, the higher priority listener going first, naturally.
You can force the execution of the remaining listeners to stop by returning false from any listener.
Listening to Events
Listening to events simply means that you're telling the Events class that you want to be notified when that event is triggered, and that you want a chance to do something at that time. This is great for sending emails, checking authorization of the user, etc.
You define the listener inside of the config file using the on() method. The first parameter is the name of the event you want to run on. The second parameter is any callable function, but will often be closures.
$events->on('new_user', function ($user, $role) {
Mail::queue('new_user_email', $user);
});
You can define the order the events are executed by specifying the listener's priority in the third parameter. The priority is any whole number. The lower the number, the higher the priority.
$events->on('new_user', function ($user, $role) {
Mail::queue('new_user_email', $user);
}, 10);
There are three pre-defined values you can use if you don't need to specify any fine-grained orders:
EVENTS_PRIORITY_LOW = 200
EVENTS_PRIORITY_NORMAL = 100
EVENTS_PRIORITY_HIGH = 10
myth/events 适用场景与选型建议
myth/events 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「events」 「hooks」 「pubsub」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 myth/events 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 myth/events 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 myth/events 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Библиотека для реализаций паттерна Pub/Sub
Wireless Cross-Component Communication
A powerful event calendar Tool for Laravel's Nova 5.
A lightweight WordPress hook helper library. Register hooks before WordPress loads, run callbacks only once, and more.
Command bus implementation: Commands and domain events
Hooks integrated in Voyager
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-25