shieldon/event-dispatcher
最新稳定版本:1.0.0
Composer 安装命令:
composer require shieldon/event-dispatcher
包简介
Event dispatcher for PHP.
README 文档
README
This package is designed as a part of Shieldon Firewall 2. You can also use it on your projects as well.
Installation
Use PHP Composer:
composer require shieldon/event-dispatcher
Or, download it and include the Shieldon autoloader.
require 'autoload.php';
Usage
Add a Listener
/** * @param string $name The name of an event. * @param string|array $func Callable function or class. * @param int $priority The execution priority. * * @return bool */ \Shieldon\Event\Event::addLister(string $name, $func, int $priority = 10): bool
Please note, the priority must be unique. This method returns true when add a listener, false when the prirotiy has been taken by another listener.
Dispatch
/** * @param string $name The name of an event. * @param array $args The arguments. * * @return mixed */ \Shieldon\Event\Event::doDispatch(string $name, array $args = []): mixed
Return the filtered result, it's similar to WordPress' filter. You can ignore the return if you don't need that.
Example
Closure
Add a listener.
\Shieldon\Event\Event::addListener('test_1', function() { echo 'This is a closure function call.'; });
Dispatch.
$result = \Shieldon\Event\Event::doDispatch('test_1');
Function
Function for listener.
function test_event_disptcher() { echo 'This is a function call.'; }
Add a listener.
\Shieldon\Event\Event::addListener('test_2', 'test_event_disptcher');
Dispatch.
$result = \Shieldon\Event\Event::doDispatch('test_2');
Class
Add a listener.
$example = new Example(); \Shieldon\Event\Event::addListener('test_3', [$example, 'example1']);
Dispatch.
$result = \Shieldon\Event\Event::doDispatch('test_3');
Hope this helps.
Author
License
MIT
统计信息
- 总下载量: 22.82k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-14