gdecris / event-ivey
具有通过适配器扩展性的简单事件和队列系统
v1.0.2
2017-10-15 12:58 UTC
Requires
- php: ^5.5.9 || ^7.0
- aws/aws-sdk-php: ^3.19
- illuminate/container: ^5.1
- predis/predis: ^1.1
Requires (Dev)
- phpunit/phpunit: ^5.5
- vlucas/phpdotenv: ^2.4
README
支持队列驱动器的PHP事件系统
使用 illuminate/container 设置
// Register the appropriate queue
$container->singleton(QueueContract::class, MemoryQueue::class);
// Bind the dispatcher as a singleton
$container->singleton(EventDispatcher::class);
基本用法
监听事件
$container->make(EventDispatcher::class)->listen('event.name', function($payload) {
// Listener receives the payload
});
触发事件
$container->make(EventDispatcher::class)->fire('event.name', ['some' => 'payload data']);
队列监听器
注册监听器
$container->make(EventDispatcher::class)->listen('event.name', MyListener::class);
监听器类
use Ivey\Events\EventListener;
class MyListener extends EventListener
{
proteceted static $should_queue = true;
public function fire($payload)
{
// TODO: Implement fire() method.
}
}
工作进程
运行工作进程
$worker = $container->make(Worker::class);
// Sleep for 5 seconds retry a max of 3 times before failing
$worker->setSleep(5)
->setTries(3)
->runDaemon();
失败的任务
// Add a listener for catching the failed jobs so you can handle them accordingly
$container->make(EventDispatcher::class)->listen('failed.job', function ($payload) {
});