This commit is contained in:
xl
2022-05-04 03:11:10 +08:00
parent 072db59b9f
commit 87d47e0971
+17 -14
View File
@@ -3,6 +3,7 @@
namespace Kiri\Rpc; namespace Kiri\Rpc;
use Exception; use Exception;
use JetBrains\PhpStorm\ArrayShape;
use Kiri; use Kiri;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
@@ -92,7 +93,7 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
public function onBeforeShutdown(OnBeforeShutdown $beforeShutdown) public function onBeforeShutdown(OnBeforeShutdown $beforeShutdown): void
{ {
$doneList = $this->manager->doneList(); $doneList = $this->manager->doneList();
$agent = $this->container->get(Agent::class); $agent = $this->container->get(Agent::class);
@@ -123,7 +124,7 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
* @param OnWorkerExit $exit * @param OnWorkerExit $exit
* @return void * @return void
*/ */
public function onWorkerExit(OnWorkerExit $exit) public function onWorkerExit(OnWorkerExit $exit): void
{ {
Timer::clear($this->timerId); Timer::clear($this->timerId);
} }
@@ -226,19 +227,19 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
try { try {
$handler = $this->collector->find($data['service'], 'GET'); $handler = $this->collector->find($data['service'], 'GET');
if (is_integer($handler) || is_null($handler)) { if (is_integer($handler) || is_null($handler)) {
throw new Exception('Method not found', -32601); throw new Exception('Handler not found', -32601);
} else { }
$controller = $handler->callback[0];
if (!method_exists($controller, $data['method'])) {
throw new Exception('Method not found', -32601);
}
$params = $this->container->getMethodParameters($controller::class, $data['method']);
Context::setContext(RequestInterface::class, $this->createServerRequest($params)); $controller = $handler->callback[0];
if (!method_exists($controller, $data['method'])) {
throw new Exception('Method not found', -32601);
}
$params = $this->container->getMethodParameters($controller::class, $data['method']);
return $this->handler($controller, $data['method'], $params); Context::setContext(RequestInterface::class, $this->createServerRequest($params));
}
} catch (\Throwable $throwable) { return $this->handler($controller, $data['method'], $params);
} catch (\Throwable $throwable) {
$code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode(); $code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode();
return $this->failure($code, jTraceEx($throwable), [], $data['id'] ?? null); return $this->failure($code, jTraceEx($throwable), [], $data['id'] ?? null);
} }
@@ -262,7 +263,8 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
* @param $params * @param $params
* @return array * @return array
*/ */
private function handler($controller, string $method, $params): array #[ArrayShape([])]
private function handler($controller, string $method, $params): array
{ {
$result = call_user_func([$controller, $method], ...$params); $result = call_user_func([$controller, $method], ...$params);
return [ return [
@@ -280,6 +282,7 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
* @param null $id * @param null $id
* @return array * @return array
*/ */
#[ArrayShape([])]
protected function failure($code, $message, array $data = [], $id = null): array protected function failure($code, $message, array $data = [], $id = null): array
{ {
$error = [ $error = [