acdphp / laravel-queued-events
将事件分发到队列中。这对于使用扇出队列的分布式系统事件特别有用。
v2.1.0
2024-01-03 11:01 UTC
Requires
- php: ^8.0
Requires (Dev)
- ekino/phpstan-banned-code: ^1.0
- larastan/larastan: ^2.7
- laravel/pint: ^1.5
- orchestra/testbench: ^7.38
- pestphp/pest: ^1.23
- pestphp/pest-plugin-laravel: ^1.4
- phpstan/phpstan: ^1.10
README
将事件分发到队列中。这对于使用扇出队列的分布式系统事件特别有用。
安装
- 安装包
composer require acdphp/laravel-queued-events
用法
-
将
QueuedEvent
扩展到您的事件中。use Acdphp\QueuedEvents\Events\QueuedEvent; class UserCreatedEvent extends QueuedEvent { // Remove the Dispatchable trait public function __construct(public $object) { } }
-
调用
dispatch()
UserCreatedEvent::dispatch(['foo' => 'bar']);
- 您可以指定队列连接和队列
UserCreatedEvent::dispatch(['foo' => 'bar']) ->onConnection('your-fanout-queue-connection') ->onQueue('your-custom-queue');
- 还提供了实用工具:
dispatchIf()
,dispatchUnless()
// Dispatches if $condition is true UserCreatedEvent::dispatchIf($condition, ['foo' => 'bar']); // Dispatches if $condition is false UserCreatedEvent::dispatchUnless($condition, ['foo' => 'bar']);
-
默认分发方法以
internal
前缀UserCreatedEvent::internalDispatch(['foo' => 'bar']); UserCreatedEvent::internalDispatchIf(['foo' => 'bar']); UserCreatedEvent::internalDispatchUnless(['foo' => 'bar']);
配置
php artisan vendor:publish --tag=queued-events-config
QUEUED_EVENTS_QUEUE_CONNECTION
默认队列连接将设置为您的 QUEUE_CONNECTION
所设置。您可以通过设置 QUEUED_EVENTS_QUEUE_CONNECTION
来覆盖此设置。
QUEUED_EVENTS_QUEUE
默认队列将是 default
。您可以通过设置 QUEUED_EVENTS_QUEUE
来覆盖此设置。
注意
- 使用 Laravel 辅助函数,如
event(...)
或app('events')->dispatch(...)
,将内部分发作业。仅使用::dispatch
,::dispatchIf
和::dispatchUnless
来在队列中分发。 - 如果您只在一个单体应用程序中使用此功能,请使用 Laravel 的队列监听器。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅许可证文件。