Beacon is an Event Management system built originally for use in the Phred Framework, but opened to the public to install in any codebase. It follows a strict PSR-14 (Event Dispatcher) implementation with an "Enterprise-First" architecture.
- **Inheritance Resolution**: Automatically resolves listeners for parent classes and interfaces; includes a configuration toggle for high-performance interface skipping.
- Discovery & Registration
- **Attribute Discovery**: PHP 8.2 `#[AsEventListener]` for zero-config registration.
- **Discovery Logic**: A `ListenerDiscovery` service scans directories (using PSR-4 mapping or explicit paths).
- **Discovery Cache**: PSR-16 cache support for scanning and mapping attributes (eliminates runtime scanning).
- **DI Integration**: Accept a PSR-11 container to resolve listener instances; discovery yields service IDs or class names for lazy instantiation.
- **Manual Registration**: Fluent API for one-off listeners or closures (uses `addListener`).
- Integration & Advanced Features
- **Async Dispatching**: Offloaded via `QueueingDispatcher` decorator (in Bridges); serializes the `EventEnvelope`.
- **PII & Logging**: Masking and sensitive data handling live strictly in Bridges or Middleware, keeping the engine core pure.
- **Resiliency & Error Handling**: `continue` mode emits a `DispatchFailed` event containing the listener, exception, and original event payload for audit/retry.
- **Enterprise Telemetry**: Trace execution timing and listener flow.
- **Testing Helpers**: PHPUnit traits for easy event assertions.
# Concepts: Unified Event Listening
## The Dispatcher API
The primary way to trigger an event is through the `$dispatch()` method.
```php
$dispatcher->dispatch($event);
```
## Listener Registration
We prefer structured class-based listeners, but support closures for lightweight tasks.
-`addListener(string $event, callable $listener, int $priority = 0)`
-`addSubscriber(SubscriberInterface $subscriber)`
# API Examples (Drafts)
## Basic Dispatching
```php
$dispatcher = new EventDispatcher($provider);
$event = new UserRegistered($user);
// "Enterprise" Dispatching
$dispatcher->dispatch($event);
```
## Middleware Example
```php
class LoggingMiddleware implements DispatchMiddlewareInterface {
public function handle(object $event, callable $next): object {
$log->info("Dispatching: " . $event::class);
return $next($event);
}
}
```
## Class-based Subscriber (Unified)
```php
class UserSubscriber implements SubscriberInterface {
public static function getSubscribedEvents(): array {
return [
UserRegistered::class => 'onUserRegistered',
UserDeleted::class => ['onUserDeleted', 10], // with priority
];
}
public function onUserRegistered(UserRegistered $event): void { /* ... */ }