From b951011f5d9a0ca5dcb3f5d16d1029f6fa44fdb1 Mon Sep 17 00:00:00 2001 From: shady Date: Sat, 15 Mar 2014 15:53:43 +0100 Subject: [PATCH 1/2] Return an array instead of a StdClass object --- Controller/JsonRpcController.php | 38 ++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/Controller/JsonRpcController.php b/Controller/JsonRpcController.php index 609870e..56adc42 100755 --- a/Controller/JsonRpcController.php +++ b/Controller/JsonRpcController.php @@ -71,25 +71,30 @@ public function __construct($container, $config) public function execute(Request $httprequest) { $json = $httprequest->getContent(); - $request = json_decode($json); + $request = json_decode($json, true); + if ($request === null) { return $this->getErrorResponse(self::PARSE_ERROR, null); - } elseif (!(isset($request->jsonrpc) && isset($request->method) && $request->jsonrpc == '2.0')) { + } elseif (!(isset($request['jsonrpc']) && isset($request['method']) && $request['jsonrpc'] == '2.0')) { return $this->getErrorResponse(self::INVALID_REQUEST, $request->id); } - if (!in_array($request->method, array_keys($this->config['functions']))) { + + if (!in_array($request['method'], array_keys($this->config['functions']))) { return $this->getErrorResponse(self::METHOD_NOT_FOUND, $request->id); } - $service = $this->container->get($this->config['functions'][$request->method]['service']); - $method = $this->config['functions'][$request->method]['method']; - $params = (isset($request->params) ? $request->params : array()); + + $service = $this->container->get($this->config['functions'][$request['method']]['service']); + $method = $this->config['functions'][$request['method']]['method']; + $params = (isset($request['params']) ? $request['params'] : array()); + if (is_callable(array($service, $method))) { $r = new \ReflectionMethod($service, $method); + if (is_array($params)) { if (!(count($params) >= $r->getNumberOfRequiredParameters() && count($params) <= $r->getNumberOfParameters()) ) { - return $this->getErrorResponse(self::INVALID_PARAMS, $request->id, + return $this->getErrorResponse(self::INVALID_PARAMS, $request['id'], sprintf('Number of given parameters (%d) does not match the number of expected parameters (%d required, %d total)', count($params), $r->getNumberOfRequiredParameters(), $r->getNumberOfParameters())); } @@ -100,7 +105,7 @@ public function execute(Request $httprequest) /* @var \ReflectionParameter $rp */ $name = $rp->name; if (!isset($params->$name) && !$rp->isOptional()) { - return $this->getErrorResponse(self::INVALID_PARAMS, $request->id, + return $this->getErrorResponse(self::INVALID_PARAMS, $request['id'], sprintf('Parameter %s is missing', $name)); } if (isset($params->$name)) { @@ -111,23 +116,26 @@ public function execute(Request $httprequest) } $params = $newparams; } + try { $result = call_user_func_array(array($service, $method), $params); } catch (\Exception $e) { - return $this->getErrorResponse(self::INTERNAL_ERROR, $request->id, $e->getMessage()); + return $this->getErrorResponse(self::INTERNAL_ERROR, $request['id'], $e->getMessage()); } + $response = array('jsonrpc' => '2.0'); $response['result'] = $result; - $response['id'] = (isset($request->id) ? $request->id : null); + $response['id'] = (isset($request['id']) ? $request['id'] : null); if ($this->container->has('jms_serializer')) { $response = $this->container->get('jms_serializer')->serialize($response, 'json', $this->serializationContext); } else { $response = json_encode($response); } + return new Response($response, 200, array('Content-Type' => 'application/json')); } else { - return $this->getErrorResponse(self::METHOD_NOT_FOUND, $request->id); + return $this->getErrorResponse(self::METHOD_NOT_FOUND, $request['id']); } } @@ -151,6 +159,7 @@ protected function getError($code) $message = 'Internal error'; break; } + return array('code' => $code, 'message' => $message); } @@ -158,8 +167,13 @@ protected function getErrorResponse($code, $id, $data = null) { $response = array('jsonrpc' => '2.0'); $response['error'] = $this->getError($code); - if ($data != null) $response['error']['data'] = $data; + + if ($data != null) { + $response['error']['data'] = $data; + } + $response['id'] = $id; + return new Response(json_encode($response), 200, array('Content-Type' => 'application/json')); } From dbce756267881a484f99cbd6a85fd0bd9ee3ee29 Mon Sep 17 00:00:00 2001 From: shady Date: Mon, 17 Mar 2014 11:10:26 +0100 Subject: [PATCH 2/2] fix missing refactor --- Controller/JsonRpcController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Controller/JsonRpcController.php b/Controller/JsonRpcController.php index 56adc42..3f50679 100755 --- a/Controller/JsonRpcController.php +++ b/Controller/JsonRpcController.php @@ -76,11 +76,11 @@ public function execute(Request $httprequest) if ($request === null) { return $this->getErrorResponse(self::PARSE_ERROR, null); } elseif (!(isset($request['jsonrpc']) && isset($request['method']) && $request['jsonrpc'] == '2.0')) { - return $this->getErrorResponse(self::INVALID_REQUEST, $request->id); + return $this->getErrorResponse(self::INVALID_REQUEST, $request['id']); } if (!in_array($request['method'], array_keys($this->config['functions']))) { - return $this->getErrorResponse(self::METHOD_NOT_FOUND, $request->id); + return $this->getErrorResponse(self::METHOD_NOT_FOUND, $request['id']); } $service = $this->container->get($this->config['functions'][$request['method']]['service']);