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:

InterfaceImplementation
EventDispatcherInterfaceEventDispatcher
ListenerProviderInterfaceListenerProvider
StoppableEventInterfaceStoppableEvent (abstract class)

vs WordPress Hooks

FeatureWP HooksWPZylos Events
Type safetyBad:Good:
IDE supportLimitedFull
TestabilityDifficultEasy
StandardsWordPressPSR-14