1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Adapter\React; |
4
|
|
|
|
5
|
|
|
use React\EventLoop\LoopInterface; |
6
|
|
|
use React\Promise\Deferred; |
7
|
|
|
use React\HttpClient\Client as ReactClient; |
8
|
|
|
use React\HttpClient\Request as ReactRequest; |
9
|
|
|
use React\HttpClient\Response as ReactResponse; |
10
|
|
|
use Http\Client\HttpClient; |
11
|
|
|
use Http\Client\HttpAsyncClient; |
12
|
|
|
use Http\Client\Exception\HttpException; |
13
|
|
|
use Http\Client\Exception\RequestException; |
14
|
|
|
use Http\Message\MessageFactory; |
15
|
|
|
use Psr\Http\Message\RequestInterface; |
16
|
|
|
use Psr\Http\Message\StreamInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Client for the React promise implementation. |
20
|
|
|
* |
21
|
|
|
* @author Stéphane Hulard <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Client implements HttpClient, HttpAsyncClient |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* React HTTP client. |
27
|
|
|
* |
28
|
|
|
* @var Client |
29
|
|
|
*/ |
30
|
|
|
private $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* React event loop. |
34
|
|
|
* |
35
|
|
|
* @var LoopInterface |
36
|
|
|
*/ |
37
|
|
|
private $loop; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* HttpPlug message factory. |
41
|
|
|
* |
42
|
|
|
* @var MessageFactory |
43
|
|
|
*/ |
44
|
|
|
private $messageFactory; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Initialize the React client. |
48
|
|
|
* |
49
|
|
|
* @param LoopInterface|null $loop React Event loop |
50
|
|
|
* @param Resolver $resolver React async DNS resolver |
|
|
|
|
51
|
|
|
* @param ReactClient $client React client to use |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
MessageFactory $messageFactory, |
55
|
|
|
LoopInterface $loop = null, |
56
|
|
|
ReactClient $client = null |
57
|
|
|
) { |
58
|
|
|
if (null !== $client && null === $loop) { |
59
|
|
|
throw new \RuntimeException( |
60
|
|
|
'You must give a LoopInterface instance with the Client' |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
$this->loop = (null !== $loop) ?: ReactFactory::buildEventLoop(); |
|
|
|
|
64
|
|
|
$this->client = (null !== $client) ?: ReactFactory::buildHttpClient($this->loop); |
|
|
|
|
65
|
|
|
|
66
|
|
|
$this->messageFactory = $messageFactory; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function sendRequest(RequestInterface $request) |
73
|
|
|
{ |
74
|
|
|
$promise = $this->sendAsyncRequest($request); |
75
|
|
|
|
76
|
|
|
return $promise->wait(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function sendAsyncRequest(RequestInterface $request) |
83
|
|
|
{ |
84
|
|
|
$reactRequest = $this->buildReactRequest($request); |
85
|
|
|
$deferred = new Deferred(); |
86
|
|
|
|
87
|
|
|
$reactRequest->on('error', function (\Exception $error) use ($deferred, $request) { |
88
|
|
|
$deferred->reject(new RequestException( |
89
|
|
|
$error->getMessage(), |
90
|
|
|
$request, |
91
|
|
|
$error |
92
|
|
|
)); |
93
|
|
|
}); |
94
|
|
|
$reactRequest->on('response', function (ReactResponse $reactResponse = null) use ($deferred, $reactRequest, $request) { |
95
|
|
|
$bodyStream = null; |
96
|
|
|
$reactResponse->on('data', function ($data) use (&$bodyStream) { |
|
|
|
|
97
|
|
|
if ($data instanceof StreamInterface) { |
98
|
|
|
$bodyStream = $data; |
99
|
|
|
} else { |
100
|
|
|
$bodyStream->write($data); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
}); |
103
|
|
|
|
104
|
|
|
$reactResponse->on('end', function (\Exception $error = null) use ($deferred, $request, $reactResponse, &$bodyStream) { |
|
|
|
|
105
|
|
|
$bodyStream->rewind(); |
|
|
|
|
106
|
|
|
$response = $this->buildResponse( |
107
|
|
|
$reactResponse, |
|
|
|
|
108
|
|
|
$bodyStream |
|
|
|
|
109
|
|
|
); |
110
|
|
|
if (null !== $error) { |
111
|
|
|
$deferred->reject(new HttpException( |
112
|
|
|
$error->getMessage(), |
113
|
|
|
$request, |
114
|
|
|
$response, |
115
|
|
|
$error |
116
|
|
|
)); |
117
|
|
|
} else { |
118
|
|
|
$deferred->resolve($response); |
119
|
|
|
} |
120
|
|
|
}); |
121
|
|
|
}); |
122
|
|
|
|
123
|
|
|
$reactRequest->end((string) $request->getBody()); |
124
|
|
|
|
125
|
|
|
$promise = new Promise($deferred->promise()); |
126
|
|
|
$promise->setLoop($this->loop); |
127
|
|
|
|
128
|
|
|
return $promise; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Build a React request from the PSR7 RequestInterface. |
133
|
|
|
* |
134
|
|
|
* @param RequestInterface $request |
135
|
|
|
* |
136
|
|
|
* @return ReactRequest |
137
|
|
|
*/ |
138
|
|
|
private function buildReactRequest(RequestInterface $request) |
139
|
|
|
{ |
140
|
|
|
$headers = []; |
141
|
|
|
foreach ($request->getHeaders() as $name => $value) { |
142
|
|
|
$headers[$name] = (is_array($value) ? $value[0] : $value); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$reactRequest = $this->client->request( |
|
|
|
|
146
|
|
|
$request->getMethod(), |
147
|
|
|
(string) $request->getUri(), |
148
|
|
|
$headers, |
149
|
|
|
$request->getProtocolVersion() |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
return $reactRequest; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Transform a React Response to a valid PSR7 ResponseInterface instance. |
157
|
|
|
* |
158
|
|
|
* @param ReactResponse $response |
159
|
|
|
* |
160
|
|
|
* @return ResponseInterface |
161
|
|
|
*/ |
162
|
|
|
private function buildResponse( |
163
|
|
|
ReactResponse $response, |
164
|
|
|
StreamInterface $body |
165
|
|
|
) { |
166
|
|
|
$body->rewind(); |
167
|
|
|
|
168
|
|
|
return $this->messageFactory->createResponse( |
169
|
|
|
$response->getCode(), |
170
|
|
|
$response->getReasonPhrase(), |
171
|
|
|
$response->getHeaders(), |
172
|
|
|
$body, |
173
|
|
|
$response->getVersion() |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.