2017 © Pedro Peláez
 

library events

Super simple event dispatching library for PHP

image

sphido/events

Super simple event dispatching library for PHP

  • Thursday, February 23, 2017
  • by OzzyCzech
  • Repository
  • 1 Watchers
  • 3 Stars
  • 4,606 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 14 Versions
  • 12 % Grown

The README.md

Sphido / Events

Events is simple pure functional event dispatching library for PHP 5.5+ and have nice and clear interface with function on(), one(), off(), trigger(), filter(), ensure(), listeners(), events() - that's all!, (*1)

With sphido/events can:, (*2)

  • listeners prioritization
  • add/remove listeners
  • filter values by functions
  • stop propagation in function chain
  • and have event default handler

Trigger event

on('event', function () {
  echo "wow it's works yeah!";
});

trigger('event'); // print wow it's works yeah!

Function trigger() return array of all callback listeners results., (*3)

Listeners prioritization

on( 'event', function () { echo " stay hungry"; }, 200);
on( 'event', function () { echo "stay foolish"; }, 100);

trigger('event'); // print "stay foolish stay hungry"

Notice: default event priority is 10!, (*4)

Filter - change value by listeners

Function filter() return result of all callback function hook to event. Filtred value it's transmitted from one function to another., (*5)

add_filter('price', function($price) {
  return (int)$price . ' USD';
});

echo filter('price', 100); // print 100 USD

add_filter('price', function($price) {
  return 'The price is: ' . $price ;
});

echo filter('price', 100); // print The price is: 100 USD

This function it's basically copy of Wordpress add_filter and apply_filters functions., (*6)

Ensure handler

Sometimes you need ensure that will be handled by some default function, but need allow overridden that function by something else., (*7)

on('render', function () { echo 'my custom renderer'; });

echo ensure('render', function () {
  return 'default renderer';
});
// print "my custom renderer"

Remove listener from event

Add and remove listener:, (*8)

$handler = function() { };
on('event', $handler); // add
off('event', $handler); // remove

Add and remove all listeners:, (*9)

$handler = function() { };
on('event', $handler);
on('event', $handler);
on('event', $handler);
off('event'); // remove all listeners

Call listener just once

one('event', function(){ echo "called me once"; });

trigger('event'); // will print "called me once" 
trigger('event'); // will print nothing

Stop propagation example

on('event', function () { echo 'a'; });
on('event', function () { echo 'b'; });
on('event', function () { echo 'c'; return false; }); // stop propagation now
on('event', function () { echo 'd'; });

trigger('event'); // print abc

Trigger multiple events at once

on('one', function () { echo 'you know that: ';});
on('two', function ($a, $b) { echo " $a is not $b ";});

trigger(['one', 'two'], 100, 200); // print 'you know that: 100 is not 200'

Apply multiple filters at once

add_filter('one', function ($array) { return array_sum($array);});
add_filter('two', function ($value) { return 'Suma is ' . $value;});

filter(['one', 'two'], [10, 20, 30, 40])); // output will be 'Suma is 100'

Getting listeners array

Getting events static stdClass:, (*10)

events(); // return all events
events()->hook; // return selected hook

Getting listeners array:, (*11)

listeners('hook'); // return hook listeners

For more examples visit tests., (*12)

The Versions

23/02 2017

dev-master

9999999-dev https://www.sphido.org

Super simple event dispatching library for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0

 

The Development Requires

filter simple event trigger handle

28/11 2016

v0.4.0

0.4.0.0 https://www.sphido.org

Super simple event dispatching library for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=7.0

 

The Development Requires

filter simple event trigger handle

29/04 2016

v0.3.0

0.3.0.0 http://www.sphido.org

Super simple event dispatching library for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.6

 

The Development Requires

filter simple event trigger handle

06/06 2015

v0.2.1

0.2.1.0 http://www.sphido.org

Super simple event dispatching library for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.6

 

The Development Requires

filter simple event trigger handle

24/10 2014

v0.1.0

0.1.0.0 http://www.omdesign.cz

Super simple event dispatching library for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.6

 

The Development Requires

filter simple event trigger handle

03/10 2014

v0.0.9

0.0.9.0 http://www.omdesign.cz

Super simple event dispatching library for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.6

 

The Development Requires

filter simple event trigger handle

30/07 2014

v0.0.8

0.0.8.0 http://www.omdesign.cz

Super simple event dispatching library for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.6

 

The Development Requires

filter simple event trigger handle

22/02 2014

v0.0.7

0.0.7.0 http://www.omdesign.cz

Super simple event dispatching library for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.6

 

The Development Requires

filter simple event trigger handle

07/02 2014

v0.0.6

0.0.6.0 http://www.omdesign.cz

Super simple event dispatching library for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.6

 

The Development Requires

filter simple event trigger handle

31/01 2014

v0.0.5

0.0.5.0 http://www.omdesign.cz

Super simple event dispatching library for PHP

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.6

 

The Development Requires

filter simple event trigger handle

31/01 2014

v0.0.4

0.0.4.0

Super simple event dispatching library for PHP

  Sources   Download

The Requires

  • php >=5.5.6

 

The Development Requires

31/01 2014

v0.0.3

0.0.3.0

Super simple event dispatching library for PHP

  Sources   Download

The Requires

  • php >=5.5.6

 

The Development Requires

31/01 2014

v0.0.2

0.0.2.0

Super simple event dispatching library for PHP

  Sources   Download

The Requires

  • php >=5.5.6

 

The Development Requires

27/01 2014

v0.0.1

0.0.1.0

Super simple event dispatching library for PHP

  Sources   Download

The Requires

  • php >=5.5.6

 

The Development Requires