Skip to content

symfonyx/php-jsonrpc-server-sdk

Repository files navigation

PHP JSON-RPC server sdk

License Code size PHP Versions

Scrutinizer Code Quality Build Status Code Coverage

Travis Build Status Travis PHP versions

Latest Stable Version Packagist PHP version

Simple server SDK to convert a json-rpc request string into json-rpc response string

How to use

Sdk requires only two things :

⚠️ No dependency injection is managed in this library

Example

JSON-RPC Method

use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface; class DummyMethod implements JsonRpcMethodInterface { /**  * {@inheritdoc}  */ public function validateParams(array $paramList) : array { $violationList = []; //If case your app require a specific param for instance if (!isset($paramList['my-required-key')) { $violationList[] = [ 'path' => 'my-required-key', 'error' => 'Key is required' ] } return $violationList; } /**  * {@inheritdoc}  */ public function apply(array $paramList = null) { // Handle the request ... // Then return a result return [ 'status' => 'done', ]; // Or return null; // Or return 12345; } }

Array method resolver (simple example)

You could take example on the one used for behat tests

use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface; use Yoanm\JsonRpcServer\Domain\Model\MethodResolverInterface; class ArrayMethodResolver implements MethodResolverInterface { /** @var JsonRpcMethodInterface[] */ private $methodList = []; /**  * @param string $methodName  *  * @return JsonRpcMethodInterface|null  */ public function resolve(string $methodName) { return $this->methodList[$methodName]; } /**  * @param JsonRpcMethodInterface $method  * @param string $methodName  */ public function addMethod(JsonRpcMethodInterface $method, string $methodName) { $this->methodList[$methodName] = $method; } }

Then add your method to the resolver and create the endpoint :

use Yoanm\JsonRpcServer\App\Creator\CustomExceptionCreator; use Yoanm\JsonRpcServer\App\Creator\ResponseCreator; use Yoanm\JsonRpcServer\App\Manager\MethodManager; use Yoanm\JsonRpcServer\App\RequestHandler; use Yoanm\JsonRpcServer\App\Serialization\RequestDenormalizer; use Yoanm\JsonRpcServer\App\Serialization\ResponseNormalizer; use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint; use Yoanm\JsonRpcServer\Infra\Serialization\RawRequestSerializer; use Yoanm\JsonRpcServer\Infra\Serialization\RawResponseSerializer; $resolver = new ArrayMethodResolver(); $resolver->addMethod( 'dummy-method' new DummyMethod() ); $responseCreator = new ResponseCreator(); $endpoint = new JsonRpcEndpoint( new RawRequestSerializer( new RequestDenormalizer() ), new RequestHandler( new MethodManager( $resolver, new CustomExceptionCreator() ), $responseCreator ), new RawResponseSerializer( new ResponseNormalizer() ), $responseCreator );

Once endpoint is ready, you can send it request string :

use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint; $requestString = <<<JSONRPC {  "jsonrpc": "2.0",  "id": 1  "method": "dummy-method",  "params": {  "my-required-key": "a-value"  } } JSONRPC; $responseString = $endpoint->index($requestString);

$responseString will be the following string depending of method returned value :

  • {"jsonrpc":"2.0","id":1,"result":{"status":"done"}}
  • {"jsonrpc":"2.0","id":1,"result":null}
  • {"jsonrpc":"2.0","id":1,"result":12345}

Contributing

See contributing note

About

Server SDK to convert a json-rpc request string into json-rpc response string

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 87.4%
  • Gherkin 10.7%
  • Makefile 1.9%