1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter; |
13
|
|
|
|
14
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
15
|
|
|
use Ivory\HttpAdapter\Normalizer\BodyNormalizer; |
16
|
|
|
use React\Dns\Resolver\Factory as DnsResolverFactory; |
17
|
|
|
use React\EventLoop\Factory as EventLoopFactory; |
18
|
|
|
use React\HttpClient\Factory as HttpClientFactory; |
19
|
|
|
use React\HttpClient\Response; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* React http adapter. |
23
|
|
|
* |
24
|
|
|
* @author GeLo <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ReactHttpAdapter extends AbstractHttpAdapter |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function getName() |
32
|
|
|
{ |
33
|
|
|
return 'react'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
protected function sendInternalRequest(InternalRequestInterface $internalRequest) |
40
|
|
|
{ |
41
|
|
|
$loop = EventLoopFactory::create(); |
42
|
|
|
$dnsResolverFactory = new DnsResolverFactory(); |
43
|
|
|
$httpClientFactory = new HttpClientFactory(); |
44
|
|
|
|
45
|
|
|
$error = null; |
46
|
|
|
$response = null; |
47
|
|
|
$body = null; |
48
|
|
|
|
49
|
|
|
$request = $httpClientFactory->create($loop, $dnsResolverFactory->createCached('8.8.8.8', $loop))->request( |
50
|
|
|
$internalRequest->getMethod(), |
51
|
|
|
$uri = (string) $internalRequest->getUri(), |
52
|
|
|
$this->prepareHeaders($internalRequest, true, true, true) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$request->on('error', function (\Exception $onError) use (&$error) { |
56
|
|
|
$error = $onError; |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
$request->on('response', function (Response $onResponse) use (&$response, &$body) { |
60
|
|
|
$onResponse->on('data', function ($data) use (&$body) { |
61
|
|
|
$body .= $data; |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
$response = $onResponse; |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
$request->end($this->prepareBody($internalRequest)); |
68
|
|
|
$loop->run(); |
69
|
|
|
|
70
|
|
|
if ($error !== null) { |
71
|
|
|
throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $error->getMessage()); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->getConfiguration()->getMessageFactory()->createResponse( |
75
|
|
|
(integer) $response->getCode(), |
|
|
|
|
76
|
|
|
$response->getVersion(), |
|
|
|
|
77
|
|
|
$response->getHeaders(), |
|
|
|
|
78
|
|
|
BodyNormalizer::normalize($body, $internalRequest->getMethod()) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.