nimbly/announce
Composer 安装命令:
composer require nimbly/announce
包简介
PSR-14 event dispatcher with subscribers and full dependency injection.
README 文档
README
A simple framework agnostic PSR-14 event dispatcher for your event-driven application.
Features
- Uses PHP's
#Attributefeature to register class methods as event handlers - Optional PSR-11 Container support
- Full autowiring support for your subscribers and listeners - Announce will inject not just the event but any needed services or other dependencies from your container!
Installation
composer require nimbly/announce
Quick start
Create an event class
Your events can be standalone classes or they can extend the StoppableEvent abstract class. By extending the StoppableEvent abstract you gain the ability to stop event propogation if needed.
namespace App\Events; use App\Models\User; use Nimbly\Announce\Event; class UserRegisteredEvent extends StoppableEvent { public function __construct(public User $user) { } }
Create a subscriber
Subscribers are classes that will handle your events. You can have as many subscribers as you would like.
To register a subscriber's method to handle a particular event or set of events, use the Nimbly\Announce\Subscribe attribute and pass in a comma separated list of event names to listen for.
namespace App\Subscribers; use App\Events\UserRegisteredEvent; use App\Services\EmailService; use Nimbly\Announce\Subscribe; class EmailSubscriber { #[Subscribe(UserRegisteredEvent::class)] public function onUserRegistered( UserRegisteredEvent $event, EmailService $emailService): void { $emailService->send("registration_email", $event->user->email); } }
Initiate Dispatcher
To register your subscribers with the event dispatcher, pass in an array of class names or instances into the Dispatcher constructor.
You can also pass in a PSR-11 compliant container instance to be used in autowiring your subscribers as well as for event handlers on your subscribers.
$dispatcher = new Dispatcher( subscribers: [ EmailSubscriber::class, new FooSubscriber, ], container: $container );
Dispatch event
To trigger an event, just call the dispatch method with the event instance.
$event = new UserRegisteredEvent($user); $dispatcher->dispatch($event);
Stopping event propagation
If you need to stop event propagation during its lifetime, just call the stop() method on the event instance. The event will no longer be propagated to any further subscribed listeners. This requires the event to extend from the StoppableEvent abstract.
class UserRegisteredEvent extends StoppableEvent { public function __construct(public User $user) {} }
class EmailSubscriber { #[Subscribe(UserRegisteredEvent::class)] public function onUserRegistered( UserRegisteredEvent $event, EmailService $emailService): void { $emailService->send("registration_email", $event->user->email); // Prevent any further handlers from processing this event $event->stop(); } }
Registering individual listeners
Alternatively, you can register individual events using the listen method.
$dispatcher = new Dispatcher; $dispatcher->listen( UserRegisteredEvent::class, function(UserRegisteredEvent $event): void { // do some event stuff } );
Wildcard listeners
You can register a "wild card" listener by using the * event name. This will subscribe to all events fired.
$dispatcher = new Dispatcher; $dispatcher->listen( "*", function($event): void { // Respond to all events... } );
nimbly/announce 适用场景与选型建议
nimbly/announce 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.03k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nimbly/announce 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nimbly/announce 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7.03k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-23