Skip to content

Commit 68011b2

Browse files
author
Sören
committed
v2.0
1 parent 6291e91 commit 68011b2

20 files changed

+726
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
composer.lock
3+
composer.phar
4+
vendor/

Makefile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Default
2+
all: deps-install
3+
4+
5+
# DEPENDENCY MANAGEMENT
6+
7+
# Updates dependencies according to lock file
8+
deps-install: composer.phar
9+
./composer.phar --no-interaction install
10+
11+
# Updates dependencies according to json file
12+
deps-update: composer.phar
13+
./composer.phar self-update
14+
./composer.phar --no-interaction update
15+
16+
17+
# TESTS AND REPORTS
18+
19+
# Code standard check
20+
cs-check: composer.lock
21+
./vendor/bin/phpcs --standard=PSR1,PSR12 --encoding=UTF-8 --report=full --colors src
22+
23+
24+
# INITIAL INSTALL
25+
26+
# Ensures composer is installed
27+
composer.phar:
28+
curl -sS https://getcomposer.org/installer | php
29+
30+
# Ensures composer is installed and dependencies loaded
31+
composer.lock: composer.phar
32+
./composer.phar --no-interaction install

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Textalk JSON-RPC 2.0
2+
3+
xJsonRPC-PHP is a JSON-RPC library for PHP featuring a client (TODO) and a server. Currently it follows the 2.0 spec of JSON-RPC, including batch calls
4+
5+
## Server
6+
7+
### Usage
8+
9+
There's currently 3 server implementations: Server, WebServer and StdInServer
10+
11+
#### Server
12+
13+
Base class, accepts a JSON string as argument to handle() that is processed as a JSON-RPC request
14+
15+
#### WebServer
16+
17+
Will attempt to read out php://input to get the JSON-RPC request, handle() should be called (without arguments) to make it start processing
18+
19+
#### StdInServer
20+
21+
Does the same as WebServer except it uses php://stdin (command line, etc.) instead of php://input
22+
23+
24+
### Implementing methods
25+
26+
27+
To implement methods you subclass one of the above servers and add methods.
28+
These methods must be public in order to be allowed for server use.
29+
30+
Example
31+
```php
32+
use \Textalk\JsonRpc\Server;
33+
34+
class ExampleServer extends Server
35+
{
36+
public function echo($echo)
37+
{
38+
return $echo;
39+
}
40+
}
41+
42+
$server = ExampleServer();
43+
$response = $server->handle();
44+
```
45+
46+
And then you can call the echo method with your preferred JSON-RPC client library by connecting to whatever url you have set-up that calls ``$server->handle()``
47+
48+
## Client
49+
50+
TODO

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "textalk/jsonrpc",
3+
"type": "library",
4+
"description": " JSON-RPC 2.0 client/server library for PHP",
5+
"keywords": ["jsonrpc"],
6+
"license": "ISC",
7+
"autoload": {
8+
"psr-4": {
9+
"": "src/"
10+
}
11+
},
12+
"require": {
13+
"php": "^7.1|^8.0"
14+
},
15+
"require-dev": {
16+
"squizlabs/php_codesniffer": "^3.5"
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Textalk\JsonRpc;
4+
5+
class ApplicationError extends Exception
6+
{
7+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Textalk\JsonRpc;
4+
5+
class BatchRequest
6+
{
7+
protected $client;
8+
protected $calls;
9+
protected $result;
10+
11+
public function __construct(WebClient $client)
12+
{
13+
$this->client = $client;
14+
$this->calls = [];
15+
}
16+
17+
public function __call($method, $args)
18+
{
19+
$reqid = count($this->calls);
20+
$this->calls[] = $this->client->assembleRequest($method, $args, $reqid);
21+
22+
return $reqid;
23+
}
24+
25+
public function __invoke()
26+
{
27+
$this->result = $this->client->sendRequest($this->calls);
28+
return $this->result;
29+
}
30+
}

src/Textalk/JsonRpc/Exception.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Textalk\JsonRpc;
4+
5+
class Exception extends \Exception
6+
{
7+
public const
8+
PARSE_ERROR = -32700,
9+
INVALID_REQUEST = -32600,
10+
METHOD_NOT_FOUND = -32601,
11+
INVALID_PARAMS = -32602,
12+
INTERNAL_ERROR = -32603;
13+
14+
protected $data;
15+
16+
public function __construct($code, $message, $data = null)
17+
{
18+
$this->data = $data;
19+
parent::__construct($message, $code);
20+
}
21+
22+
public function getDict()
23+
{
24+
$data = [
25+
'code' => $this->getCode(),
26+
'message' => $this->getMessage()
27+
];
28+
if ($this->data) {
29+
$data['data'] = $this->data;
30+
}
31+
return $data;
32+
}
33+
34+
public function getData()
35+
{
36+
return $this->data;
37+
}
38+
}

src/Textalk/JsonRpc/Helper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Textalk\JsonRpc;
4+
5+
class Helper
6+
{
7+
public static function isAssoc(array $array)
8+
{
9+
foreach (array_keys($array) as $key) {
10+
if (!is_int($key)) {
11+
return true;
12+
}
13+
}
14+
return false;
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Textalk\JsonRpc;
4+
5+
/**
6+
* Internal JSON-RPC error.
7+
*/
8+
class InternalError extends Exception
9+
{
10+
public function __construct()
11+
{
12+
parent::__construct(self::INTERNAL_ERROR, 'Internal error');
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Textalk\JsonRpc;
4+
5+
/**
6+
* Invalid method parameter(s).
7+
*/
8+
class InvalidParamsError extends Exception
9+
{
10+
public function __construct($data = null)
11+
{
12+
parent::__construct(self::INVALID_PARAMS, 'Invalid params', $data);
13+
}
14+
}

0 commit comments

Comments
 (0)