From 796b67ce88079f7bef4dede0d923e39726360d92 Mon Sep 17 00:00:00 2001 From: wasinger Date: Wed, 23 Jul 2014 17:42:20 +0200 Subject: [PATCH] Added JsonRpcController::addMethod() for adding functions after instanciation --- Controller/JsonRpcController.php | 33 ++++++++++++++++++++++++++++++++ Tests/JsonRpcControllerTest.php | 24 +++++++++++++++++++++++ Tests/Testservice.php | 8 +++++++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/Controller/JsonRpcController.php b/Controller/JsonRpcController.php index 779fbb6..8315f13 100755 --- a/Controller/JsonRpcController.php +++ b/Controller/JsonRpcController.php @@ -140,6 +140,39 @@ public function execute(Request $httprequest) } } + /** + * Add a new function that can be called by RPC + * + * @param string $alias The function name used in the RPC call + * @param string $service The service name of the method to call + * @param string $method The method of $service + * @param bool $overwrite Whether to overwrite an existing function + * @throws \InvalidArgumentException + */ + public function addMethod($alias, $service, $method, $overwrite = false) + { + if (!isset($this->config['functions'])) $this->config['functions'] = array(); + if (isset($this->config['functions'][$alias]) && !$overwrite) { + throw new \InvalidArgumentException('JsonRpcController: The function "' . $alias . '" already exists.'); + } + $this->config['functions'][$alias] = array( + 'service' => $service, + 'method' => $method + ); + } + + /** + * Remove a method definition + * + * @param string $alias + */ + public function removeMethod($alias) + { + if (isset($this->config['functions'][$alias])) { + unset($this->config['functions'][$alias]); + } + } + protected function getError($code) { $message = ''; diff --git a/Tests/JsonRpcControllerTest.php b/Tests/JsonRpcControllerTest.php index df667f8..6240a7e 100755 --- a/Tests/JsonRpcControllerTest.php +++ b/Tests/JsonRpcControllerTest.php @@ -61,6 +61,30 @@ public function testHello() $this->assertEquals(-32602, $response['error']['code']); } + public function testAddMethod() + { + $requestdata = array( + 'jsonrpc' => '2.0', + 'id' => 'test', + 'method' => 'testhi', + 'params' => array('name' => 'Tom') + ); + // this request will fail because there is no such method "testhi" + $response = $this->makeRequest($requestdata); + $this->assertArrayHasKey('error', $response); + $this->assertArrayNotHasKey('result', $response); + $this->assertEquals(-32601, $response['error']['code']); + + // add the method definition for "testhi" + $this->controller->addMethod('testhi', 'wa72_jsonrpc.testservice', 'hi'); + + // now the request should succeed + $response = $this->makeRequest($requestdata); + $this->assertArrayHasKey('result', $response); + $this->assertArrayNotHasKey('error', $response); + $this->assertEquals('Hi Tom!', $response['result']); + } + private function makeRequest($requestdata) { return json_decode($this->controller->execute( diff --git a/Tests/Testservice.php b/Tests/Testservice.php index c1b9fc8..1e65388 100755 --- a/Tests/Testservice.php +++ b/Tests/Testservice.php @@ -2,7 +2,13 @@ namespace Wa72\JsonRpcBundle\Tests; class Testservice { - public function hello($name) { + public function hello($name) + { return 'Hello ' . $name . '!'; } + + public function hi($name) + { + return 'Hi ' . $name . '!'; + } } \ No newline at end of file