在线看片18_亚洲性吧_在线污_神马午夜达达兔_奇米色网_在线免费看污

自定義事件

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

使用示例

定義事件容器

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

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

主站蜘蛛池模板: 久久久天堂国产精品女人 | 婷婷国产 | 欧美成人精品欧美一级私黄 | 欧州一区二区 | 男人亚洲天堂 | 国产精品久久综合 | 麻豆视频在线免费看 | 天堂va | 日韩精品福利 | 色小姐综合网 | 黄色av中文字幕 | 国产午夜精品一区二区三区嫩草 | 香蕉av在线播放 | 天天干天天操天天射 | 成人免费看黄 | 婷婷丁香在线 | 一二三在线视频 | 国产精品嫩草69影院 | av大全在线 | 一个色的综合 | 蜜臀久久久久 | 男人天堂一区 | 午夜视频网站 | 国产精品麻豆免费版 | 欧美精品久久久久久久久久 | 国产欧美综合一区 | 一区二区三区在线观看 | 日本aⅴ在线观看 | 在线视频 | 一级欧美一级日韩片 | 超碰碰97 | 国产无遮挡又黄又爽又色 | 婷久久| 嫩草一区二区三区 | 国产精品国产一区二区三区四区 | 久久精品国产成人av | 亚洲精品高潮 | 久久国产精品免费 | 老鸭窝久久 | 大地资源高清播放在线观看免费 | 2018天天操|