sabre/event
Lightweight PHP library for event-driven development: EventEmitter, promises, an event loop, and coroutines. Designed for building asynchronous, event-based applications. Documentation at sabre.io/event. Requires PHP 7.1.
Install the package with composer require sabre/event "^6.0" (requires PHP 7.4+). Begin by mastering the Emitter class for synchronous event management—e.g., attaching listeners via $emitter->on('user.created', $callback) and triggering with $emitter->emit('user.created', $payload). For asynchronous work, start with simple Promise usage: Promise::resolve('hello')->then(fn($v) => echo $v);. The official examples repo includes clear demos: simple-event-emitter.php, promise-all.php, and event-loop.php are ideal first reads.
EmitterTrait in domain services (e.g., OrderService) to expose on()/emit()—ideal for decoupling order confirmation, shipping, and analytics without tight integration.Promise\all() for parallel HTTP requests (e.g., fetching user + order + inventory) or Promise\race() for fallback logic.EventLoop::addPeriodicTimer() for background tasks (e.g., health checks) and EventLoop::run() to keep workers alive while waiting on async operations.coroutine(function () {
$user = yield $this->fetchUser($id);
$orders = yield $this->fetchOrders($user['id']);
return compact('user', 'orders');
})->then($this->handleCompleteProfile(...));
on($event, $callback, $priority) to enforce execution order (e.g., audit logger before cache invalidator).int $priority in Emitter::on()). Extending Emitter requires matching signatures—PHPStan level 9 (now default in 6.0.1) will catch mismatches.Promise and EventLoop but forget to call EventLoop::run(), your then() callbacks will never fire—common in CLI scripts where developers assume async = non-blocking without驱动.?float $microseconds). Ensure your static analysis ignores null-related false positives.WildcardEmitter::emit('user.created.admin', $data) matches listeners for user.created.*, but no ordering guarantee exists across wildcards—avoid overlapping patterns in critical paths.wait() in HTTP context: While Promise::wait() blocks until resolution, never use it in Laravel controllers—it negates async benefits and blocks the web server. Reserve for CLI, queue workers, or testing.once() vs on() priority bug: In older versions, once() ignored priority—fixed in v2.0.1+, but verify your composer.lock excludes broken legacy versions (≤2.0.0).How can I help you explore Laravel packages today?