phoole/event

轻量级、强大且完全兼容PSR-14的事件管理库,适用于PHP

1.1.0 2019-11-19 06:57 UTC

This package is auto-updated.

Last update: 2024-09-19 17:26:17 UTC


README

Build Status Scrutinizer Code Quality Code Climate PHP 7 Latest Stable Version License

轻量级、强大且完全兼容PSR-14的事件管理库,适用于PHP。

安装

使用 composer 工具安装。

composer require "phoole/event"

或者将以下行添加到您的 composer.json

{
    "require": {
       "phoole/event": "1.*"
    }
}

功能

  • 完全支持PSR-14

  • 支持具有事件触发能力的类,使用 EventCapableTrait

  • 支持具有事件监听能力的类,使用 ListenerCapableTrait

用法

  • 快速入门

    use Phoole\Event\Provider;
    use Phoole\Event\Dispatcher;
    use Phoole\Event\Events\StoppableEvent;
    
    // create your own event class
    class MyEvent extends StoppableEvent {
    }
    
    // an invokable class for event handling
    class MyEventHandler
    {
        public function __invoke(MyEvent $event)
        {
             echo 'handling event...';
             return $event; // optional
        }
    } 
    
    // initiate event dispatcher
    $events = new Dispatcher(new Provider());
    
    // bind a callable with default priority 50 (0-100)
    $provider->attach(function(myEvent $event) {
        echo 'triggered...';
        return $event; // optional
    });
    
    // bind a invokable object with priority 80
    $provider->attach(new MyEventHandler(), 80);
    
    // fire the trigger, wil see output 'handling event...triggered...'
    $events->dispatch(new MyEvent());
  • 事件层次结构

    与许多事件库使用事件名触发不同,PSR-14兼容的事件库现在只支持使用事件对象触发。通过设置有意义的事件对象层次结构,开发人员可以在事件处理中获得极大的灵活性。

    监听器将 处理配置了事件参数的事件(及其子类)。

    use Phoole\Event\Events\StoppableEvent;
    
    // my own event hierarchy top
    class MyEvent extends StoppableEvent
    {
    }
    
    // my user authentication event
    class MyAuthEvent extends MyEvent
    {
        protected $userInfo;
    
        public function __construct(array $userInfo)
        {
            $this->userInfo = $userInfo;
        }
    
        public function getUserInfo(): array
        {
            return $this->userInfo;
        }
    }
    
    $provider = new Provider();
    $events = new Dispatcher($provider);
    
    // attach a listener to the event hierarchy top
    $provider->attach(function(MyEvent $event) {
        echo 'Event '.get_class($event).' fired';
        return $event; // optional
    });
    
    // attach a listener for logging users
    $provider->attach(function(MyAuthEvent $event)) use ($logger) {
        $logger->info(sprintf("User '%s' logged", $event->getUserInfo()['name']));
        return $event; // optional
    });
    
    ...
    
    // upon user logged in, trigger the event
    // BOTH listeners will process THIS event
    $events->dispatcher(new MyAuthEvent($userInfo));
  • 创建 监听器类

    监听器类可以实现 ListenerCapableInterface 并使用 ListenerCapableTrait

    use Phoole\Event\ListenerCapableTrait;
    use Phoole\Event\ListenerCapableInterface;
    
    // my listener class
    class MyListener implements ListenerCapableInterface
    {
        use ListenerCapableTrait;
    
        // define on listener method
        public function MethodOne(MyEvent $event)
        {
             echo 'handling MyEvent in MethodOne...';
        }
        
        // define another listener method
        public function MethodTwo(MyEvent $event)
        {
            echo 'handling MyEvent in MethodTwo...';
        }
    
        // config this listener
        protected function eventsListening()
        {
            return [
                'MethodOne',
                ['MethodTwo', 80] // with priority 80
            ];
        }
    }
    
    // global dispatcher & provider
    $provider = new Provider();
    $events = new Dispatcher($provider);
    
    // set provider is enough
    $listener = new MyListener();
    $listener->setProvider($provider);
    
    // fire the event
    $events->dispatch(new MyEvent());

    与容器库 phoole/di 一起,开发人员甚至无需担心设置分发器、提供者或注入提供者。

    use Phoole\Di\Container;
    
    // initiate the listener with dependencies injected
    $listener = Container::create(MyListener::class);
    
    // fire the event
    Container::events()->dispatch(new MyEvent());
  • 在您的类中触发事件

    EventCapableInterfaceEventCapableTrait 为开发人员在类中触发事件提供了能力。

    use Phoole\Event\EventCapableTrait;
    use Phoole\Event\EventCapableInterface;
    
    class MyEventCapable implements EventCapableInterface
    {
        use EventCapableTrait;
    
        public function myMethod()
        {
             $this->triggerEvent(new MyEvent());
        }
    }
    
    // global dispatcher & provider
    $provider = new Provider();
    $events = new Dispatcher($provider);
    
    $eventCapable = new MyEventCapable();
    $eventCapable->setDispatcher($dispatcher);
    
    // will trigger an event
    $eventCapable->myMethod();

    与容器库 phoole/di 一起,开发人员甚至无需担心设置分发器、提供者或注入分发器。

    // initiate object with dependencies injected
    $eventCapable = Container::create(MyEventCapable::class);
    
    // will trigger an event
    $eventCapable->myMethod();

测试

$ composer test

依赖项

  • PHP >= 7.2.0

  • Phoole/base 1.*

许可