2017 © Pedro Peláez
 

library evenement-plus

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

image

attozk/evenement-plus

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

  • Sunday, November 2, 2014
  • by attozk
  • Repository
  • 1 Watchers
  • 1 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 56 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Événement-Plus

This is a fork of Événement by Igor Wiedler with advanced dispatching options., (*1)

Build Status, (*2)

Install

The recommended way to install Événement-Plus is through composer., (*3)

Just create a composer.json file for your project:, (*4)

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

And run these two commands to install it:, (*5)

$ 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:, (*6)

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

Usage

Included are two types of event emitters:, (*7)

  • 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., (*8)

Creating an Emitter

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

Adding Listeners

Addint multiple listeners using an array:, (*9)

<?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:, (*10)

<?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:, (*11)

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

<?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:, (*13)

<?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., (*14)

Following shows benchmarks for various scenarios., (*15)

Fixed number of listeners and variable emits, (*16)

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, (*17)

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., (*18)

The Versions

02/11 2014

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

event-dispatcher event-emitter

02/11 2012

v2.0.0

2.0.0.0

Événement is a very simple event dispatching library for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

event-dispatcher event-emitter

30/05 2012

v1.0.0

1.0.0.0

Événement is a very simple event dispatching library for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

event-dispatcher

30/10 2011

0.1.3

0.1.3.0 https://github.com/igorw/evenement

Événement is a very simple event dispatching library for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

event-dispatcher

22/09 2011

0.1.0

0.1.0.0 https://github.com/igorw/Evenement

Événement is a very simple event dispatching library for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

event-dispatcher