定制 stk2k/eventstream 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

stk2k/eventstream

Composer 安装命令:

composer require stk2k/eventstream

包简介

A simple PHP pub-sub library

README 文档

README

Latest Version on Packagist Software License Build Status Coverage Status Code Climate Total Downloads

Description

You can use this library to build an application using simple pub-sub pattern. This library consists of three main objects below:

  • EventStream

event stream is frontend facade of this system. you can push events to event source, register event listeners to event emitter, and flush events.

  • EventSource

event source is an event provider. it has a role to provide events when event stream request. you have to declare event source, and call EventStream#source() method to attach source.

  • EventEmitter

event emitter is an event dispatcher. it has a role to manage user callback functions. use Emitter/SimpleEventEmitter class for normal use, and pass it to EventStream#emitter() method.

Demo

01. event source

use stk2k\eventstream\EventStream;
use stk2k\eventstream\EventSourceInterface;
use stk2k\eventstream\emitter\SimpleEventEmitter;
use stk2k\eventstream\exception\EventSourceIsNotPushableException;
use Stk2k\EventStream\Event;

class NumberEventSource implements EventSourceInterface
{
    protected $numbers;
    
    public function __construct() {
        $this->numbers = ['one', 'two', 'three'];
    }
    public function canPush() : bool {
        return false;
    }
    public function push(Event $event) : EventSourceInterface {
        return $this;
    }
    public function next() {
        $number = array_shift($this->numbers);
        return $number ? new Event('number',$number) : null;
    }
}
  
// create event stream and setup callback, then flush all events
(new EventStream())
    ->channel('my channel', new NumberEventSource(), new SimpleEventEmitter())
    ->listen('number', function(Event $e){
        echo 'received number='.$e->getPayload(), PHP_EOL;
    })
    ->flush();

      
// received number=one
// received number=two
// received number=three
  
// you can not push event to unpushable event source
try{
    (new NumberEventSource())->push(new Even('number','four'));   // throws EventSourceIsNotPushableException
}
catch(EventSourceIsNotPushableException $e){
    echo 'Event not publishable.';
}
  
class PushableNumberEventSource extends NumberEventSource
{
    public function canPush() : bool {
        return true;
    }
    public function push(Event $event) : EventSourceInterface {
        if ($event->getName() === 'number'){
            $this->numbers[] = $event->getPayload();
        }
        return $this;
    }
}
  
// you acn push event to pushable event source
try{
    (new EventStream())
        ->channel('my channel')
        ->source((new PushableNumberEventSource())->push('number','four'))
        ->emitter(new SimpleEventEmitter())
        ->listen('number', function(Event $e){
                echo 'received number='.$e->getPayload(), PHP_EOL;
            })
        ->flush()
        ->push('number', 'five')
        ->flush();
}
catch(EventSourceIsNotPushableException $e){
    echo 'Event not publishable.';
}
  
// received number=one
// received number=two
// received number=three
// received number=four
// received number=five

Usage

  1. create event stream object
  2. define your own event source class and attach new instance to stream.
  3. define your own event emitter or use bundled emitter classes and attach new instance to stream.
  4. define your own callback(s) and attach it(them) to stream or emitter.
  5. flush events in event source via EventStream#flush() method.

More Examples

  • numbers.php: simple emitter and source sample
  • multi_channel.php: listen different channel(event)s.
  • regular_expression.php: single bind but listen multi channels by regular expression.
  • wild_card.php: single bind but listen multi channels by wild card.

Requirement

PHP 7.2 or later

Installing stk2k/eventstream

The recommended way to install Calgamo/Bench is through Composer.

composer require stk2k/eventstream

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

License

MIT

Author

stk2k

stk2k/eventstream 适用场景与选型建议

stk2k/eventstream 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.49k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 06 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 stk2k/eventstream 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4.49k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 9
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-27