Skip to content

Commit

Permalink
Merge pull request #6 from shadypierre/master
Browse files Browse the repository at this point in the history
Return an array instead of a StdClass object
  • Loading branch information
Christoph Singer committed Mar 18, 2014
2 parents f259241 + dbce756 commit 5e7e143
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions Controller/JsonRpcController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
return $this->getErrorResponse(self::INVALID_REQUEST, $request->id);
} 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']))) {
return $this->getErrorResponse(self::METHOD_NOT_FOUND, $request->id);

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()));
}
Expand All @@ -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)) {
Expand All @@ -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']);
}
}

Expand All @@ -151,15 +159,21 @@ protected function getError($code)
$message = 'Internal error';
break;
}

return array('code' => $code, 'message' => $message);
}

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'));
}

Expand Down

0 comments on commit 5e7e143

Please sign in to comment.