五月天激情丁香,国产精品2019,国产成人精品亚洲2020,国产精品免费视频一区二区三区,开心久久婷婷综合中文字幕,天堂视频在线观看免费完整版

自定義事件

EasySwoole 中,可以通過 \EasySwoole\Component\Container 容器實現自定義事件功能。

使用示例

定義事件容器

新增 App\Event\Event.php 文件,內容如下:

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.fe88.cn
 * @document http://www.fe88.cn
 * @contact http://www.fe88.cn/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace App\Event;

use EasySwoole\Component\Container;
use EasySwoole\Component\Singleton;

class Event extends Container
{
    use Singleton;

    public function set($key, $item)
    {
        if (is_callable($item)) {
            return parent::set($key, $item);
        } else {
            return false;
        }
    }

    public function hook($event, ...$args)
    {
        $call = $this->get($event);
        if (is_callable($call)) {
            return call_user_func($call, ...$args);
        } else {
            return null;
        }
    }
}

注冊事件

在框架的 initialize 事件(即項目根目錄的 EasySwooleEvent.phpinitialize 函數)中進行注冊事件:

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.fe88.cn
 * @document http://www.fe88.cn
 * @contact http://www.fe88.cn/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace EasySwoole\EasySwoole;

use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;

class EasySwooleEvent implements Event
{
    public static function initialize()
    {
        // 注冊事件
        \App\Event\Event::getInstance()->set('test', function () {
            echo 'this is test event!' . PHP_EOL;
        });
    }

    public static function mainServerCreate(EventRegister $register)
    {

    }
}

觸發事件

注冊事件之后,就可以在框架的任意位置觸發事件來進行調用,調用形式如下:

<?php
\App\Event\Event::getInstance()->hook('test');

在控制器中觸發事件進行調用

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.fe88.cn
 * @document http://www.fe88.cn
 * @contact http://www.fe88.cn/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\Controller;

class Index extends Controller
{
    public function index()
    {
        // 觸發事件
        \App\Event\Event::getInstance()->hook('test');
    }
}

訪問 http://127.0.0.1:9501/ (示例請求地址)就可以看到終端顯示如下結果:this is test event!。

主站蜘蛛池模板: 欧美性69| 久99久热只有精品国产男同 | 日韩不卡在线 | 激情网在线 | 图片区精品综合自拍 | 99视频在线观看高清 | 久久精品综合视频 | 我要看一级毛片 | 玖玖五月| 免费a级毛片 | 男人阁久久 | 免费观看黄色小视频 | 婷婷网址 | 久久亚洲视频 | 五月天激情小说网 | 亚洲国产日韩a在线亚洲 | 精品综合久久久久97 | 久久亚洲精品玖玖玖玖 | 国产人成77777视频网站 | 久久成人福利视频 | 国产精品亚洲自在线播放页码 | 国外欧美一区另类中文字幕 | 欧美男女交性过程视频 | 久久这里只有精品久久 | 91亚洲国产成人久久精品网站 | 看片视频在线观看 | 欧美综合自拍亚洲综合网 | 久久国内精品视频 | 99国产精品| 精品国产亚洲人成在线 | 一级毛片在线全部免费播放 | 久久天天躁狠狠躁狠狠躁 | 国产精品久久久久久久久久免费 | 欧美日韩亚洲国内综合网俺 | 奇米第四色在线播放 | 久久香蕉精品视频 | 免费一区二区三区毛片10分钟 | 久草天堂| 色精品一区二区三区 | 日韩欧美一区二区三区不卡视频 | 久久久久国产精品免费免费 |