|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpJsonRpc\Server; |
|
4
|
|
|
|
|
5
|
|
|
use PhpJsonRpc\Common\Interceptor\Interceptor; |
|
6
|
|
|
use PhpJsonRpc\Core\Invoke; |
|
7
|
|
|
use PhpJsonRpc\Core\Invoke\AbstractInvoke; |
|
8
|
|
|
use PhpJsonRpc\Core\InvokeSpec; |
|
9
|
|
|
use PhpJsonRpc\Core\Result; |
|
10
|
|
|
use PhpJsonRpc\Core\ResultSpec; |
|
11
|
|
|
use PhpJsonRpc\Error\JsonRpcException; |
|
12
|
|
|
use PhpJsonRpc\Error\MethodNotFoundException; |
|
13
|
|
|
use PhpJsonRpc\Server\Processor\ProcessorContainer; |
|
14
|
|
|
|
|
15
|
|
|
class Processor |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $handlers = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var MapperInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $mapper; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Invoker |
|
29
|
|
|
*/ |
|
30
|
|
|
private $invoker; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Interceptor |
|
34
|
|
|
*/ |
|
35
|
|
|
private $preProcess; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Processor constructor. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->mapper = new Mapper(); |
|
43
|
|
|
$this->invoker = new Invoker(); |
|
44
|
|
|
$this->preProcess = Interceptor::createBase(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param mixed $object |
|
49
|
|
|
*/ |
|
50
|
|
|
public function addHandler($object) |
|
51
|
|
|
{ |
|
52
|
|
|
if (!is_object($object)) { |
|
53
|
|
|
throw new \DomainException('Expected object'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$key = get_class($object); |
|
57
|
|
|
$this->handlers[$key] = $object; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param MapperInterface $mapper |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setMapper(MapperInterface $mapper) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->mapper = $mapper; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param InvokeSpec $specifier |
|
70
|
|
|
* |
|
71
|
|
|
* @return ResultSpec |
|
72
|
|
|
*/ |
|
73
|
|
|
public function process(InvokeSpec $specifier): ResultSpec |
|
74
|
|
|
{ |
|
75
|
|
|
$resultUnits = []; |
|
76
|
|
|
$callUnits = $specifier->getUnits(); |
|
77
|
|
|
|
|
78
|
|
|
foreach ($callUnits as $unit) { |
|
79
|
|
|
$unit = $this->preProcess($unit); |
|
80
|
|
|
|
|
81
|
|
|
if ($unit instanceof Invoke\Invoke) { |
|
82
|
|
|
$resultUnits[] = $this->handleCallUnit($unit); |
|
83
|
|
|
} elseif ($unit instanceof Invoke\Notification) { |
|
84
|
|
|
$resultUnits[] = $this->handleNotificationUnit($unit); |
|
85
|
|
|
} else { |
|
86
|
|
|
$resultUnits[] = $this->handleErrorUnit($unit); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return new ResultSpec($resultUnits, $specifier->isSingleCall()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return Interceptor |
|
95
|
|
|
*/ |
|
96
|
|
|
public function onPreProcess(): Interceptor |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->preProcess; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param AbstractInvoke $invoke |
|
103
|
|
|
* |
|
104
|
|
|
* @return AbstractInvoke |
|
105
|
|
|
*/ |
|
106
|
|
View Code Duplication |
private function preProcess(AbstractInvoke $invoke): AbstractInvoke |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$result = $this->preProcess->handle(new ProcessorContainer($this, $invoke)); |
|
109
|
|
|
|
|
110
|
|
|
if ($result instanceof ProcessorContainer) { |
|
111
|
|
|
return $result->getInvoke(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
throw new \RuntimeException(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param Invoke\Invoke $unit |
|
119
|
|
|
* |
|
120
|
|
|
* @return Result\AbstractResult |
|
121
|
|
|
*/ |
|
122
|
|
|
private function handleCallUnit(Invoke\Invoke $unit): Result\AbstractResult |
|
123
|
|
|
{ |
|
124
|
|
|
try { |
|
125
|
|
|
list($class, $method) = $this->getClassAndMethod($unit->getRawMethod()); |
|
126
|
|
|
$result = $this->invoker->invoke($this->handlers[$class], $method, $unit->getRawParams()); |
|
127
|
|
|
} catch (JsonRpcException $exception) { |
|
128
|
|
|
return new Result\Error($unit->getRawId(), $exception); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return new Result\Result($unit->getRawId(), $result); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param Invoke\Notification $unit |
|
136
|
|
|
* |
|
137
|
|
|
* @return Result\AbstractResult |
|
138
|
|
|
*/ |
|
139
|
|
|
private function handleNotificationUnit(Invoke\Notification $unit): Result\AbstractResult |
|
140
|
|
|
{ |
|
141
|
|
|
try { |
|
142
|
|
|
list($class, $method) = $this->getClassAndMethod($unit->getRawMethod()); |
|
143
|
|
|
$this->invoker->invoke($this->handlers[$class], $method, $unit->getRawParams()); |
|
144
|
|
|
} catch (JsonRpcException $exception) { |
|
145
|
|
|
return new Result\Error(null, $exception); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return new Result\Notification(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param Invoke\Error $unit |
|
153
|
|
|
* |
|
154
|
|
|
* @return Result\AbstractResult |
|
155
|
|
|
*/ |
|
156
|
|
|
private function handleErrorUnit(Invoke\Error $unit): Result\AbstractResult |
|
157
|
|
|
{ |
|
158
|
|
|
return new Result\Error(null, $unit->getBaseException()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param string $requestedMethod |
|
163
|
|
|
* |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
private function getClassAndMethod(string $requestedMethod) |
|
167
|
|
|
{ |
|
168
|
|
|
list($class, $method) = $this->mapper->getClassAndMethod($requestedMethod); |
|
169
|
|
|
|
|
170
|
|
|
if ($class && array_key_exists($class, $this->handlers) && method_exists($this->handlers[$class], $method)) { |
|
171
|
|
|
return [$class, $method]; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
throw new MethodNotFoundException(); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.