s1mpletru1h/events

事件 - 万事之基。

0.0.2 2019-08-27 22:27 UTC

This package is auto-updated.

Last update: 2024-09-28 10:19:00 UTC


README

事件 | 万事之基。

Build Status Total Downloads Latest Stable Version License

关于事件

事件让您能够看到应用程序中发生的一切。

事件通过允许您在任何应用程序中创建同步的、事件驱动的功能回调响应图来实现,只需调用两个简单的方法

$events->pub($event_name)$events->sub($event_name, $callback)

彩色日志允许您通过标准输出、文本文件日志以及不久的将来通过内存和sql/no-sql数据存储来查看应用程序中的情况。

简单

一个 EVENT 只是一个全大写的文本字符串,可选地由冒号分隔的 :LEVEL,可以是以下字符串之一,['VERBOSE', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],以及一个任意的数组 ['of' => $data]

事件通过 $events->pub('EVENT:LEVEL', ['arbitrary' => $data]); 函数发布,并通过 $events->sub('EVENT', function($event, $data) {...}); 订阅。

可测试性

您可以通过调用 $events->didOccur(['SOME', 'EVENTS'], $within_the_last_n_events); 来查看事件是否发生。此方法将返回 true 如果所有事件确实在 $within_the_last_n_events 内发生,否则返回 false

易用性

每个事件都可以有无限个回调绑定在每次触发时执行。唯一的要求是回调必须接受两个参数:事件名称(一个必需的字符串)和事件数据(一个可选的数组)。事件订阅者回调返回值被捕获并推送到一个数组中,并通过一个数组从 pub 函数返回,以保持返回顺序。

有趣

彩色日志使您的应用程序运行起来在标准输出、每个实例的日志文件、合并到单个主日志文件或者两者(即将推出的数据库事件日志!)中变得有趣。

用法

// create an instance of events
$this->events = new Events('main', Events::DEBUG);

// subscribe to an event and trigger a callback that takes the $event and an array of optional $data
$this->events->sub('EVERYTHING_BEGINS', function(string $event, array $data = []) {
   return "$event really did happen";
});

// trigger the event, and all of it's callbacks fired in their subscribe order by publishing the event
$now = $this->events->pub('EVERYTHING_BEGINS', ['with' => 'a bang']);

// see the value of everything by publishing an event to a log level of your choice with 'EVENT:LEVEL'
$this->events->pub('RETURNED_VALUES:DEBUG', $now);

// ensure that an event really did happen within the last $n events
$did_occur = $this->events->didOccur(['RETURNED_VALUES'], 2);

测试

composer install
phpunit