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!
With sphido/events can:
- listeners prioritization
- add/remove listeners
- filter values by functions
- stop propagation in function chain
- and have event default handler
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.
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!
Function filter()
return result of all callback function hook to event. Filtred value it's transmitted from one function to another.
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.
Sometimes you need ensure that will be handled by some default function, but need allow overridden that function by something else.
on('render', function () { echo 'my custom renderer'; });
echo ensure('render', function () {
return 'default renderer';
});
// print "my custom renderer"
Add and remove listener:
$handler = function() { };
on('event', $handler); // add
off('event', $handler); // remove
Add and remove all listeners:
$handler = function() { };
on('event', $handler);
on('event', $handler);
on('event', $handler);
off('event'); // remove all listeners
one('event', function(){ echo "called me once"; });
trigger('event'); // will print "called me once"
trigger('event'); // will print nothing
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
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'
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 events static stdClass
:
events(); // return all events
events()->hook; // return selected hook
Getting listeners array:
listeners('hook'); // return hook listeners
For more examples visit tests.