亚洲精品成人_精品成人一区_999视频在线播放_免费黄色在线_亚洲成人久久久_久久www免费视频

自定義事件

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

使用示例

定義事件容器

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

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://www.b3f21.cn
 * @document http://www.b3f21.cn
 * @contact http://www.b3f21.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.b3f21.cn
 * @document http://www.b3f21.cn
 * @contact http://www.b3f21.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.b3f21.cn
 * @document http://www.b3f21.cn
 * @contact http://www.b3f21.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!

主站蜘蛛池模板: 自拍视频在线 | 天天干天天操天天操 | 九九热国产在线 | av福利在线观看 | 免费av网址在线观看 | 久久久国产成人一区二区三区 | 欧美日本韩国在线 | 国产美女在线播放 | 欧美成人精精品一区二区频 | 快射天堂网2021 | 国产98在线传媒麻豆有限公司 | 国产视频一区二区三区在线观看 | 国产理论在线 | 免费毛片播放 | 欧美视频黄 | 97色伦图片97综合影院 | 涩涩屋导航深夜福利 | av黄色在线观看 | 韩日毛片 | 99国内精品久久久久久久夜夜嗨 | 亚洲一区二区三区免费视频 | 亚洲欧美日韩高清 | 成人免费观看视频 | 99久久综合国产精品二区国产 | 日本不卡视频在线观看 | 精品一区免费观看 | 免费麻豆av | 久久久在线 | 欧美成人精品一区二区 | 一级特黄视频 | youporn久久精品国产 | 黄色拍拍视频 | 成人在线观看网站 | 久操欧美 | 亚洲精品观看 | 国产在线观看中文字幕 | 欧美日韩在线第一页 | 2020阿v天堂网m| 一级毛片免费的 | 国产视频在 | 亚洲精品视频一区二区三区 |