1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package presentation |
4
|
|
|
* @subpackage processors |
5
|
|
|
* @author marius orcsik <[email protected]> |
6
|
|
|
* @date 12.08.25 |
7
|
|
|
*/ |
8
|
|
|
namespace vsc\application\processors; |
9
|
|
|
|
10
|
|
|
use vsc\domain\models\JsonRPCRequest; |
11
|
|
|
use vsc\domain\models\JsonRPCResponse; |
12
|
|
|
use vsc\infrastructure\vsc; |
13
|
|
|
use vsc\presentation\requests\HttpRequestA; |
14
|
|
|
use vsc\presentation\responses\ExceptionResponseError; |
15
|
|
|
use vsc\Exception; |
16
|
|
|
|
17
|
|
|
abstract class RPCProcessorA extends ProcessorA { |
18
|
|
|
private $oRequest; |
19
|
|
|
private $oResponse; |
20
|
|
|
protected $oInterface; |
21
|
|
|
|
22
|
|
|
public function __construct() { |
23
|
|
|
$this->oRequest = new JsonRPCRequest(vsc::getEnv()->getHttpRequest()); |
24
|
|
|
$this->oResponse = new JsonRPCResponse(); |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
public function init() { |
28
|
1 |
|
$this->oResponse->id = $this->oRequest->id; |
29
|
|
|
// if (!$oRequest->accepts('application/json')) { |
30
|
|
|
// // user-agent doesn't understand json |
31
|
|
|
// } |
32
|
1 |
|
} |
33
|
|
|
|
34
|
1 |
|
public function getRequest() { |
35
|
1 |
|
return $this->oRequest; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function getResponse() { |
39
|
1 |
|
return $this->oResponse; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
abstract public function getRPCInterface($sNameSpace = null); |
|
|
|
|
43
|
|
|
|
44
|
1 |
|
public function hasRPCMethod($oInterface, $sMethod) { |
45
|
1 |
|
if (empty ($sMethod) || empty ($oInterface)) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$oReflectedInterface = new \ReflectionObject($oInterface); |
50
|
1 |
|
if ($oReflectedInterface->hasMethod($sMethod)) { |
51
|
|
|
/* @var $oReflectedMethod \ReflectionMethod */ |
52
|
1 |
|
$oReflectedMethod = $oReflectedInterface->getMethod($sMethod); |
53
|
1 |
|
return $oReflectedMethod->isPublic(); |
54
|
|
|
} |
55
|
1 |
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function callRPCMethod() { |
59
|
1 |
|
$sRawMethod = $this->oRequest->method; |
60
|
|
|
|
61
|
1 |
|
if (strpos($sRawMethod, '.') !== false) { |
62
|
|
|
list($sNameSpace, $sMethod) = explode('.', $sRawMethod); |
63
|
|
|
} else { |
64
|
1 |
|
$sMethod = $sRawMethod; |
65
|
1 |
|
$sNameSpace = 'wp'; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$oInterface = $this->getRPCInterface($sNameSpace); |
69
|
1 |
|
if (!$this->hasRPCMethod($oInterface, $sMethod)) { |
70
|
1 |
|
$this->oResponse->error = 'Invalid RPC request: method [' . var_export($sMethod, true) . '] does not exist'; |
71
|
|
|
} else { |
72
|
|
|
if (is_null($this->oRequest->params) || !is_array($this->oRequest->params)) { |
73
|
|
|
throw new ExceptionResponseError('Invalid RPC request: missing parameters'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$oReflectedInterface = new \ReflectionObject($oInterface); |
77
|
|
|
if ($oReflectedInterface->hasMethod($sMethod)) { |
78
|
|
|
/* @var $oReflectedMethod \ReflectionMethod */ |
79
|
|
|
$oReflectedMethod = $oReflectedInterface->getMethod($sMethod); |
80
|
|
|
if ($oReflectedMethod->isPublic()) { |
81
|
|
|
return $oReflectedMethod->invokeArgs($oInterface, $this->oRequest->params); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
1 |
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function handleRequest(HttpRequestA $oHttpRequest) { |
89
|
|
|
try { |
90
|
1 |
|
$this->oResponse->result = $this->callRPCMethod(); |
91
|
|
|
} catch (ExceptionResponseError $e) { |
92
|
|
|
|
93
|
|
|
$oError = vsc::getEnv()->getHttpResponse(); |
94
|
|
|
$oError->setStatus($e->getErrorCode()); |
95
|
|
|
$this->getMap()->setResponse($oError); |
|
|
|
|
96
|
|
|
$this->oResponse->error = $e->getMessage(); |
97
|
|
|
|
98
|
|
|
} catch (Exception $e) { |
99
|
|
|
$oError = vsc::getEnv()->getHttpResponse(); |
100
|
|
|
$oError->setStatus(500); |
101
|
|
|
$this->getMap()->setResponse($oError); |
|
|
|
|
102
|
|
|
$this->oResponse->error = $e->getMessage(); |
103
|
|
|
|
104
|
|
|
} |
105
|
1 |
|
return $this->oResponse; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.