dev-master
9999999-dev https://github.com/phpf/EventPhpf Event package
MIT
The Requires
- php >=5.3
by The PHPf Developer
hook filter event action
Phpf Event package
Simple, powerful, and extendable JavaScript-like events for PHP., (*1)
on()
to bind, trigger()
to emit)preventDefault()
), or stop propagation altogether (stopPropagation()
)one()
)off()
)
$events = new \Phpf\Event\Manager; $events->on('myevent', function ($event, $myarg) { if ($event->isDefaultPrevented()){ return; } echo "I'm doing my event called $myarg!"; }); $events->trigger('myevent', 'Example'); // outputs "I'm doing my event called Example!"
By default, events are added with a priority of 10 and executed from lowest to highest:, (*2)
$events->on('myevent', function ($event) { echo "Child"; }, 15); $events->on('myevent', function ($event) { echo "Bear"; }, 9); $events->trigger('myevent'); // outputs "BearChild"
You can change the sort order to high-to-low like so:, (*3)
$events->setSortOrder(\Phpf\Event\Manager::SORT_HIGH_LOW);
To cancel an event, simply call the off()
method:, (*4)
$events->off('myevent');
This will remove any listeners bound to the event, so they will not be called if subsequently triggered., (*5)
You can limit an event's execution to a single listener by using the one()
method instead of on()
:, (*6)
$events->one('myevent', function ($event) { echo "I will print."; }); $events->on('myevent', function ($event) { echo "I will not print, even though I was bound later."; }); $events->trigger('myevent'); // Prints "I will print."
When events listeners are executed, any value returned from the listener will be collected; on completion (or propagation stoppage), the results will be returned as an indexed array., (*7)
For example:, (*8)
$events->on('myevent', function ($event) { return 'Hello'; }); $events->on('myevent', function ($event) { return 'Goodbye'; }); $results = $events->trigger('myevent'); print_r($results); // array(0 => 'Hello', 1 => 'Goodbye');
Like JS, propagation of events can be stopped by a listener at any time., (*9)
$events->on('myevent', function ($event) { echo "This will be printed"; $event->stopPropagation(); }); $events->on('myevent', function ($event) { echo "This will not be printed."; });
$events->on('myevent', function ($event) { echo "I will not be called."; }, 12); $events->on('myevent', function ($event) { echo "I will print second."; $event->stopPropagation(); }, 11); $events->on('myevent', function ($event) { echo "I will print first."; });
The completed events and their returned arrays are stored for later use. The event object can be retrieved using the event()
method, the results using the result()
method:, (*10)
$results = $events->trigger('myevent'); // ...later on, in another script: $myevent = $events->event('myevent'); $sameResults = $events->result('myevent'); // ... do stuff with event/results // re-trigger the event ! $newResults = $events->trigger($myevent);
You can also pass an instance of the Event
class to the trigger()
method instead of the event ID. This way, you can use custom event objects., (*11)
For example, if you want listeners to be able to modify a single returned value (a "filter"), you could create a class like this:, (*12)
namespace MyEvents; use Phpf\Event\Event; class FilterEvent extends Event { protected $value; public function __construct($id, $initial_value) { // must call parent constructor with id if overriding parent::__construct($id); $this->value = $initial_value; } public function getValue() { return $this->value; } public function setValue($value) { $this->value = $value; } }
And then use it like so:, (*13)
$events->on('myFilterEvent', function ($event) { $newval = rtrim($event->getValue(), 's') . ' objects'; $event->setValue($newval); }); $events->on('myFilterEvent', function ($event) { $event->setValue($event->getValue() . ' are cool.'); }); // Create new object with initial value "Custom events" $filterEvent = new \MyEvents\FilterEvent('myFilterEvent', 'Custom events'); $events->trigger($filterEvent); $filtered = $events->event('myFilterEvent'); echo $filtered->getValue(); // Prints "Custom event objects are cool."
Phpf Event package
MIT
hook filter event action