1 | <?php |
||
27 | class Client implements HttpClient, HttpAsyncClient |
||
28 | { |
||
29 | /** |
||
30 | * React HTTP client. |
||
31 | * |
||
32 | * @var Client |
||
33 | */ |
||
34 | private $client; |
||
35 | |||
36 | /** |
||
37 | * React event loop. |
||
38 | * |
||
39 | * @var LoopInterface |
||
40 | */ |
||
41 | private $loop; |
||
42 | |||
43 | /** |
||
44 | * @var ResponseFactory |
||
45 | */ |
||
46 | private $responseFactory; |
||
47 | |||
48 | /** |
||
49 | * @var StreamFactory |
||
50 | */ |
||
51 | private $streamFactory; |
||
52 | |||
53 | /** |
||
54 | * Initialize the React client. |
||
55 | * |
||
56 | * @param ResponseFactory|null $responseFactory |
||
57 | * @param LoopInterface|null $loop |
||
58 | * @param ReactClient|null $client |
||
59 | * @param StreamFactory|null $streamFactory |
||
60 | */ |
||
61 | 108 | public function __construct( |
|
62 | 1 | ResponseFactory $responseFactory = null, |
|
63 | LoopInterface $loop = null, |
||
64 | ReactClient $client = null, |
||
65 | StreamFactory $streamFactory = null |
||
66 | ) { |
||
67 | 108 | if (null !== $client && null === $loop) { |
|
68 | throw new \RuntimeException( |
||
69 | 'You must give a LoopInterface instance with the Client' |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | 108 | $this->loop = $loop ?: ReactFactory::buildEventLoop(); |
|
74 | 108 | $this->client = $client ?: ReactFactory::buildHttpClient($this->loop); |
|
|
|||
75 | |||
76 | 108 | $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); |
|
77 | 108 | $this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find(); |
|
78 | 108 | } |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 53 | public function sendRequest(RequestInterface $request) |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 108 | public function sendAsyncRequest(RequestInterface $request) |
|
94 | { |
||
95 | 108 | $reactRequest = $this->buildReactRequest($request); |
|
96 | 108 | $deferred = new Deferred(); |
|
97 | |||
98 | $reactRequest->on('error', function (\Exception $error) use ($deferred, $request) { |
||
99 | 3 | $deferred->reject(new RequestException( |
|
100 | 3 | $error->getMessage(), |
|
101 | 3 | $request, |
|
102 | $error |
||
103 | 3 | )); |
|
104 | 108 | }); |
|
105 | |||
106 | $reactRequest->on('response', function (ReactResponse $reactResponse = null) use ($deferred, $reactRequest, $request) { |
||
107 | 105 | $bodyStream = $this->streamFactory->createStream(); |
|
108 | $reactResponse->on('data', function ($data) use (&$bodyStream) { |
||
109 | 105 | $bodyStream->write((string) $data); |
|
110 | 105 | }); |
|
111 | |||
112 | 105 | $reactResponse->on('end', function (\Exception $error = null) use ($deferred, $request, $reactResponse, &$bodyStream) { |
|
113 | 105 | $response = $this->buildResponse( |
|
114 | 105 | $reactResponse, |
|
115 | $bodyStream |
||
116 | 105 | ); |
|
117 | 105 | if (null !== $error) { |
|
118 | $deferred->reject(new HttpException( |
||
119 | $error->getMessage(), |
||
120 | $request, |
||
121 | $response, |
||
122 | $error |
||
123 | )); |
||
124 | } else { |
||
125 | 105 | $deferred->resolve($response); |
|
126 | } |
||
127 | 105 | }); |
|
128 | 108 | }); |
|
129 | |||
130 | 108 | $reactRequest->end((string) $request->getBody()); |
|
131 | |||
132 | 108 | $promise = new Promise($deferred->promise()); |
|
133 | 108 | $promise->setLoop($this->loop); |
|
134 | |||
135 | 108 | return $promise; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * Build a React request from the PSR7 RequestInterface. |
||
140 | * |
||
141 | * @param RequestInterface $request |
||
142 | * |
||
143 | * @return ReactRequest |
||
144 | */ |
||
145 | 108 | private function buildReactRequest(RequestInterface $request) |
|
146 | { |
||
147 | 108 | $headers = []; |
|
148 | |||
149 | 108 | foreach ($request->getHeaders() as $name => $value) { |
|
150 | 108 | $headers[$name] = (is_array($value) ? $value[0] : $value); |
|
151 | 108 | } |
|
152 | |||
153 | 108 | $reactRequest = $this->client->request( |
|
154 | 108 | $request->getMethod(), |
|
155 | 108 | (string) $request->getUri(), |
|
156 | 108 | $headers, |
|
157 | 108 | $request->getProtocolVersion() |
|
158 | 108 | ); |
|
159 | |||
160 | 108 | return $reactRequest; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * Transform a React Response to a valid PSR7 ResponseInterface instance. |
||
165 | * |
||
166 | * @param ReactResponse $response |
||
167 | * @param StreamInterface $body |
||
168 | * |
||
169 | * @return ResponseInterface |
||
170 | */ |
||
171 | 105 | private function buildResponse( |
|
185 | } |
||
186 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..