Skip to content

Commit 03afe49

Browse files
committed
Simplified the translator from an object to a callable
1 parent 9cf8cb6 commit 03afe49

File tree

11 files changed

+86
-309
lines changed

11 files changed

+86
-309
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"autoload-dev": {
2828
"psr-4": {
29+
"Datto\\JsonRpc\\Examples\\": "examples",
2930
"Datto\\JsonRpc\\Tests\\": "tests"
3031
}
3132
}

tests/Example/Stateful/Math.php renamed to examples/Application/Math.php

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,26 @@
2222
* @copyright 2015 Datto, Inc.
2323
*/
2424

25-
namespace Datto\JsonRpc\Tests\Example\Stateful;
25+
namespace Datto\JsonRpc\Examples\Application;
2626

2727
class Math
2828
{
29-
public function subtract()
30-
{
31-
$arguments = func_get_args();
32-
33-
if (count($arguments) === 1) {
34-
// Named arguments
35-
$a = @$arguments[0]['minuend'];
36-
$b = @$arguments[0]['subtrahend'];
37-
} else {
38-
// Positional arguments
39-
$a = @$arguments[0];
40-
$b = @$arguments[1];
41-
}
42-
43-
return self::sub($a, $b);
44-
}
45-
4629
/**
47-
* Returns the value $a - $b
30+
* Returns the value $a + $b.
4831
*
4932
* @param mixed $a
5033
* @param mixed $b
5134
*
5235
* @return int|null
53-
* Returns $a - $b if both $a and $b are integers
54-
* Returns null otherwise
36+
* Returns $a + $b if both $a and $b are integers.
37+
* Returns null otherwise.
5538
*/
56-
private static function sub($a, $b)
39+
public static function add($a, $b)
5740
{
58-
if (!is_int($a) || !is_int($b)) {
59-
return null;
41+
if (is_int($a) && is_int($b)) {
42+
return $a + $b;
6043
}
6144

62-
return $a - $b;
45+
return null;
6346
}
6447
}

examples/client.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
$client = new Client();
88

9-
$client->query(1, 'Math/subtract', array(5, 3));
9+
$client->query(1, 'add', array(1, 2));
1010

1111
$request = $client->encode();
1212

13-
echo $request, "\n"; // {"jsonrpc":"2.0","id":1,"method":"Math\/subtract","params":[5,3]}
13+
echo $request, "\n";
14+
// {"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]}

examples/server.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
require_once __DIR__ . '/../vendor/autoload.php';
44

5-
use Datto\JsonRpc\Tests\Example\Stateless\Translator;
65
use Datto\JsonRpc\Server;
76

8-
$translator = new Translator();
9-
$server = new Server($translator);
7+
$interpreter = function ($method) {
8+
// Convert a JSON-RPC string method name into an actual callable method
9+
return array('\\Datto\\JsonRpc\\Examples\\Application\\Math', $method);
10+
};
1011

11-
$request = '{"jsonrpc":"2.0","id":1,"method":"Math\/subtract","params":[5,3]}';
12+
$server = new Server($interpreter);
1213

13-
$reply = $server->reply($request);
14+
$reply = $server->reply('{"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]}');
1415

15-
echo $reply, "\n"; // {"jsonrpc":"2.0","id":1,"result":2}
16+
echo $reply, "\n";
17+
// {"jsonrpc":"2.0","id":1,"result":3}

src/Server.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ class Server
3535
{
3636
const VERSION = '2.0';
3737

38-
/** @var Translator */
39-
private $translator;
38+
/** @var callable */
39+
private $interpreter;
4040

4141
/**
42-
* @param Translator $translator
42+
* @param callable $interpreter
4343
*/
44-
public function __construct(Translator $translator)
44+
public function __construct(callable $interpreter)
4545
{
46-
$this->translator = $translator;
46+
$this->interpreter = $interpreter;
4747
}
4848

4949
/**
@@ -208,7 +208,7 @@ private function processRequest($request)
208208
*/
209209
private function processQuery($id, $method, $arguments)
210210
{
211-
$callable = $this->translator->getCallable($method);
211+
$callable = $this->getCallable($method);
212212

213213
if (!is_callable($callable)) {
214214
return self::errorMethod($id);
@@ -228,20 +228,36 @@ private function processQuery($id, $method, $arguments)
228228
* Processes a notification. No response is necessary.
229229
*
230230
* @param string $method
231-
* String value representing a method to invoke on the server
231+
* String value representing a method to invoke on the server.
232232
*
233233
* @param array $arguments
234234
* Array of arguments that will be passed to the method.
235235
*/
236236
private function processNotification($method, $arguments)
237237
{
238-
$callable = $this->translator->getCallable($method);
238+
$callable = $this->getCallable($method);
239239

240240
if (is_callable($callable)) {
241241
self::run($callable, $arguments);
242242
}
243243
}
244244

245+
/**
246+
* Converts a string method name to an actual callable, by asking
247+
* the interpreter to translate.
248+
*
249+
* @param string $method
250+
* String value representing the method to invoke on the server.
251+
*
252+
* @return callable|null
253+
* Returns the callable that corresponds to the string name.
254+
* Returns null if there is no equivalent callable.
255+
*/
256+
private function getCallable($method)
257+
{
258+
return call_user_func($this->interpreter, $method);
259+
}
260+
245261
/**
246262
* Executes a callable with the supplied arguments, and returns the result.
247263
*

src/Translator.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

tests/ClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testNotification()
3333
$client = new Client();
3434
$client->notify('subtract', array(3, 2));
3535

36-
$this->compare($client, '{"jsonrpc":"2.0","method":"subtract","params":[3,2]}');
36+
$this->compare($client, '{"jsonrpc":"2.0","method":"subtract","params":[3,2]}');
3737
}
3838

3939
public function testQuery()

tests/Example/Stateful/Translator.php

Lines changed: 0 additions & 69 deletions
This file was deleted.

tests/Example/Stateless/Math.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)