1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of ReactGuzzleRing. |
5
|
|
|
* |
6
|
|
|
** (c) 2014 Cees-Jan Kiewiet |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace WyriHaximus\React\Guzzle\HttpClient; |
12
|
|
|
|
13
|
|
|
use Clue\React\Buzz\Browser; |
14
|
|
|
use Clue\React\Buzz\Io\Sender; |
15
|
|
|
use Clue\React\Socks\Client as SocksClient; |
16
|
|
|
use Psr\Http\Message\RequestInterface; |
17
|
|
|
use React\EventLoop\LoopInterface; |
18
|
|
|
use React\HttpClient\Client as HttpClient; |
19
|
|
|
use React\SocketClient\Connector; |
20
|
|
|
use React\SocketClient\ConnectorInterface; |
21
|
|
|
use React\SocketClient\DnsConnector; |
22
|
|
|
use ReflectionObject; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class RequestFactory |
26
|
|
|
* |
27
|
|
|
* @package WyriHaximus\React\Guzzle\HttpClient |
28
|
|
|
*/ |
29
|
|
|
class RequestFactory |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @param RequestInterface $request |
34
|
|
|
* @param array $options |
35
|
|
|
* @param HttpClient $httpClient |
36
|
|
|
* @param LoopInterface $loop |
37
|
|
|
* @return \React\Promise\Promise |
38
|
|
|
*/ |
39
|
|
|
public function create(RequestInterface $request, array $options, HttpClient $httpClient, LoopInterface $loop) |
40
|
|
|
{ |
41
|
1 |
|
return \WyriHaximus\React\futurePromise($loop)->then(function () use ($request, $options, $httpClient, $loop) { |
42
|
|
|
$sender = $this->createSender($options, $httpClient, $loop); |
43
|
|
|
return (new Browser($loop, $sender)) |
44
|
|
|
->withOptions($this->convertOptions($options)) |
45
|
|
|
->send($request); |
46
|
1 |
|
}); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function createSender(array $options, HttpClient $httpClient, LoopInterface $loop) |
50
|
|
|
{ |
51
|
|
|
$connector = $this->extractConnector($httpClient); |
52
|
|
|
|
53
|
|
|
if (isset($options['proxy'])) { |
54
|
|
|
$resolver = $this->extractResolver($connector); |
55
|
|
|
$connector = (new SocksClient($options['proxy'], $loop, $connector, $resolver))->createConnector(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return Sender::createFromLoopConnectors($loop, $connector); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function convertOptions(array $options) |
62
|
|
|
{ |
63
|
|
|
return $options; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function extractConnector(HttpClient $httpClient) |
67
|
|
|
{ |
68
|
|
|
$reflection = new ReflectionObject($httpClient); |
69
|
|
|
$property = $reflection->getProperty('connector'); |
70
|
|
|
$property->setAccessible(true); |
71
|
|
|
return $property->getValue($httpClient); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function extractResolver(ConnectorInterface $connector) |
75
|
|
|
{ |
76
|
|
|
if ($connector instanceof Connector || $connector instanceof DnsConnector) { |
77
|
|
|
$reflection = new ReflectionObject($connector); |
78
|
|
|
$property = $reflection->getProperty('resolver'); |
79
|
|
|
$property->setAccessible(true); |
80
|
|
|
return $property->getValue($connector); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|