nacosvel / mitt
The Nacosvel Components
v1.0.0
2024-09-12 12:49 UTC
Requires
- php: >=8.0
- nacosvel/facde: ^1.0
README
Installation
You can install the package via Composer:
composer require nacosvel/mitt
Usage
Registering an Event Listener
Use the on method to register a listener for an event:
$mitt = new \Nacosvel\Mitt\Emitter(); $mitt->on('user.registered', function ($user) { echo "User registered: {$user->name}"; });
Emitting Events
Use the emit method to trigger an event:
$user = new User(['name' => 'John Doe']); $mitt->emit('user.registered', $user);
Removing an Event Listener
Use the off method to remove a listener:
$mitt->off('user.registered');
One-Time Listener
Use the once method to register a listener that will be triggered only once:
$mitt->once('user.updated', function ($user) { echo "User updated: {$user->name}"; });
Example
$mitt = new \Nacosvel\Mitt\Emitter(); $mitt->on('order.created', function ($order) { echo "Order created: {$order->id}"; }); $mitt->emit('order.created', ['id' => 123]);
License
Nacosvel Mitt is made available under the MIT License (MIT). Please see License File for more information.