This commit is contained in:
2021-11-29 15:43:54 +08:00
parent 62e0eabe4f
commit c6973dbe83
+29 -11
View File
@@ -4,21 +4,25 @@ namespace Kiri\Rpc;
use Annotation\Annotation; use Annotation\Annotation;
use Annotation\Inject; use Annotation\Inject;
use Http\Constrict\RequestInterface;
use Http\Handler\Router; use Http\Handler\Router;
use Http\Message\ServerRequest;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Consul\Agent; use Kiri\Consul\Agent;
use Psr\Container\ContainerExceptionInterface; use Kiri\Context;
use Psr\Container\ContainerInterface;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Kiri; use Kiri\Kiri;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use Server\Events\OnBeforeShutdown; use Psr\Http\Message\ServerRequestInterface;
use Server\Events\OnStart;
use Server\Contract\OnCloseInterface; use Server\Contract\OnCloseInterface;
use Server\Contract\OnConnectInterface; use Server\Contract\OnConnectInterface;
use Server\Contract\OnReceiveInterface; use Server\Contract\OnReceiveInterface;
use Server\Events\OnBeforeShutdown;
use Server\Events\OnStart;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Coroutine\Channel; use Swoole\Coroutine\Channel;
use Swoole\Server; use Swoole\Server;
@@ -181,7 +185,9 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
if (is_null($handler)) { if (is_null($handler)) {
throw new \Exception('Method not found', -32601); throw new \Exception('Method not found', -32601);
} else { } else {
return $this->handler($handler, $data); $PsrRequest = Context::setContext(RequestInterface::class, $this->createServerRequest($params));
return $this->handler($handler, $PsrRequest);
} }
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
$code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode(); $code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode();
@@ -190,19 +196,31 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
} }
/**
* @param $params
* @return ServerRequestInterface
*/
private function createServerRequest($params): ServerRequestInterface
{
return (new ServerRequest())->withParsedBody($params);
}
/** /**
* @param array $handler * @param array $handler
* @param $data * @param $request
* @return array * @return array
* @throws \ReflectionException * @throws \ReflectionException
*/ */
private function handler(array $handler, $data): array private function handler(array $handler, $request): array
{ {
$controller = Kiri::getDi()->get($handler[0]); $controller = Kiri::getDi()->get($handler[0]);
$dispatcher = $controller->{$handler[1]}($request);
$dispatcher = $controller->{$handler[1]}(...$data['params']); return [
'jsonrpc' => '2.0',
return ['jsonrpc' => '2.0', 'result' => $dispatcher, 'id' => $data['id'] ?? null]; 'result' => $dispatcher,
'id' => $data['id'] ?? null
];
} }