Skip to content

Commit

Permalink
Simplified the translator from an object to a callable
Browse files Browse the repository at this point in the history
  • Loading branch information
smortensen committed Jul 24, 2015
1 parent 9cf8cb6 commit 03afe49
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 309 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"autoload-dev": {
"psr-4": {
"Datto\\JsonRpc\\Examples\\": "examples",
"Datto\\JsonRpc\\Tests\\": "tests"
}
}
Expand Down
33 changes: 8 additions & 25 deletions tests/Example/Stateful/Math.php → examples/Application/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,26 @@
* @copyright 2015 Datto, Inc.
*/

namespace Datto\JsonRpc\Tests\Example\Stateful;
namespace Datto\JsonRpc\Examples\Application;

class Math
{
public function subtract()
{
$arguments = func_get_args();

if (count($arguments) === 1) {
// Named arguments
$a = @$arguments[0]['minuend'];
$b = @$arguments[0]['subtrahend'];
} else {
// Positional arguments
$a = @$arguments[0];
$b = @$arguments[1];
}

return self::sub($a, $b);
}

/**
* Returns the value $a - $b
* Returns the value $a + $b.
*
* @param mixed $a
* @param mixed $b
*
* @return int|null
* Returns $a - $b if both $a and $b are integers
* Returns null otherwise
* Returns $a + $b if both $a and $b are integers.
* Returns null otherwise.
*/
private static function sub($a, $b)
public static function add($a, $b)
{
if (!is_int($a) || !is_int($b)) {
return null;
if (is_int($a) && is_int($b)) {
return $a + $b;
}

return $a - $b;
return null;
}
}
5 changes: 3 additions & 2 deletions examples/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

$client = new Client();

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

$request = $client->encode();

echo $request, "\n"; // {"jsonrpc":"2.0","id":1,"method":"Math\/subtract","params":[5,3]}
echo $request, "\n";
// {"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]}
14 changes: 8 additions & 6 deletions examples/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

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

use Datto\JsonRpc\Tests\Example\Stateless\Translator;
use Datto\JsonRpc\Server;

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

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

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

echo $reply, "\n"; // {"jsonrpc":"2.0","id":1,"result":2}
echo $reply, "\n";
// {"jsonrpc":"2.0","id":1,"result":3}
32 changes: 24 additions & 8 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class Server
{
const VERSION = '2.0';

/** @var Translator */
private $translator;
/** @var callable */
private $interpreter;

/**
* @param Translator $translator
* @param callable $interpreter
*/
public function __construct(Translator $translator)
public function __construct(callable $interpreter)
{
$this->translator = $translator;
$this->interpreter = $interpreter;
}

/**
Expand Down Expand Up @@ -208,7 +208,7 @@ private function processRequest($request)
*/
private function processQuery($id, $method, $arguments)
{
$callable = $this->translator->getCallable($method);
$callable = $this->getCallable($method);

if (!is_callable($callable)) {
return self::errorMethod($id);
Expand All @@ -228,20 +228,36 @@ private function processQuery($id, $method, $arguments)
* Processes a notification. No response is necessary.
*
* @param string $method
* String value representing a method to invoke on the server
* String value representing a method to invoke on the server.
*
* @param array $arguments
* Array of arguments that will be passed to the method.
*/
private function processNotification($method, $arguments)
{
$callable = $this->translator->getCallable($method);
$callable = $this->getCallable($method);

if (is_callable($callable)) {
self::run($callable, $arguments);
}
}

/**
* Converts a string method name to an actual callable, by asking
* the interpreter to translate.
*
* @param string $method
* String value representing the method to invoke on the server.
*
* @return callable|null
* Returns the callable that corresponds to the string name.
* Returns null if there is no equivalent callable.
*/
private function getCallable($method)
{
return call_user_func($this->interpreter, $method);
}

/**
* Executes a callable with the supplied arguments, and returns the result.
*
Expand Down
51 changes: 0 additions & 51 deletions src/Translator.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testNotification()
$client = new Client();
$client->notify('subtract', array(3, 2));

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

public function testQuery()
Expand Down
69 changes: 0 additions & 69 deletions tests/Example/Stateful/Translator.php

This file was deleted.

64 changes: 0 additions & 64 deletions tests/Example/Stateless/Math.php

This file was deleted.

Loading

0 comments on commit 03afe49

Please sign in to comment.