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('')); } }
} 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'; } // Format raw output for the tests if ($rawOutput[0] == '[') { $rawOutput = str_replace('[{', '[<br /> {', $rawOutput); $rawOutput = str_replace('},{', '},<br /> {', $rawOutput); $rawOutput = str_replace('}]', '}<br /> ]', $rawOutput);
// Bind methods $server->notify = function () { return 'notify called'; }; $server->echo = function ($what) { return $what; }; class Math { public function sum() { $args = func_get_args(); $result = 0; foreach ($args as $one) { $result += $one; } return $result; } } $server->math = new Math(); $server->{'math2.special.substract'} = function () { $args = func_get_args(); $result = array_shift($args); foreach ($args as $one) { $result -= $one; } return $result; }; // Handle! $server->handle();