共生体/调度器

事件调度器库

v1.0.2 2024-06-28 13:48 UTC

This package is auto-updated.

Last update: 2024-09-28 12:33:08 UTC


README

简单事件调度器

此包正在开发中!

要求

  • php 8.2

安装

composer require symbiont/dispatcher

使用

使用 EventDispatcherDispatcher

use Symbiont\Dispatcher\{Dispatcher, EventDispatcher};

// global event dispatcher
EventDispatcher::register('test', function() {
    return 'test';
});
EventDispatcher::once('test', function() {
    return 'once';
})
EventDispatcher::dispatch('test');

// local event dispatcher
$dispatcher = new Dispatcher;
$dispatcher->register('test', function() {
    return 'test';
});
$dispatcher->once('test', function() {
    return 'once';
});
$dispatcher->dispatch('test');

使用 CallbackableDispatchesEvent 特性实现回调事件调度器。

use Symbiont\Dispatcher\Contracts\Callbackable;
use Symbiont\Dispatcher\Concerns\DispatchesEvent;

class Awesome implements Callbackable {
    use DispatchesEvent;
    
    public function load() {
        $this->trigger('beforeLoad');
        // something to load ..
        $this->trigger('afterLoad');
    }
}

$awesome = new Awesome;
$awesome->on('afterLoad', function() {
    // something to do after load ..
});
$awesome->load();

文档

此文档仅记录此包的技术使用,几乎没有文本。

测试

composer test

许可

MIT 许可证