mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-23 08:35:35 +08:00
refactor all base things
This commit is contained in:
23
src/ZM/Context/Context.php
Normal file
23
src/ZM/Context/Context.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Context;
|
||||
|
||||
use ZM\Context\Trait\HttpTrait;
|
||||
|
||||
/**
|
||||
* 下面是机器人类的方法
|
||||
* @method reply($message) 快速回复消息
|
||||
* @method action(string $action, array $params = []) 执行动作
|
||||
* @method getArgument(string $name) 获取BotCommand的参数
|
||||
* @method getRawArguments() 获取裸的参数
|
||||
* @method getBotEvent(bool $array = false) 获取事件原对象
|
||||
* @method getBotSelf() 获取机器人自身的信息
|
||||
*/
|
||||
class Context implements ContextInterface
|
||||
{
|
||||
use HttpTrait;
|
||||
|
||||
// TODO:完善上下文的方法们
|
||||
}
|
||||
30
src/ZM/Context/ContextInterface.php
Normal file
30
src/ZM/Context/ContextInterface.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Context;
|
||||
|
||||
use OneBot\Driver\Event\Http\HttpRequestEvent;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
interface ContextInterface
|
||||
{
|
||||
/**
|
||||
* 获取 Http Request 请求对象
|
||||
*/
|
||||
public function getRequest(): ServerRequestInterface;
|
||||
|
||||
/**
|
||||
* 获取 Http 请求事件对象
|
||||
*/
|
||||
public function getHttpRequestEvent(): HttpRequestEvent;
|
||||
|
||||
/**
|
||||
* 使用 Response 对象响应 Http 请求
|
||||
* Wrapper of HttpRequestEvent::withResponse method
|
||||
*
|
||||
* @param ResponseInterface $response 响应对象
|
||||
*/
|
||||
public function withResponse(ResponseInterface $response);
|
||||
}
|
||||
41
src/ZM/Context/Trait/HttpTrait.php
Normal file
41
src/ZM/Context/Trait/HttpTrait.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Context\Trait;
|
||||
|
||||
use OneBot\Driver\Event\Http\HttpRequestEvent;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use ZM\Exception\ZMKnownException;
|
||||
|
||||
trait HttpTrait
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getRequest(): ServerRequestInterface
|
||||
{
|
||||
return container()->get('http.request');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getHttpRequestEvent(): HttpRequestEvent
|
||||
{
|
||||
$obj = container()->get('http.request.event');
|
||||
if (!$obj instanceof HttpRequestEvent) {
|
||||
throw new ZMKnownException('E00099', 'current context container event is not HttpRequestEvent');
|
||||
}
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function withResponse(ResponseInterface $response)
|
||||
{
|
||||
$this->getHttpRequestEvent()->withResponse($response);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user