symbiotic/event-contracts

扩展PSR 14事件调度器的接口

1.4.0 2022-09-11 15:21 UTC

This package is auto-updated.

Last update: 2024-09-12 04:41:35 UTC


README

README.RU.md 俄罗斯描述

添加了一个方法,可以通过传递事件名来添加监听器,并可以将类名作为监听器传递。这种方法在添加监听器时避免了使用反射,以及不必要的闭包对象。

安装

composer require symbiotic/event-contracts

suggest symbiotic/event - reailsation 

描述

use \Psr\EventDispatcher\ListenerProviderInterface;

interface ListenersInterface extends ListenerProviderInterface
{
    /**
     * @param string $event the class name or an arbitrary event name
     * (with an arbitrary name, you need a custom dispatcher not for PSR)
     *
     * @param \Closure|string $handler function or class name of the handler
     * The event handler class must implement the handle method  (...$params) or __invoke(...$params)
     * Important! When adding listeners as class names, you will need to adapt them to \Closure
     * when you return them in the getListenersForEvent() method!!!
     *
     * @return void
     */
    public function add(string $event, $handler): void;
}

\Closure包装器的示例实现

$listenerResolver = function($listener) {
    return function(object $event) use ($listener) {
            // if classname
            if(is_string($listener) && class_exists($listener)) {
                $listener = new $listener();
                return $listener->handle($event);
            } elseif(is_callable($listener)) {
                return $listener($event);
            }
    };
};
// You can implement your wrapper directly in the getListenersForEvent() method or throw a resolver with a PSR container

class ListenerProvider implements Symbiotic\Event\ListenersInterface
{
    protected $listenerResolver;

    protected $listeners = [];

    public function __construct(\Closure $listenerResolver = null)
    {
        $this->listenerResolver = $listenerResolver;
    }

    public function add(string $event, $handler): void
    {
        $this->listeners[$event][] = $handler;
    }

    public function getListenersForEvent(object $event): iterable
    {
        $parents = \class_parents($event);
        $implements = \class_implements($event);
        $classes = array_merge([\get_class($event)], $parents ?: [], $implements ?: []);
        $listeners = [];
        foreach ($classes as $v) {
            $listeners = array_merge($listeners, isset($this->listeners[$v]) ? $this->listeners[$v] : []);
        }
        $wrapper = $this->listenerResolver;

        return $wrapper ? array_map(function ($v) use ($wrapper) {
            return $wrapper($v);
        }, $listeners) : $listeners;

    }
}

/**
 *  use resolver
 **/
$listenersProvider = new \Symbiotic\Event\ListenerProvider($listenerResolver);