From 2ad380973693a7ca8e6b93576f9a10d4c08d7c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Wed, 2 Mar 2022 16:27:15 +0800 Subject: [PATCH] modify plugin name --- Annotation/JsonRpc.php | 10 ++++++---- RpcJsonp.php | 25 ++++++++++++++++++------- RpcManager.php | 18 ++++-------------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/Annotation/JsonRpc.php b/Annotation/JsonRpc.php index d525ca3..2911ae0 100644 --- a/Annotation/JsonRpc.php +++ b/Annotation/JsonRpc.php @@ -2,12 +2,13 @@ namespace Kiri\Rpc\Annotation; +use Kiri; use Kiri\Abstracts\Config; +use Kiri\Annotation\AbstractAttribute; use Kiri\Core\Network; use Kiri\Exception\ConfigException; -use Kiri; +use Kiri\Message\Handler\Router; use Kiri\Rpc\RpcManager; -use Kiri\Annotation\AbstractAttribute; use ReflectionException; #[\Attribute(\Attribute::TARGET_CLASS)] class JsonRpc extends AbstractAttribute @@ -35,12 +36,13 @@ use ReflectionException; * @param mixed $class * @param mixed|string $method * @return mixed - * @throws ReflectionException * @throws ConfigException */ public function execute(mixed $class, mixed $method = ''): bool { - return Kiri::getDi()->get(RpcManager::class)->add($this->service, $class, $this->create()); + $manager = Kiri::getDi()->get(RpcManager::class); + + return $manager->add($this->service, $class, $this->create()); } diff --git a/RpcJsonp.php b/RpcJsonp.php index cc00af1..33be6ec 100644 --- a/RpcJsonp.php +++ b/RpcJsonp.php @@ -11,8 +11,10 @@ use Kiri\Consul\Agent; use Kiri\Context; use Kiri\Exception\ConfigException; use Kiri\Message\Constrict\RequestInterface; +use Kiri\Message\Handler\DataGrip; use Kiri\Message\Handler\Handler; use Kiri\Message\Handler\Router; +use Kiri\Message\Handler\RouterCollector; use Kiri\Message\ServerRequest; use Kiri\Server\Contract\OnCloseInterface; use Kiri\Server\Contract\OnConnectInterface; @@ -31,7 +33,6 @@ use Swoole\Coroutine\Channel; use Swoole\Server; use Swoole\Timer; - /** * */ @@ -47,12 +48,15 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa public Annotation $annotation; - private RpcManager $manager; + public RpcManager $manager; private int $timerId; + public RouterCollector $collector; + + /** * @return void * @throws ContainerExceptionInterface @@ -71,6 +75,8 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa $provider->on(OnServerBeforeStart::class, [$this, 'register']); $this->manager = Kiri::getDi()->get(RpcManager::class); + + $this->collector = $this->container->get(DataGrip::class)->get('rpc'); } @@ -212,13 +218,15 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa private function dispatch($data): array { try { - [$handler, $params] = $this->getContainer()->get(RpcManager::class)->get($data['service'], $data['method']); - if (is_null($handler)) { + $handler = $this->collector->find($data['service'], 'GET'); + if (is_integer($handler) || is_null($handler)) { throw new \Exception('Method not found', -32601); } else { + $params = $this->container->getMethodParameters($handler->callback::class, $data['method']); + Context::setContext(RequestInterface::class, $this->createServerRequest($params)); - return $this->handler($handler); + return $this->handler($handler, $data['method'], $params); } } catch (\Throwable $throwable) { $code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode(); @@ -240,13 +248,16 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa /** * @param Handler $handler + * @param string $method + * @param $params * @return array */ - private function handler(Handler $handler): array + private function handler(Handler $handler, string $method, $params): array { + $result = call_user_func([$handler->callback, $method], ...$params); return [ 'jsonrpc' => '2.0', - 'result' => call_user_func($handler->callback, ...$handler->params), + 'result' => $result, 'id' => $data['id'] ?? null ]; } diff --git a/RpcManager.php b/RpcManager.php index a4f9e15..3f871ed 100644 --- a/RpcManager.php +++ b/RpcManager.php @@ -8,8 +8,7 @@ use Kiri\Abstracts\Component; use Kiri\Annotation\Inject; use Kiri\Consul\Agent; use Kiri\Consul\Health; -use Kiri\Message\Handler\Handler; -use ReflectionException; +use Kiri\Message\Handler\Router; class RpcManager extends Component { @@ -84,24 +83,15 @@ class RpcManager extends Component * @param string $class * @param array $serviceConfig * @return bool - * @throws ReflectionException */ public function add(string $name, string $class, array $serviceConfig): bool { - $methods = Kiri::getDi()->getReflect($class); - $lists = $methods->getMethods(\ReflectionMethod::IS_PUBLIC); - if (!isset($this->_rpc[$name])) { $this->_rpc[$name] = ['methods' => [], 'id' => $serviceConfig['ID'], 'config' => $serviceConfig]; } - - foreach ($lists as $reflection) { - if ($reflection->getDeclaringClass() != $class) { - continue; - } - $methodName = $reflection->getName(); - $this->_rpc[$name]['methods'][$methodName] = [new Handler('/', [$class, $methodName]), null]; - } + Router::addServer('rpc', static function () use ($name, $class) { + Router::get($name, $class); + }); return true; }