An event emitter that supports namespaces.
$ composer require benit8/event-emitter
You can either extend the EventEmitter
class or use the EventEmitterTrait
. An interface
EventEmitterInterface
is also available.
use Benit8\EventEmitter\EventEmitterTrait;
class MyReactor
{
use EventEmitterTrait;
}
$ev->on('user.created', function ($user) {
// Fired every time a 'user.created' event is emitted
});
$ev->on('user', function ($user) {
// Listening to all 'user.*' events
});
$ev->once('user.created', function ($user) {
// Will remove itself after firing
});
$user = new User(/* ... */);
// Will trigger 'user.created' and 'user' listeners
$ev->emit('user.created', $user);
$ev->removeListener('user.created', $myCallable);
// All listeners
$ev->removeAllListeners();
// Subset listeners
$ev->removeAllListeners('user');