tbht/php-events 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tbht/php-events

Composer 安装命令:

composer require tbht/php-events

包简介

a event emitter for php

关键字:

README 文档

README

PHP Composer codecov

Install

composer require tbht/php-events

Usage

add_listener

use Press\Utils\Events;

$ee = new Events();

$ee->add_listener('foo', function () {
    echo "this is a listener for php \n";
});

// the same as $ee->on()
$ee->on('foo', function () {
    echo "this is a listener for php \n";
});

$ee->emit('foo');

// the same as
$ee->emit_event('foo');

remove_listener

string

use Press\Utils\Events;

$ee = new Events();

$bar_listener = function ($arg) {
  var_dump($arg);
};

$ee->add_listener('bar', $bar_listener);

$ee->remove_listener('bar', $bar_listener);

regular expression

use Press\Utils\Events;

$ee = new Events();
$fn1 = function () {};
$fn2 = function () {};
$fn3 = function () {};
$fn4 = function () {};
$fn5 = function () {};

$ee->add_listeners([
  'foo' => [$fn1, $fn2, $fn3, $fn4, $fn5],
  'bar' => [$fn1, $fn2, $fn3, $fn4, $fn5],
  'baz' => [$fn1, $fn2, $fn3, $fn4, $fn5]
]);

$ee->remove_listener('/ba[rz]/', $fn3);

assert(
  [$fn5, $fn4, $fn2, $fn1] === $ee->flatten_listeners($ee->get_listeners('bar'))
);

add_once_listener

use Press\Utils\Events;

$ee = new Events();
$counter = 0;

$bar_listener_once = function ($counter) use (&$counter) {
  $counter++;
};

$ee->add_once_listener('foo', $bar_listener_once);

$ee->emit('foo');
$ee->emit_event('foo');

assert($counter === 1, 'the counter should be 1');

get_listeners

use Press\Utils\Events;

$fn1 = function () {
    var_dump('this is first fn');
};

$fn2 = function () {
    var_dump('this is second fn');
};

$ee = new Events();
$ee->add_listeners('foo', $fn1)
   ->add_listeners('foo', $fn2);

// listeners is an array
$listeners = $ee->get_listeners('foo');
$listeners = $ee->flatten_listeners($listeners);
var_dump($listeners);

Advance Usage

remove listeners with regex

use Press\Utils\Events;

$ee = new Events();

$fn1 = function () {};
$fn2 = function () {};
$fn3 = function () {};
$fn4 = function () {};
$fn5 = function () {};

$ee->add_listeners([
  'foo' => [$fn1, $fn2, $fn3, $fn4, $fn5],
  'bar' => [$fn1, $fn2, $fn3, $fn4, $fn5],
  'baz' => [$fn1, $fn2, $fn3, $fn4, $fn5]
]);

$ee->remove_listener('/ba[rz]/', $fn3);

$foo_list = $ee->get_listeners('foo');
$bar_list = $ee->get_listeners('bar');
$baz_list = $ee->get_listeners('baz');

count($foo_list) === 0;
count($bar_list) === 0;
count($baz_list) === 0;

get listeners as key-value

$ee = new Events();
$ee->add_listener('bar', function () {});
$ee->add_listener('baz', function () {});

$listeners = $ee->get_listeners_wrapper('/ba[rz]/');

count($listeners['bar']) === 1;
count($listeners['baz']) === 1;

tbht/php-events 适用场景与选型建议

tbht/php-events 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 925 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 06 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「events」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tbht/php-events 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tbht/php-events 我们能提供哪些服务?
定制开发 / 二次开发

基于 tbht/php-events 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 925
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 12
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-21