Overview
Architecture and design philosophy of WPZylos Events.
Philosophy
WPZylos Events provides a PSR-14 compliant event dispatcher that integrates seamlessly with WordPress plugins. It enables loose coupling between components through event-driven architecture.
Architecture
┌─────────────────────────────────────────────────────────────┐
| EventDispatcher |
+--───────────────────────────────────────────────────────────┤
| |
| ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ |
| | Events | | Listeners | | Subscribers | |
| | (Objects) | | (Callables) | | (Multi-handlers) | |
| +--────┬──────┘ +--────┬──────┘ +--────────┬──────────┘ |
| | | | |
| ▼ ▼ ▼ |
| ┌─────────────────────────────────────────────────────┐ |
| | ListenerProvider | |
| +--───────────────────────────────────────────────────┘ |
| |
+--───────────────────────────────────────────────────────────┘
Key Concepts
Events
Events are simple PHP objects that carry data about something that happened:
class UserCreated
{
public function __construct(
public readonly User $user,
public readonly \DateTimeImmutable $createdAt
) {}
}
Listeners
Listeners are callables that respond to specific events:
$provider->addListener(UserCreated::class, function (UserCreated $event) {
// React to user creation
});
Subscribers
Subscribers are classes that handle multiple related events:
class UserEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
UserCreated::class => 'onCreated',
UserDeleted::class => 'onDeleted',
];
}
}
PSR-14 Compliance
The package implements all PSR-14 interfaces:
| Interface | Implementation |
|---|---|
EventDispatcherInterface | EventDispatcher |
ListenerProviderInterface | ListenerProvider |
StoppableEventInterface | StoppableEvent (abstract class) |
vs WordPress Hooks
| Feature | WP Hooks | WPZylos Events |
|---|---|---|
| Type safety | Bad: | Good: |
| IDE support | Limited | Full |
| Testability | Difficult | Easy |
| Standards | WordPress | PSR-14 |