diff --git a/app/Front/Docs/Content/framework/07-events.md b/app/Front/Docs/Content/framework/07-events.md index 08d34f6..cea1551 100644 --- a/app/Front/Docs/Content/framework/07-events.md +++ b/app/Front/Docs/Content/framework/07-events.md @@ -39,6 +39,20 @@ final class CreateUserCommand Note that handler method names can be anything: invokable methods, `handleUserCreated()`, `onUserCreated()`, `whateverYouWant()`, … +Alternatively, event listeners can be registered as closures on the `EventBus` instance: + +```php +#[ConsoleCommand(name: 'users:sync')] +public function __invoke(): void +{ + $this->eventBus->listen(UserCreated::class, function (UserCreated $event) { + $this->console->info("User {$event->name} created."); + }); + + $this->synchronizeUsers(); +} +``` + Triggering an event can be done with the `event()` function: ```php @@ -50,7 +64,7 @@ Alternatively to using the `event()` function, you can inject the `EventBus`, an ```php use Tempest\EventBus\EventBus; -final readonly class UserController() +final readonly class UserController { public function __construct( private EventBus $eventBus, @@ -148,4 +162,4 @@ return new EventBusConfig( ## Built-in framework events -Tempest already uses a handful of internal events, although they aren't properly documented yet. We plan no added a variety of framework-specific events that users can hook into in [version 1.1](https://github.com/tempestphp/tempest-framework/issues/268). \ No newline at end of file +Tempest already uses a handful of internal events, although they aren't properly documented yet. We plan no added a variety of framework-specific events that users can hook into in [version 1.1](https://github.com/tempestphp/tempest-framework/issues/268).