|
1
|
|
|
<?php namespace Comodojo\RpcClient; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\RpcClient\Processor\JsonProcessor; |
|
4
|
|
|
use \Comodojo\RpcClient\Processor\XmlProcessor; |
|
5
|
|
|
use \Comodojo\RpcClient\Components\Transport; |
|
6
|
|
|
use \Comodojo\RpcClient\Components\Protocol; |
|
7
|
|
|
use \Comodojo\RpcClient\Components\Encryption; |
|
8
|
|
|
use \Comodojo\RpcClient\Components\Encoding; |
|
9
|
|
|
use \Comodojo\RpcClient\Components\RequestManager; |
|
10
|
|
|
use \Comodojo\RpcClient\Utils\NullLogger; |
|
11
|
|
|
use \Comodojo\Exception\RpcException; |
|
12
|
|
|
use \Comodojo\Exception\HttpException; |
|
13
|
|
|
use \Comodojo\Exception\XmlrpcException; |
|
14
|
|
|
use \Exception; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Comodojo RPC client. It's able to talk in XML and JSON (2.0). |
|
18
|
|
|
* |
|
19
|
|
|
* It optionally supports a not standard encrypted transport |
|
20
|
|
|
* |
|
21
|
|
|
* @package Comodojo Spare Parts |
|
22
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
23
|
|
|
* @license MIT |
|
24
|
|
|
* |
|
25
|
|
|
* LICENSE: |
|
26
|
|
|
* |
|
27
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
28
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
29
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
30
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
31
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
32
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
33
|
|
|
* THE SOFTWARE. |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
class RpcClient { |
|
37
|
|
|
|
|
38
|
|
|
use Protocol; |
|
39
|
|
|
use Encryption; |
|
40
|
|
|
use Encoding; |
|
41
|
|
|
|
|
42
|
|
|
const JSONRPC = "JSON"; |
|
43
|
|
|
|
|
44
|
|
|
const XMLRPC = "XML"; |
|
45
|
|
|
|
|
46
|
|
|
// internals |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Autoclean requests |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private $autoclean = true; |
|
54
|
|
|
|
|
55
|
|
|
private $transport; |
|
56
|
|
|
|
|
57
|
|
|
private $logger; |
|
58
|
|
|
|
|
59
|
|
|
private $request; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Class constructor |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $server Remote RPC server address |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \Comodojo\Exception\HttpException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function __construct($server, Logger $logger = null) { |
|
69
|
|
|
|
|
70
|
|
|
if ( empty($server) ) throw new Exception("Invalid RPC server address"); |
|
71
|
|
|
|
|
72
|
|
|
$this->logger = is_null($logger) ? new NullLogger() : $logger; |
|
73
|
|
|
|
|
74
|
|
|
$this->request = new RequestManager(); |
|
75
|
|
|
|
|
76
|
|
|
try { |
|
77
|
|
|
|
|
78
|
|
|
$this->transport = new Transport($server); |
|
79
|
|
|
|
|
80
|
|
|
} catch (HttpException $he) { |
|
81
|
|
|
|
|
82
|
|
|
throw $he; |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
final public function logger() { |
|
89
|
|
|
|
|
90
|
|
|
return $this->logger; |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
final public function transport() { |
|
95
|
|
|
|
|
96
|
|
|
return $this->transport; |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
final public function request() { |
|
101
|
|
|
|
|
102
|
|
|
return $this->request; |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Set autoclean on/off |
|
108
|
|
|
* |
|
109
|
|
|
* @param bool $mode If true, requests will be removed from queue at each send() |
|
110
|
|
|
* |
|
111
|
|
|
* @return \Comodojo\RpcClient\RpcClient |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setAutoclean($mode = true) { |
|
114
|
|
|
|
|
115
|
|
|
$this->autoclean = filter_var($mode, FILTER_VALIDATE_BOOLEAN); |
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function getAutoclean() { |
|
122
|
|
|
|
|
123
|
|
|
return $this->autoclean; |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function addRequest(RpcRequest $request) { |
|
128
|
|
|
|
|
129
|
|
|
$this->request->add($request); |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Send request(s) to server |
|
137
|
|
|
* |
|
138
|
|
|
* @return mixed |
|
139
|
|
|
* |
|
140
|
|
|
* @throws \Comodojo\Exception\RpcException |
|
141
|
|
|
* @throws \Comodojo\Exception\HttpException |
|
142
|
|
|
* @throws \Comodojo\Exception\XmlrpcException |
|
143
|
|
|
* @throws \Exception |
|
144
|
|
|
*/ |
|
145
|
|
|
public function send() { |
|
146
|
|
|
|
|
147
|
|
|
$requests = $this->request->get(); |
|
148
|
|
|
|
|
149
|
|
|
if ( empty($requests) ) throw new Exception("No request to send"); |
|
150
|
|
|
|
|
151
|
|
|
if ( $this->getProtocol() == self::XMLRPC ) { |
|
152
|
|
|
|
|
153
|
|
|
$processor = new XmlProcessor($this->getEncoding(), $this->logger()); |
|
154
|
|
|
|
|
155
|
|
|
$content_type = "text/xml"; |
|
156
|
|
|
|
|
157
|
|
|
} else { |
|
158
|
|
|
|
|
159
|
|
|
$processor = new JsonProcessor($this->getEncoding(), $this->logger()); |
|
160
|
|
|
|
|
161
|
|
|
$content_type = "application/json"; |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
try { |
|
166
|
|
|
|
|
167
|
|
|
$payload = $processor->encode($requests); |
|
168
|
|
|
|
|
169
|
|
|
$response = $this->transport()->performCall($this->logger, $payload, $content_type, $this->getEncryption()); |
|
170
|
|
|
|
|
171
|
|
|
$result = $processor->decode($response); |
|
172
|
|
|
|
|
173
|
|
|
} catch (HttpException $he) { |
|
174
|
|
|
|
|
175
|
|
|
throw $he; |
|
176
|
|
|
|
|
177
|
|
|
} catch (RpcException $re) { |
|
178
|
|
|
|
|
179
|
|
|
throw $re; |
|
180
|
|
|
|
|
181
|
|
|
} catch (XmlrpcException $xe) { |
|
182
|
|
|
|
|
183
|
|
|
throw $xe; |
|
184
|
|
|
|
|
185
|
|
|
} catch (Exception $e) { |
|
186
|
|
|
|
|
187
|
|
|
throw $e; |
|
188
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
if ( $this->autoclean ) $this->request->clean(); |
|
192
|
|
|
|
|
193
|
|
|
return $result; |
|
194
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|