diff --git a/README.md b/README.md index a273781..b61d584 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ _* feature complete_ - [Automatic translation of messages](#automatic-translation-of-messages) - [Accessibility](#accessibility) - [Unit testing](#unit-testing) + - [Extending behavior](#extending-behavior) - [View customization](#view-customization) - [Testing](#testing) - [Changelog](#changelog) @@ -400,6 +401,43 @@ final class RegisterUserControllerTest extends TestCase } ``` +### Extending behavior + +Imagine that you'd like to keep track of how many toasts are dispatched daily to display on an admin dashboard. +First, create a new class that encapsulates this logic: + +```php +final readonly class DailyCountingCollector implements Collector +{ + public function __construct(private Collector $next) {} + + public function collect(Toast $toast): void + { + // increment the counter on durable storage + + $this->next->collect($toast); + } + + public function release(): array + { + return $this->next->release(); + } +} +``` + +After that, extend the behavior in your `AppServiceProvider`: + +```php +public function register(): void +{ + $this->app->extend(Collector::class, + static fn (Collector $next) => new DailyCountingCollector($next) + ); +} +``` + +That's it! + ## View customization > **Warning** You **must** keep the `x-data` and `x-init` directives and you **must** keep using the `x-for` loop.