|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpJsonRpc; |
|
4
|
|
|
|
|
5
|
|
|
use PhpJsonRpc\Server\Processor; |
|
6
|
|
|
use PhpJsonRpc\Server\RequestParser; |
|
7
|
|
|
use PhpJsonRpc\Server\ResponseBuilder; |
|
8
|
|
|
use PhpJsonRpc\Server\MapperInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Implementation of JSON-RPC2 specification |
|
12
|
|
|
* |
|
13
|
|
|
* @link http://www.jsonrpc.org/specification |
|
14
|
|
|
*/ |
|
15
|
|
|
class Server |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $payload; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Processor |
|
24
|
|
|
*/ |
|
25
|
|
|
private $processor; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var RequestParser |
|
29
|
|
|
*/ |
|
30
|
|
|
private $requestParser; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ResponseBuilder |
|
34
|
|
|
*/ |
|
35
|
|
|
private $responseBuilder; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Server constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $payload |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(string $payload = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->payload = $payload ?? file_get_contents('php://input'); |
|
45
|
|
|
$this->processor = new Processor(); |
|
46
|
|
|
|
|
47
|
|
|
$this->requestParser = new RequestParser(); |
|
48
|
|
|
$this->responseBuilder = new ResponseBuilder(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return RequestParser |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getRequestParser(): RequestParser |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->requestParser; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return ResponseBuilder |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getResponseBuilder(): ResponseBuilder |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->responseBuilder; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Add handler-object for handling request |
|
69
|
|
|
* |
|
70
|
|
|
* @param mixed $object |
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
|
|
public function addHandler($object) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->processor->addHandler($object); |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Add mapper-object for mapping request-method on class and method |
|
81
|
|
|
* |
|
82
|
|
|
* @param MapperInterface $mapper |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
public function addMapper(MapperInterface $mapper) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->processor->addMapper($mapper); |
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Run server |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function execute(): string |
|
97
|
|
|
{ |
|
98
|
|
|
$calls = $this->requestParser->parse($this->payload); |
|
99
|
|
|
$result = $this->processor->process($calls); |
|
100
|
|
|
|
|
101
|
|
|
return $this->responseBuilder->build($result); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|