This commit is contained in:
xl
2022-05-04 03:11:10 +08:00
parent 072db59b9f
commit 87d47e0971
+8 -5
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,8 +227,9 @@ 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]; $controller = $handler->callback[0];
if (!method_exists($controller, $data['method'])) { if (!method_exists($controller, $data['method'])) {
throw new Exception('Method not found', -32601); throw new Exception('Method not found', -32601);
@@ -237,7 +239,6 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
Context::setContext(RequestInterface::class, $this->createServerRequest($params)); Context::setContext(RequestInterface::class, $this->createServerRequest($params));
return $this->handler($controller, $data['method'], $params); return $this->handler($controller, $data['method'], $params);
}
} catch (\Throwable $throwable) { } 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,6 +263,7 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
* @param $params * @param $params
* @return array * @return array
*/ */
#[ArrayShape([])]
private function handler($controller, string $method, $params): array private function handler($controller, string $method, $params): array
{ {
$result = call_user_func([$controller, $method], ...$params); $result = call_user_func([$controller, $method], ...$params);
@@ -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 = [