Skip to content

Commit

Permalink
Merge pull request #32 from innocenzi/docs/closure-based-listeners
Browse files Browse the repository at this point in the history
docs(event-bus): document closure-based listeners
  • Loading branch information
brendt authored Oct 11, 2024
2 parents ba44c0a + 803c24e commit eea011e
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions app/Front/Docs/Content/framework/07-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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).
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).

0 comments on commit eea011e

Please sign in to comment.