定制 attozk/evenement-plus 二次开发

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

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

attozk/evenement-plus

最新稳定版本:v2.0.0

Composer 安装命令:

composer require attozk/evenement-plus

包简介

Événement-plus is an advanced event dispatching library for PHP

README 文档

README

This is a fork of Événement by Igor Wiedler with advanced dispatching options.

Build Status

Install

The recommended way to install Événement-Plus is through composer.

Just create a composer.json file for your project:

{
    "require": {
        "attozk/evenement-plus": "1.0.*"
    }
}

And run these two commands to install it:

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install

Now you can add the autoloader, and you will have access to the library:

<?php
require 'vendor/autoload.php';

Usage

Included are two types of event emitters:

  • EventEmitter - the original from Événement
  • EventEmitterRegex - with regex based event dispatching

EventEmitter Usage

Creating an Emitter

<?php
$emitter = new Evenement\EventEmitter();

Adding Listeners

<?php
$emitter->on('user.created', function (User $user) use ($logger) {
    $logger->log(sprintf("User '%s' was created.", $user->getLogin()));
});

Emitting Events

<?php
$emitter->emit('user.created', array($user));

EventEmitterRegex Usage

EventEmitterRegex uses regex to dispatching events.

Creating an Emitter

<?php
$emitter = new Evenement\EventEmitterRegex();

Adding Listeners

Addint multiple listeners using an array:

<?php
$emitter->on(['request.www.domain.com', 'request.www.example.com'], function (Request $request) use ($httpd) {
    $httpd->response(404, 'Not found.');
});

Above is the same as adding one listener at a time:

<?php
$emitter->on('request.www.domain.com', function (Request $request) use ($httpd) {
    $httpd->response(404, 'Not found.');
});

$emitter->on('request.www.example.com', function (Request $request) use ($httpd) {
    $httpd->response(404, 'Not found.');
});

Adding regex listeners:

The following listeners would match request.www.domain.\w+ and request.example.(com|pk) patterns

<?php
$emitter->on(['request.www.domain.\w+', 'request.example.(com|pk)'], function (Request $request) use ($httpd) {
    $httpd->response(404, 'Not found.');
});

Emitting Events

<?php
$emitter->emit('user.created', array($user));

// or multiple evetns at once
$emitter->emit(['user.created', 'welcome'], array($user));

// or emit using regex patterns
$emitter->emit(['request.*.pk', 'request.*.domain.pk'], array($request));

Emitting Events First Match Win

<?php
$emitter->emitFirstMatch(['request.*.pk', 'request.*.domain.pk'], array($request));

Emitting Events With Default Fallback-Callback

For cases when you want to perform a default action when there are no listeners:

<?php
$fallback = function() use($logger) { 
    $logger->debug(...);
};

$emitter->emit('user.created', array($user), $fallback);

// or multiple evetns at once
$emitter->emit(['user.created', 'welcome'], array($user), $fallback);

// or emit using regex patterns
$emitter->emit(['request.*.pk', 'request.*.domain.pk'], array($request), $fallback);

Benchmarking

There is no doubt regex matching would be slower as the number of listeners goes up.

Following shows benchmarks for various scenarios.

Fixed number of listeners and variable emits

Time for 100 listeners and 10 emits
EventEmitter:                            1     ms
EventEmitterRegex:                       3     ms

Time for 100 listeners and 100 emits
EventEmitter:                            1     ms
EventEmitterRegex:                       16    ms

Time for 100 listeners and 1000 emits
EventEmitter:                            1     ms
EventEmitterRegex:                       98    ms

Time for 100 listeners and 10000 emits
EventEmitter:                            11    ms
EventEmitterRegex:                       1001  ms

Same number of listeners and emits

Time for 10 listeners and emits
EventEmitter:                            1     ms
EventEmitterRegex:                       1     ms

Time for 100 listeners and emits
EventEmitter:                            1     ms
EventEmitterRegex:                       11    ms

Time for 1000 listeners and emits
EventEmitter:                            7     ms
EventEmitterRegex:                       1355  ms

Tests

$ phpunit

License

MIT, see LICENSE.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-11-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固