public function renderDefault() { // Get server $server = new Lightbulb\Json\Rpc2\Server(); $server->access = new AccessHandler(); $server->user = new UserHandler(); // Supress handling of output, as we are sending it differently $server->supressOutput(); // When running the request, we need to check whether there has been anything returned. If not, // then we have to send an empty text response, as Nette\...\JsonResponse throws exception // on empty responses. $response = $server->handle(); if (!empty($response) || is_array($response)) { // is_array in order to send [] $this->sendResponse(new \Nette\Application\Responses\JsonResponse($response)); } else { $this->sendResponse(new \Nette\Application\Responses\TextResponse('')); } }
public function renderDefault() { // Get server $server = new Lightbulb\Json\Rpc2\Server(); // Bind available functions $server->myTest = new MyTestHandler(); // contains mytest.* methods $server->myFunction = function ($param1, $param2) { /* ... */ }; $server->{'mytesthandler.myfunc'} = array($myObject, 'myMethod'); $server->myStaticHandler = 'MyStaticClass::theStaticFunction'; // Bind events $server->onBeforeCall[] = function ($server) { }; $server->onBeforeCall[] = function ($server) { }; $server->onSuccess[] = function ($server) { }; $server->onError[] = function ($server) { }; // Another way of in-binding the events; it does *not* remove the last one $server->onError = function ($server) { }; // Supress handling of output, as we are sending it differently $server->supressOutput(); // When running the request, we need to check whether there has been anything returned. If not, // then we have to send an empty text response, as Nette\...\JsonResponse throws exception // on empty responses. $response = $server->handle(); if (!empty($response) || is_array($response)) { // is_array in order to send [] $this->sendResponse(new \Nette\Application\Responses\JsonResponse($response)); } else { $this->sendResponse(new \Nette\Application\Responses\TextResponse('')); } }
}; $server->notify_sum = function () { $params = func_get_args(); $result = 0; foreach ($params as $arg) { $result += $arg; } echo 'notify_sum called, result: ' . $result; }; $server->get_data = function () { return array('hello', 5); }; /** * The test suite! */ $server->supressOutput(); $pass = $fail = $total = 0; echo '<pre>'; foreach ($requests as $key => $request) { $total++; echo '<div>--> ' . $request . '</div>'; $output = $server->handle($request); $rawOutput = $server->getRawOutput(); // Check the output $wanted = json_decode($responses[$key]); if ($wanted == $output) { $pass++; $color = '#00aa00'; } else { $fail++; $color = '#ff0000';