Events & Listeners
Yabasi framework provides a robust event system that allows you to easily implement and manage application events. This system helps in decoupling various aspects of your application, making it more maintainable and extensible.
Event Dispatcher
The core of Yabasi's event system is the EventDispatcher
class. It manages event registration and dispatching.
namespace Yabasi\Events;
use Closure;
class EventDispatcher
{
protected array $listeners = [];
public function listen(string $eventName, $listener): void
{
$this->listeners[$eventName][] = $listener;
}
public function dispatch(Event $event): void
{
$eventName = $event->getName();
if (isset($this->listeners[$eventName])) {
foreach ($this->listeners[$eventName] as $listener) {
if ($listener instanceof Closure) {
$listener($event);
} elseif (is_array($listener) && count($listener) == 2) {
[$class, $method] = $listener;
if (is_string($class)) {
$class = new $class();
}
$class->$method($event);
}
}
}
}
}
Creating Events
Events in Yabasi are simple classes that extend the base Event
class. They typically contain data relevant to the event.
namespace App\Events;
use Yabasi\Events\Event;
use App\Models\User;
class UserRegisteredEvent extends Event
{
public function __construct(public User $user)
{
parent::__construct('user.registered');
}
}
Creating Listeners
Listeners are classes or closures that respond to specific events. They perform actions based on the event data.
namespace App\Listeners;
use App\Events\UserRegisteredEvent;
class SendWelcomeEmail
{
public function handle(UserRegisteredEvent $event)
{
// Send welcome email to $event->user
}
}
Registering Events and Listeners
You can register events and listeners in your service providers or anywhere you have access to the EventDispatcher.
namespace App\Providers;
use Yabasi\Events\EventDispatcher;
use Yabasi\ServiceProvider\ServiceProvider;
use App\Events\UserRegisteredEvent;
use App\Listeners\SendWelcomeEmail;
class EventServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->container->get(EventDispatcher::class)->listen(UserRegisteredEvent::class, [SendWelcomeEmail::class, 'handle']);
}
}
Dispatching Events
To dispatch an event, you can use the EventDispatcher's dispatch method.
use App\Events\UserRegisteredEvent;
use Yabasi\Events\EventDispatcher;
class UserController
{
public function register(Request $request, EventDispatcher $eventDispatcher)
{
// User registration logic
$user = User::create($request->all());
// Dispatch the event
$eventDispatcher->dispatch(new UserRegisteredEvent($user));
return response()->json(['message' => 'User registered successfully']);
}
}
Event Subscribers
Event subscribers are classes that can listen to multiple events. They provide a more organized way to handle related events.
namespace App\Listeners;
use Yabasi\Events\EventDispatcher;
class UserEventSubscriber
{
public function subscribe(EventDispatcher $events): void
{
$events->listen(
'user.registered',
[$this, 'handleUserRegistered']
);
$events->listen(
'user.login',
[$this, 'handleUserLogin']
);
}
public function handleUserRegistered($event) { /* ... */ }
public function handleUserLogin($event) { /* ... */ }
}
Best Practices
- Keep events and listeners small and focused on a single responsibility.
- Use event names that clearly describe what happened (e.g., 'user.registered', 'order.shipped').
- Consider using event subscribers for closely related events.
- Avoid performing time-consuming tasks directly in listeners. Instead, dispatch jobs to be processed in the background.
- Use type-hinting in your listener methods to ensure you're working with the correct event type.
By leveraging Yabasi's event system, you can create more modular and maintainable applications. Events allow you to decouple various parts of your application, making it easier to add new functionality without modifying existing code.