walmsles/event-dispatch

此包已被废弃且不再维护。未建议替代包。

解耦的事件订阅/派发类

1.0.0 2016-05-10 01:34 UTC

This package is not auto-updated.

Last update: 2020-10-02 21:23:15 UTC


README

简单的发布/订阅事件派发器。这个项目是对TDD的探索——我应该更多地这样做,并想为什么不在其他项目中尝试一下,做一些稍微有用的东西呢 :-)

此包提供什么

此包提供了一个干净、解耦的发布/订阅接口,用于派发自定义定义的事件。事件可以完全自定义,包括传递到事件中的数据。事件通过PHP的call_user_func_array()进行派发,因此要传递多个参数,请传递一个数组有效载荷作为数组().事件监听器可以是任何PHP可调用结构——闭包、函数、类方法。参考PHP文档call_user_func_array

使用方法

use EventDispatch\Dispatcher;

$dispatcher = new Dispatcher();

$dispatcher->subscribe('myEvent', function($myData1, $myData2) {
    // so somehting with $myData
    
    return $myData1 . ' ' . $myData2;
});

$dispatch->dispatch('myEvent', array('event data', 'event data 2');

安装

composer require walmsles/event-dispatch

如果您没有composer - 获取它! http://getcomposer.org

许可证 - MIT

The MIT License (MIT)

Copyright (c) 2016 Michael Walmsley

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.