Simple PHP library that allow you to dispatch events in your system.
Install it via Composer (kovalevsky-projects/event-dispatcher
on Packagist)
Here's the simple example of the usage:
$dispatcher = new \KovalevskyProjects\EventDispatcher\EventDispatcher();
$dispatcher->on('hello.world', function() {
echo 'Hello World!';
});
$dispatcher->trigger('hello.world');
Also you can use event parameters. It can be single parameter or an array of the parameters:
$dispatcher->on('hello.world', function($text) {
echo $text;
});
$dispatcher->on('foo.bar', function($foo, $bar) {
echo $foo + $bar;
});
$dispatcher->trigger('hello.world', 'Hello World!');
// You can use simple numeric arrays instead of associative
$dispatcher->trigger('foo.bar', array(
'foo' => 2,
'bar' => 2,
));
trigger($event, $parameters = null)
- Dispatches the specified event.
You can specify single parameter or an array of parameters as second argument:
$dispatcher->trigger('foo.bar', 'some parameter');
// or
$dispatcher->trigger('foo.bar', [
'one' => 'parameter one',
'two' => 'parameter two',
]);
on($event, $action)
- Adds the action for the event.
$dispatcher->on('post.update', function($post, $author) {
notify('The post ' . $post->title . ' updated by the ' . $author);
});
off($event, $action)
- Removes the action.
$dispatcher->on('some.action', $callableFunction);
// ...
$dispatcher->off('some.action', $callableFunction);
Please note: If your are using the closures, then you can't remove the action using Off()
method.
bind($events, $action = null)
- Attaches the action for the more than one event.
$dispatcher->bind('foo bar baz', function() {
echo 'You will see this message when foo, bar and baz events will be triggered';
});
// or with array
$dispatcher->bind(['foo', 'bar', 'baz'], function() { ... });
// or you can specify different actions for the each event
$dispatcher->bind([
'foo' => function() { ... },
'bar' => function() { ... },
'baz' => function() { ... },
]);
unbind($events = null)
- Removes all previously attached actions.
$dispatcher->unbind('foo bar baz');
// or
$dispatcher->unbind(['foo', 'bar', 'baz']);
// or you can remove ALL actions.
$dispatcher->unbind();
has($event, $action = null)
- Checks whether the specified event has action.
$dispatcher->has('foo');
// or you can check for the specific action
$dispatcher->has('foo', $someAction);