改名
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
+10
-21
@@ -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];
|
||||
}
|
||||
|
||||
|
||||
+29
-3
@@ -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];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user