黄p网站_在线看中文字幕_亚洲电影免费观看_成人激情视频_欧美成视频_中国av在线

成員屬性注解

我們直接看以下示例:

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 參數,我們可以在框架的全局 onRequest 事件中進行注入,如下:

<?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 參數
            Di::getInstance()->set('param2Key', UserService::class); // param2 參數也可在 bootstrap、initialize、mainServerCreate 等事件中提前注冊。

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

            return true;
        });
    }

    public static function mainServerCreate(EventRegister $register)
    {

    }
}

Context 注解

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

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

Di 注解

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

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

Inject 注解

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

$this->param1 = new \App\Service\UserService(...$args)
主站蜘蛛池模板: 久久久久久久久99精品 | 欧美一级高潮片免费的 | 亚洲国产成人在线 | 丁香亚洲| 天天狠狠操 | 综合天天 | www久| 国产片在线观看 | 国产精品美女www爽爽爽软件 | 欧美色欧美亚洲另类七区 | 真人一级毛片 | 久久av一区二区 | 亚洲久视频 | 精品欧美久久 | 亚洲毛片| 福利午夜 | h成人在线 | 亚洲一区二区三区免费视频 | 日韩中文一区二区三区 | 日韩中文字幕三区 | 亚洲美女网站 | 久久99精品国产99久久6男男 | 国产精品欧美久久久久一区二区 | 精品日韩视频 | 亚洲精选一区二区 | 国产精品99久久久久久动医院 | 精品一区二区三区免费 | 亚洲视频成人 | 久热精品视频 | 国产激情综合五月久久 | 精品欧美一区二区三区 | 欧美一级一 | 日韩精品 电影一区 亚洲 | 一级毛片aaaaaa免费看 | 欧美精品一区二区三区视频 | 精品美女在线 | 国产精品久久久久久久久久久久冷 | 免费欧美一级 | 青青草久久爱 | 欧美精品1区2区 | 亚洲成人一区二区 |