使用订阅设计模式实现 Event (事件)机制

这里的设计参考了 PHP SPL 里面是 SplSubject 和 SplObserver 接口, API 方法参考了 Symfony EventDispatcher 。

代码:

<?php

interface EventInterface
{
    public function getName();

    public function attach(ListenerInterface $listener);

    public function detach(ListenerInterface $listener);

    public function notify();
}

interface ListenerInterface
{
    public function update(EventInterface $event);
}

class Event implements EventInterface
{
    protected $name;

    protected $listeners;

    public function __construct(string $name)
    {
        $this->name = $name;
        $this->listeners = new SplObjectStorage();
    }

    public function getName()
    {
        return $this->name;
    }

    public function attach(ListenerInterface $listener)
    {
        $this->listeners->attach($listener);
    }

    public function detach(ListenerInterface $listener)
    {
        $this->listeners->detach($listener);
    }

    public function notify()
    {
        foreach ($this->listeners as $listener) {
            $listener->update($this);
        }
    }
}

class Listener implements ListenerInterface
{

    public function update(EventInterface $event)
    {
        echo $event->getName() . "\n";
    }
}

class UserEvent extends Event
{

    const NAME = 'user.event';

    public $user;

    public function __construct($user)
    {
        $this->user = $user;
        parent::__construct(self::NAME);
    }
}

class EventDispatcher
{
    private $events = [];

    public function addListener($event, Listener $listener)
    {
        if ($event instanceof Event) {
            $eventName = $event->getName();
        } else {
            $eventName = $event;
            $event = new Event($eventName);
        }

        if (empty($this->events[$eventName])) {
            $event->attach($listener);
            $this->events[$eventName] = $event;
        } else {
            $this->events[$eventName]->attach($listener);
        }
    }

    public function dispatch(string $eventName)
    {
        $this->events[$eventName]->notify();
    }

}

$eventDispatcher = new EventDispatcher();
$event = new UserEvent(['username' => 'kevin']);
$eventDispatcher->addListener($event, new Listener());
$eventDispatcher->addListener('user.event', new Listener());
$eventDispatcher->addListener('anther.event', new Listener());

$eventDispatcher->dispatch(UserEvent::NAME);

echo 'success';