From 7e91e227b21ad6fc7651a8d90a18a80ca09b5e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Thu, 28 Oct 2021 15:14:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Annotation/JsonRpc.php | 10 ++++++---- src/JsonRpcConsumers.php | 4 ++-- src/RpcJsonp.php | 31 ++++++++++--------------------- src/RpcManager.php | 32 +++++++++++++++++++++++++++++--- 4 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/Annotation/JsonRpc.php b/src/Annotation/JsonRpc.php index d6f5e0e..6e3ba31 100644 --- a/src/Annotation/JsonRpc.php +++ b/src/Annotation/JsonRpc.php @@ -3,8 +3,8 @@ namespace Kiri\Rpc\Annotation; use Annotation\Attribute; -use Http\Handler\Router; -use Kiri\Kiri; +use Kiri\Rpc\RpcManager; +use ReflectionException; #[\Attribute(\Attribute::TARGET_CLASS)] class JsonRpc extends Attribute { @@ -13,6 +13,7 @@ use Kiri\Kiri; /** * @param string $method * @param string $version + * @param string $protocol */ public function __construct(public string $method, public string $version = '2.0', public string $protocol = 'json') { @@ -24,10 +25,11 @@ use Kiri\Kiri; * @param mixed $class * @param mixed|string $method * @return mixed + * @throws ReflectionException */ - public function execute(mixed $class, mixed $method = ''): mixed + public function execute(mixed $class, mixed $method = ''): bool { - return parent::execute($class, $method); // TODO: Change the autogenerated stub + return RpcManager::add($this->method, $class); } diff --git a/src/JsonRpcConsumers.php b/src/JsonRpcConsumers.php index 9a2dc7c..15d78a3 100644 --- a/src/JsonRpcConsumers.php +++ b/src/JsonRpcConsumers.php @@ -39,7 +39,7 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface } else { $client = $this->clientNotCoroutine($config); } - $client->send(json_encode(['jsonrpc' => $version, 'method' => $method, 'params' => $data])); + $client->send(json_encode(['jsonrpc' => $version, 'service' => $service, 'method' => $method, 'params' => $data])); $client->close(); } @@ -61,7 +61,7 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface } else { $client = $this->clientNotCoroutine($config); } - $client->send(json_encode(['jsonrpc' => $version, 'method' => $method, 'params' => $data, 'id' => $id])); + $client->send(json_encode(['jsonrpc' => $version, 'service' => $service, 'method' => $method, 'params' => $data, 'id' => $id])); $read = $client->recv(); $client->close(); return json_decode($read, true); diff --git a/src/RpcJsonp.php b/src/RpcJsonp.php index f5d9a5d..ca26f64 100644 --- a/src/RpcJsonp.php +++ b/src/RpcJsonp.php @@ -4,9 +4,7 @@ namespace Kiri\Rpc; use Annotation\Inject; use Http\Constrict\ResponseInterface; -use Http\Handler\Abstracts\HandlerManager; use Http\Handler\Dispatcher; -use Http\Handler\Handler; use Http\Handler\Router; use Http\Message\ServerRequest; use Kiri\Kiri; @@ -120,13 +118,11 @@ class RpcJsonp implements OnConnectInterface, OnReceiveInterface, OnCloseInterfa private function dispatch($data): array { try { - $handler = HandlerManager::get($data['method'], 'json-rpc'); - if (is_integer($handler)) { - throw new \Exception('Invalid Request无效请求', -32600); - } else if (is_null($handler)) { + [$handler, $params] = RpcManager::get($data['service'], $data['method']); + if (is_null($handler)) { throw new \Exception('Method not found', -32601); } else { - return $this->handler($handler, $data); + return $this->handler($handler, $params, $data); } } catch (\Throwable $throwable) { $code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode(); @@ -136,26 +132,19 @@ class RpcJsonp implements OnConnectInterface, OnReceiveInterface, OnCloseInterfa /** - * @param Handler $handler + * @param array $handler + * @param array $params * @param $data * @return array - * @throws \Exception */ - private function handler(Handler $handler, $data): array + private function handler(array $handler, array $params, $data): array { - /** @var ReflectionMethod $reflection */ - $reflection = Kiri::getDi()->getReflectMethod($handler->callback[0]::class, $handler->callback[1]); + $controller = Kiri::getDi()->get($handler[0]); - $params = []; - foreach ($reflection->getParameters() as $value) { - $params[] = $data['params'][$value->getName()] ?? null; - } - $handler->params = $params; + $params = array_merge($params, $data['params']); + + $dispatcher = $controller->{$handler[1]}(...$params); - $dispatcher = (new Dispatcher($handler, $handler->_middlewares))->handle((new ServerRequest())->withData($data['params'])); - if ($dispatcher instanceof ResponseInterface) { - $dispatcher = json_decode($dispatcher->getBody()->getContents(), true); - } return ['jsonrpc' => '2.0', 'result' => $dispatcher, 'id' => $data['id'] ?? null]; } diff --git a/src/RpcManager.php b/src/RpcManager.php index de957ca..1ecf4ee 100644 --- a/src/RpcManager.php +++ b/src/RpcManager.php @@ -2,19 +2,45 @@ namespace Kiri\Rpc; +use Kiri\Kiri; +use ReflectionException; + class RpcManager { + private static array $_rpc = []; - public static function add() + + /** + * @param string $name + * @param string $class + * @return bool + * @throws ReflectionException + */ + public static function add(string $name, string $class): bool { + $methods = Kiri::getDi()->getReflect($class); + $lists = $methods->getMethods(\ReflectionMethod::IS_PUBLIC); + if (!isset(static::$_rpc[$name])) static::$_rpc[$name] = []; + + foreach ($lists as $reflection) { + $params = Kiri::getDi()->getMethodParameters($class, $reflection->getName()); + + static::$_rpc[$name][] = [$reflection->getName(), $params]; + } + return true; } - public static function get() + /** + * @param string $name + * @param string $method + * @return mixed + */ + public static function get(string $name, string $method): array { - + return static::$_rpc[$name][$method] ?? [null, null]; } }