|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Freshcells\SoapClientBundle\SoapClient; |
|
4
|
|
|
|
|
5
|
|
|
use Freshcells\SoapClientBundle\Event\Events; |
|
6
|
|
|
use Freshcells\SoapClientBundle\Event\FaultEvent; |
|
7
|
|
|
use Freshcells\SoapClientBundle\Event\RequestEvent; |
|
8
|
|
|
use Freshcells\SoapClientBundle\Event\ResponseEvent; |
|
9
|
|
|
use Ramsey\Uuid\Uuid; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class SoapClient |
|
14
|
|
|
*/ |
|
15
|
|
|
class SoapClient extends \SoapClient |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $options; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var EventDispatcherInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $dispatcher; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $mockRequests = []; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private $mockResponses = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* SoapClient constructor. |
|
36
|
|
|
* @param null $wsdl |
|
37
|
|
|
* @param array|null $options |
|
38
|
|
|
*/ |
|
39
|
9 |
|
public function __construct($wsdl = null, array $options = []) |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
9 |
|
if (isset($options['mock_requests'])) { |
|
43
|
9 |
|
$this->setMockRequests($options['mock_requests']); |
|
44
|
9 |
|
unset($options['mock_requests']); |
|
45
|
|
|
} |
|
46
|
9 |
|
if (isset($options['mock_responses'])) { |
|
47
|
6 |
|
$this->setMockResponses($options['mock_responses']); |
|
48
|
6 |
|
unset($options['mock_responses']); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$defaults = array( |
|
52
|
9 |
|
'compression' => (SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP), |
|
53
|
9 |
|
'cache_wsdl' => WSDL_CACHE_BOTH, |
|
54
|
9 |
|
'connection_timeout' => 60, |
|
55
|
|
|
'exceptions' => true, |
|
56
|
9 |
|
'features' => SOAP_SINGLE_ELEMENT_ARRAYS, |
|
57
|
9 |
|
'soap_version' => SOAP_1_2, |
|
58
|
|
|
'trace' => true, |
|
59
|
9 |
|
'user_agent' => 'freshcells/soap-client-bundle', |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
9 |
|
$options = array_merge($defaults, $options); |
|
63
|
|
|
|
|
64
|
9 |
|
if (!is_null($wsdl)) { |
|
65
|
|
|
// if option 'location' not set explicit use WSDL URL as service location |
|
66
|
9 |
|
if (!isset($options['location'])) { |
|
67
|
9 |
|
$options['location'] = $this->resolveLocation($wsdl); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
9 |
|
$this->SoapClient($wsdl, $options); |
|
72
|
9 |
|
$this->options = $options; |
|
73
|
9 |
|
} |
|
74
|
|
|
|
|
75
|
9 |
|
public function __call($function_name, $arguments) |
|
76
|
|
|
{ |
|
77
|
|
|
try { |
|
78
|
9 |
|
$response = parent::__call($function_name, $arguments); |
|
79
|
|
|
//works only with 'exceptions' => false, we always throw |
|
80
|
6 |
|
if (is_soap_fault($response)) { |
|
81
|
6 |
|
throw $response; |
|
82
|
|
|
} |
|
83
|
3 |
|
} catch (\Exception $e) { |
|
84
|
3 |
|
$this->handleFault($function_name, $arguments, $e); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
6 |
|
return $response; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function __soapCall( |
|
91
|
|
|
$function_name, |
|
92
|
|
|
$arguments, |
|
93
|
|
|
$options = null, |
|
94
|
|
|
$input_headers = null, |
|
95
|
|
|
&$output_headers = null |
|
96
|
|
|
) { |
|
97
|
|
|
try { |
|
98
|
|
|
$response = parent::__soapCall($function_name, $arguments, $options, $input_headers, $output_headers); |
|
99
|
|
|
//works only with 'exceptions' => false, we always throw |
|
100
|
|
|
if (is_soap_fault($response)) { |
|
101
|
|
|
throw $response; |
|
102
|
|
|
} |
|
103
|
|
|
} catch (\Exception $e) { |
|
104
|
|
|
$this->handleFault($function_name, $arguments, $e); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $response; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $request |
|
112
|
|
|
* @param string $location |
|
113
|
|
|
* @param string $action |
|
114
|
|
|
* @param int $version |
|
115
|
|
|
* @param null $one_way |
|
116
|
|
|
* @return bool|string |
|
117
|
|
|
*/ |
|
118
|
6 |
|
public function __doRequest($request, $location, $action, $version, $one_way = null) |
|
119
|
|
|
{ |
|
120
|
6 |
|
$id = Uuid::uuid1(); |
|
121
|
|
|
|
|
122
|
6 |
|
foreach ($this->mockRequests as $key => $mockRequest) { |
|
123
|
6 |
|
if (is_string($key)) { |
|
124
|
6 |
|
if (strrpos($action, $key) !== false) { |
|
125
|
6 |
|
$request = file_get_contents($mockRequest); |
|
126
|
6 |
|
break; |
|
127
|
|
|
} |
|
128
|
|
|
} else { |
|
129
|
|
|
if (is_callable($mockRequest)) { |
|
130
|
|
|
if ($requestFilePath = $mockRequest($request, $location, $action, $version, $one_way)) { |
|
131
|
|
|
$request = file_get_contents($requestFilePath); |
|
132
|
|
|
break; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
6 |
|
$this->preCall($id->toString(), $action, $request); |
|
139
|
|
|
|
|
140
|
6 |
|
foreach ($this->mockResponses as $key => $mockResponse) { |
|
141
|
6 |
|
if (is_string($key)) { |
|
142
|
3 |
|
if (strrpos($action, $key) !== false) { |
|
143
|
3 |
|
$response = file_get_contents($mockResponse); |
|
144
|
|
|
|
|
145
|
3 |
|
$this->postCall($id->toString(), $action, $response); |
|
146
|
|
|
|
|
147
|
3 |
|
return $response; |
|
148
|
|
|
} |
|
149
|
|
|
} else { |
|
150
|
3 |
|
if (is_callable($mockResponse)) { |
|
151
|
3 |
|
if ($responseFilePath = $mockResponse($request, $location, $action, $version, $one_way)) { |
|
152
|
3 |
|
$response = file_get_contents($responseFilePath); |
|
153
|
|
|
|
|
154
|
3 |
|
$this->postCall($id->toString(), $action, $response); |
|
155
|
|
|
|
|
156
|
3 |
|
return $response; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/* workaround for working timeout */ |
|
163
|
|
|
$socketTimeout = false; |
|
164
|
|
|
if (isset($this->options['connection_timeout']) |
|
165
|
|
|
&& (int)$this->options['connection_timeout'] > (int)ini_get('default_socket_timeout') |
|
166
|
|
|
) { |
|
167
|
|
|
$socketTimeout = ini_set('default_socket_timeout', $this->options['connection_timeout']); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$response = parent::__doRequest($request, $location, $action, $version, $one_way); |
|
171
|
|
|
|
|
172
|
|
|
$this->postCall($id->toString(), $action, $response); |
|
173
|
|
|
|
|
174
|
|
|
if ($socketTimeout !== false) { |
|
175
|
|
|
ini_set('default_socket_timeout', $socketTimeout); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $response; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Triggered before a request is executed |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $id |
|
185
|
|
|
* @param string $resource |
|
186
|
|
|
* @param string $requestContent |
|
187
|
|
|
*/ |
|
188
|
6 |
|
protected function preCall(string $id, string $resource, string $requestContent = null) |
|
189
|
|
|
{ |
|
190
|
6 |
|
if (null !== $this->dispatcher) { |
|
191
|
6 |
|
$this->dispatcher->dispatch(Events::REQUEST, new RequestEvent($id, $resource, $requestContent)); |
|
192
|
|
|
} |
|
193
|
6 |
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param string $id |
|
197
|
|
|
* @param string $resource |
|
198
|
|
|
* @param string $response |
|
199
|
|
|
*/ |
|
200
|
6 |
|
protected function postCall(string $id, string $resource, string $response = null) |
|
201
|
|
|
{ |
|
202
|
6 |
|
if (null !== $this->dispatcher) { |
|
203
|
6 |
|
$responseEvent = new ResponseEvent( |
|
204
|
6 |
|
$id, |
|
205
|
6 |
|
$resource, |
|
206
|
6 |
|
$this->__getLastRequest(), |
|
207
|
6 |
|
$this->__getLastRequestHeaders(), |
|
208
|
6 |
|
$response, |
|
209
|
6 |
|
$this->__getLastResponseHeaders() |
|
210
|
|
|
); |
|
211
|
6 |
|
$this->dispatcher->dispatch(Events::RESPONSE, $responseEvent); |
|
212
|
|
|
} |
|
213
|
6 |
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param string $id |
|
217
|
|
|
* @param string $resource |
|
218
|
|
|
* @param string $requestContent |
|
219
|
|
|
* @param \Exception $exception |
|
220
|
|
|
*/ |
|
221
|
3 |
|
protected function faultCall(string $id, string $resource, string $requestContent, \Exception $exception) |
|
222
|
|
|
{ |
|
223
|
3 |
|
if (null !== $this->dispatcher) { |
|
224
|
3 |
|
$this->dispatcher->dispatch( |
|
225
|
3 |
|
Events::FAULT, |
|
226
|
3 |
|
new FaultEvent($id, $exception, new RequestEvent($id, $resource, $requestContent)) |
|
227
|
|
|
); |
|
228
|
|
|
} |
|
229
|
3 |
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param string $wsdl |
|
233
|
|
|
* @return string |
|
234
|
|
|
*/ |
|
235
|
9 |
|
protected function resolveLocation($wsdl) |
|
236
|
|
|
{ |
|
237
|
9 |
|
$wsdlUrl = parse_url($wsdl); |
|
238
|
|
|
|
|
239
|
9 |
|
return ((isset($wsdlUrl['scheme'])) ? $wsdlUrl['scheme'].'://' : '') |
|
240
|
9 |
|
.((isset($wsdlUrl['user'])) ? $wsdlUrl['user'] |
|
241
|
9 |
|
.((isset($wsdlUrl['pass'])) ? ':'.$wsdlUrl['pass'] : '').'@' : '') |
|
242
|
9 |
|
.((isset($wsdlUrl['host'])) ? $wsdlUrl['host'] : '') |
|
243
|
9 |
|
.((isset($wsdlUrl['port'])) ? ':'.$wsdlUrl['port'] : '') |
|
244
|
9 |
|
.((isset($wsdlUrl['path'])) ? $wsdlUrl['path'] : ''); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param array $mockRequests |
|
249
|
|
|
*/ |
|
250
|
9 |
|
public function setMockRequests(array $mockRequests) |
|
251
|
|
|
{ |
|
252
|
9 |
|
$this->mockRequests = $mockRequests; |
|
253
|
9 |
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param array $mockResponses |
|
257
|
|
|
*/ |
|
258
|
9 |
|
public function setMockResponses(array $mockResponses) |
|
259
|
|
|
{ |
|
260
|
9 |
|
$this->mockResponses = $mockResponses; |
|
261
|
9 |
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
265
|
|
|
*/ |
|
266
|
9 |
|
public function setDispatcher(EventDispatcherInterface $dispatcher) |
|
267
|
|
|
{ |
|
268
|
9 |
|
$this->dispatcher = $dispatcher; |
|
269
|
9 |
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @param $function_name |
|
273
|
|
|
* @param $arguments |
|
274
|
|
|
* @param $e |
|
275
|
|
|
*/ |
|
276
|
3 |
|
protected function handleFault($function_name, $arguments, $e): void |
|
277
|
|
|
{ |
|
278
|
3 |
|
$request = $this->__getLastRequest(); |
|
279
|
3 |
|
if ($request === null) { //only dispatch this when no request was fired |
|
280
|
3 |
|
$request = implode(' ', $arguments); |
|
281
|
3 |
|
$id = Uuid::uuid1(); |
|
282
|
3 |
|
$this->faultCall($id->toString(), $function_name, $request, $e); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
3 |
|
throw $e; |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|