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

成員屬性注解

我們直接看以下示例:

UserService

<?php
namespace App\Service;

class UserService
{
    public function info()
    {
        var_dump("this is user info");
    }
}

Index 控制器類

<?php
namespace App\HttpController;

use App\Service\UserService;
use EasySwoole\HttpAnnotation\AnnotationController;
use EasySwoole\HttpAnnotation\Attributes\Property\Context;
use EasySwoole\HttpAnnotation\Attributes\Property\Di;
use EasySwoole\HttpAnnotation\Attributes\Property\Inject;

class Index12 extends AnnotationController
{
    #[Inject(object: new UserService())]
    protected ?UserService $param1;

    #[Di(key: 'param2Key')]
    protected ?UserService $param2;

    #[Context(key: 'param3Key')]
    protected ?UserService $param3;

    protected function onRequest(?string $action): ?bool
    {
        return parent::onRequest($action);
    }

    public function test()
    {
        $this->param1->info();
        $this->param2->info();
        $this->param3->info();
    }
}

如果想正常注入 param2param3 參數(shù),我們可以在框架的全局 onRequest 事件中進(jìn)行注入,如下:

<?php

namespace EasySwoole\EasySwoole;

use App\Service\UserService;
use EasySwoole\Component\Context\ContextManager;
use EasySwoole\Component\Di;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;

class EasySwooleEvent implements Event
{
    public static function initialize()
    {
        date_default_timezone_set('Asia/Shanghai');

        Di::getInstance()->set(SysConst::HTTP_GLOBAL_ON_REQUEST, function (Request $request, Response $response): bool {
            // 提前使用 Di 注冊 param2 參數(shù)
            Di::getInstance()->set('param2Key', UserService::class); // param2 參數(shù)也可在 bootstrap、initialize、mainServerCreate 等事件中提前注冊。

            // 提前使用 ContextManager 注冊 param3 參數(shù)
            ContextManager::getInstance()->set('param3Key', new UserService()); // param3 參數(shù)只可在全局 onRequest 事件中提前注冊。

            return true;
        });
    }

    public static function mainServerCreate(EventRegister $register)
    {

    }
}

Context 注解

Context 注解,完整命名空間是 \EasySwoole\HttpAnnotation\Attributes\Property\Context,用于在每次請求進(jìn)來的時候,從上下文管理器中取數(shù)據(jù),并賦值到對應(yīng)的屬性中,以上等價于:

$this->param3 = \EasySwoole\Component\ContextManager::getInstance()->get('param3Key');

Di 注解

Di 注解,完整命名空間是 \EasySwoole\HttpAnnotation\Attributes\Property\Di,用于在每次請求進(jìn)來的時候,從 IOC 中取數(shù)據(jù),并賦值到對應(yīng)的屬性中,以上等價于:

$this->param2 = \EasySwoole\Component\Di::getInstance()->get('param2Key');

Inject 注解

Inject注解,完整命令空間是 \EasySwoole\HttpAnnotation\Attributes\Property\Inject,可注入類并且傳入構(gòu)造函數(shù)參數(shù),以上等價于:

$this->param1 = new \App\Service\UserService(...$args)
主站蜘蛛池模板: 在线观看免费黄色 | 激情久久视频 | 九色在线观看视频 | 久久免费在线观看视频 | 国产一区二区视频在线观看 | 97国产精品 | 国产一区二区视频免费 | 伊人毛片 | 国产日韩av在线播放 | 成人在线你懂的 | 一级美女视频 | 日韩在线观看第一页 | 少妇三级 | 6699嫩草久久久精品影院 | 不卡的毛片 | 超碰96| 成人a级网站 | 在线观看国产一区 | 日韩一二三区在线观看 | 深夜久久| 日日干日日| 成人av影院 | 久久久精品免费 | 久久精品99国产精 | 色综合天天综合网国产成人网 | 激情中文网 | 中文字幕三区 | 有码一区 | 欧美1级片 | 欧美精品一线 | 欧美亚洲在线视频 | 四虎永久免费影院 | av在线播放网址 | 粉嫩av一区 | 亚洲综合成人在线 | 欧美在线一区二区三区四区 | 99精品久久久 | 精品国产乱码久久久久 | 国产精品播放 | 最新日本中文字幕 | 91成年视频 |