getContext()->getInput()->getHeader('x-rpc') && isset($arguments[0])) { $this->parseHttpArgument($arguments); $this->runMethod(); } else { $this->outputJson([], 400); } } /** * 解析RPC参数 * * @param $arguments * @throws Exception */ protected function parseHttpArgument($arguments) { if ($this->isRpc) { $unPackArgs = (array)getInstance()->pack->unPack($arguments[0]); if (!isset($unPackArgs['handler'])) { throw new Exception('Rpc argument of handler not set.'); } if (!isset($unPackArgs['method'])) { throw new Exception('Rpc argument of method not set.'); } if (!isset($unPackArgs['args'])) { throw new Exception('Rpc argument of args not set.'); } $this->version = $unPackArgs['version'] ?? null; $this->handler = $unPackArgs['handler']; $this->method = $unPackArgs['method']; $this->reqParams = (array)$unPackArgs['args']; $this->construct = (array)$unPackArgs['construct']; $this->rpcTime = $unPackArgs['time']; } } /** * 执行 * * @throws Exception */ protected function runMethod() { $handlerClass = '\\App\\Models\\Handlers\\' . $this->handler; $handlerInstance = $this->getObject($handlerClass, ...$this->construct); if (!method_exists($handlerInstance, $this->method)) { throw new Exception('Rpc method not found.'); } $response = $handlerInstance->{$this->method}(...$this->reqParams); $this->getContext()->getLog()->pushLog('Rpc', [$handlerClass => $this->method]); $this->outputJson($response); } }