承接 mario-legenda/php-event-emitter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

mario-legenda/php-event-emitter

Composer 安装命令:

composer require mario-legenda/php-event-emitter

包简介

EventEmitter port from node to PHP

README 文档

README

This project is a light port of Nodes EventEmitter class. Although, it does not have all the features that Node EventEmitter has, it has all that you need to implement the Observer pattern right away in your classes with minimal code refactoring.

Installation

Supports PHP >7.0

composer require mario-legenda/php-event-emitter

Usage by extending the EventEmitter class

class Task extends EventEmitter
{
    public function __construct()
    {
        parent::__construct(); // don't forget to call the parent Event Emitter constructor

        $this->emit('classConstructed');
    }

    public function runTask(): ExtendedTask
    {
        $this->emit('preRunEvent');
        
        // some task specific code

        $this->emit('postRunEvent');

        $this->emit('taskFinished');

        return $this;
    }
}

$task = new Task();

$task
    ->runTask()
    ->on('classConstructed', function($val) {
        // called when the classConstructed event is emitted
    })
    ->on('preRunEvent', function($val) {
        // called when the preRunEvent event is emitted
    })
    ->on('postRunEvent', function($val) {
        // called when the postRunEvent event is emitted
    })
    ->on('taskFinished', function($val) {
        // called when the taskFinished event is emitted
    })

An alternative to calling EventEmitter::on() with a closure would be to create a class that implements the EventEmitter\CallableInterface that exposes a single method run(). The drawback is that in PHP, we cannot set a variable number of parameters for the run() method. Therefor, if you want to use this interface, you would have to nullify every one of the arguments that you expect to receive. For example...


use EventEmitter\CallableInterface;

class Callable implements CallableInterface 
{
    public function run($argumentOne = null, $argumentTwo = null) 
    {
    }
}

Multiple event handlers

It is possible to chain multiple identical events

$eventEmitter = new EventEmitter();

$eventEmitter->emit('event', 'some value');

$eventEmitter
    ->on('event', function($val) {
        // called once
    })
    ->on('event', function($val) {
       // called twice
    })
    ->on('event', function($val) {
       // called three times
    })
    

Usage within an object

class Task
{
    private $eventEmitter;
    
    public function __construct()
    {
        $this->eventEmitter = new EventEmitter();
    }

    public function runTask(): ExtendedTask
    {
        $this->eventEmitter->emit('preRunEvent');
        
        // some task specific code

        $this->eventEmitter->emit('postRunEvent');

        $this->eventEmitter->emit('taskFinished');

        return $this;
    }
    
    public function getEventEmitter(): EventEmitter 
    {
        return $this->eventEmitter;
    }
}

$task
    ->runTask()
    ->getEventEmitter()
    ->on('classConstructed', function($val) {
        // called when the classConstructed event is emitted
    })
    ->on('preRunEvent', function($val) {
        // called when the preRunEvent event is emitted
    })
    ->on('postRunEvent', function($val) {
        // called when the postRunEvent event is emitted
    })
    ->on('taskFinished', function($val) {
        // called when the taskFinished event is emitted
    })

As you can see, the two examples are almost identical. Creating the EventEmitter instance inside the Task class is not proper dependency injection but it enables us to use the EventEmitter seamlessly. The client code does not now that Task is using (or is) an EventEmitter.

Exception handling

EventEmitter can handle internal exceptions thrown by your code.

$eventEmitter = new EventEmitter(true); // notice the argument. That means that exceptions will be handled by EventEmitter

$eventEmitter->emit('event', 'some value');

$eventEmitter
    ->on('event', function($val) {
        throw new \RuntimeException();
    })
    ->exception(function(\Throwable $e) {
        // $e instanceof RuntimeException
    })

If you chain multiple events, every event after the one where an exception occurred will not be called.

$eventEmitter
    ->on('event', function($val) {
        throw new \RuntimeException();
    })
    ->on('otherEvent', function($val) {
        // this event is never called. Execution passes to the exception()
    })
    ->exception(function(\Throwable $e) {
        // $e instanceof RuntimeException
    })

API

EventEmitter::__construct($handleErrors = false)

EventEmitter constructor. If provided true as the first argument, exceptions will be handled by the EventEmitter. Else, exceptions will be propagated to client code

EventEmitter::emit(string $eventName, ...args): void

Registers an event to be called with EventEmitter::on(). Must be called before EventEmitter::on().

EventEmitter::on(string $eventName, $callback): EventEmitter

Executes an event. $callback has to be an instance of \Closure or a instance of EventEmitter\CallableInterface

EventEmitter::exception($callback)

Called only when $handleError argument is set to true in the constructor and an exception occurred in executing one of the events.

EventEmitter::removeEvent(string $eventName, bool $strictCheck = false): bool

Removes an event and returns true if successful or false on failure. If $strictCheck is true, an exception is throw if an event cannot be removed (if it doesn't exist for example).

EventEmitter::removeAllEvents()

Removes all events.

EventEmitter::getEventNames(): ?array

Returns an array of currently registered event names or null if there are none

mario-legenda/php-event-emitter 适用场景与选型建议

mario-legenda/php-event-emitter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 mario-legenda/php-event-emitter 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-02-28