Skip to content

Commit 796b67c

Browse files
author
wasinger
committed
Added JsonRpcController::addMethod() for adding functions after instanciation
1 parent 35b9307 commit 796b67c

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

Controller/JsonRpcController.php

+33
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,39 @@ public function execute(Request $httprequest)
140140
}
141141
}
142142

143+
/**
144+
* Add a new function that can be called by RPC
145+
*
146+
* @param string $alias The function name used in the RPC call
147+
* @param string $service The service name of the method to call
148+
* @param string $method The method of $service
149+
* @param bool $overwrite Whether to overwrite an existing function
150+
* @throws \InvalidArgumentException
151+
*/
152+
public function addMethod($alias, $service, $method, $overwrite = false)
153+
{
154+
if (!isset($this->config['functions'])) $this->config['functions'] = array();
155+
if (isset($this->config['functions'][$alias]) && !$overwrite) {
156+
throw new \InvalidArgumentException('JsonRpcController: The function "' . $alias . '" already exists.');
157+
}
158+
$this->config['functions'][$alias] = array(
159+
'service' => $service,
160+
'method' => $method
161+
);
162+
}
163+
164+
/**
165+
* Remove a method definition
166+
*
167+
* @param string $alias
168+
*/
169+
public function removeMethod($alias)
170+
{
171+
if (isset($this->config['functions'][$alias])) {
172+
unset($this->config['functions'][$alias]);
173+
}
174+
}
175+
143176
protected function getError($code)
144177
{
145178
$message = '';

Tests/JsonRpcControllerTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,30 @@ public function testHello()
6161
$this->assertEquals(-32602, $response['error']['code']);
6262
}
6363

64+
public function testAddMethod()
65+
{
66+
$requestdata = array(
67+
'jsonrpc' => '2.0',
68+
'id' => 'test',
69+
'method' => 'testhi',
70+
'params' => array('name' => 'Tom')
71+
);
72+
// this request will fail because there is no such method "testhi"
73+
$response = $this->makeRequest($requestdata);
74+
$this->assertArrayHasKey('error', $response);
75+
$this->assertArrayNotHasKey('result', $response);
76+
$this->assertEquals(-32601, $response['error']['code']);
77+
78+
// add the method definition for "testhi"
79+
$this->controller->addMethod('testhi', 'wa72_jsonrpc.testservice', 'hi');
80+
81+
// now the request should succeed
82+
$response = $this->makeRequest($requestdata);
83+
$this->assertArrayHasKey('result', $response);
84+
$this->assertArrayNotHasKey('error', $response);
85+
$this->assertEquals('Hi Tom!', $response['result']);
86+
}
87+
6488
private function makeRequest($requestdata)
6589
{
6690
return json_decode($this->controller->execute(

Tests/Testservice.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
namespace Wa72\JsonRpcBundle\Tests;
33

44
class Testservice {
5-
public function hello($name) {
5+
public function hello($name)
6+
{
67
return 'Hello ' . $name . '!';
78
}
9+
10+
public function hi($name)
11+
{
12+
return 'Hi ' . $name . '!';
13+
}
814
}

0 commit comments

Comments
 (0)