/** * @param RpcRequest $rpcRequest * * @return string */ public function getRouteName(RpcRequest $rpcRequest) { $method = $rpcRequest->getMethod(); if ($rpcRequest instanceof RpcXRequest) { $method = $rpcRequest->getService() . ':' . $method; } return $method; }
/** * @param Request $request * @param RpcRequest $rpcRequest * * @return RpcResponse|RpcXResponse */ public function dispatchAction(Request $request, RpcRequest $rpcRequest) { /** @var RequestFactory $requestFactory */ $requestFactory = $this->get('ms.rpc.request_factory'); /** @var ResponseFactory $responseFactory */ $responseFactory = $this->get('ms.rpc.response_factory'); if ($rpcRequest instanceof RpcXRequest) { $service = $rpcRequest->getService(); $method = $rpcRequest->getMethod(); } else { $method = $rpcRequest->getMethod(); list($service, $method) = explode(':', $method); } $service = $this->container->get($service); $method = new \ReflectionMethod($service, $method); $params = $rpcRequest->getParams(); $params = $requestFactory->mapParams($method, $params); try { $result = $method->invokeArgs($service, $params); /* @TODO: remove json_encode/json_decode circular reference hack */ $result = json_encode($result); $result = json_decode($result, true); } catch (\Exception $result) { } $rpcResponse = $responseFactory->createFrom($request, $rpcRequest, $result); return $rpcResponse; }
/** * Create RPC response object from HTTP response object. * * @param Request $request * @param RpcRequest $rpcRequest * @param mixed $result * * @throws RpcException * * @return RpcResponse|RpcXResponse */ public function createFrom(Request $request, RpcRequest $rpcRequest = null, $result) { $requestType = $request->headers->get('Content-Type'); $responseType = $request->headers->get('Accept'); if (!$this->validate($requestType) or !$this->validate($responseType)) { throw new RpcException('Invalid protocol or encoding'); } list($protocol, $encoding) = $this->getContentType($requestType); $rpcResponse = $this->create($protocol); if ($rpcRequest !== null) { $rpcResponse->setVersion($rpcRequest->getVersion()); $rpcResponse->setId($rpcRequest->getId()); } if ($result instanceof \Exception) { $error = new Error(); $error->setCode($result->getCode()); $error->setMessage($result->getMessage()); $error->setData($result->getTraceAsString()); $rpcResponse->setError($error); return $rpcResponse; } $rpcResponse->setResult($result); return $rpcResponse; }