|
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
|
2 |
|
return \WyriHaximus\React\futurePromise($loop)->then(function () use ($request, $options, $httpClient, $loop) { |
|
42
|
2 |
|
$sender = $this->createSender($options, $httpClient, $loop); |
|
43
|
2 |
|
return (new Browser($loop, $sender)) |
|
44
|
2 |
|
->withOptions($this->convertOptions($options)) |
|
45
|
2 |
|
->send($request); |
|
46
|
2 |
|
}); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $options |
|
51
|
|
|
* @param HttpClient $httpClient |
|
52
|
|
|
* @param LoopInterface $loop |
|
53
|
|
|
* @return Sender |
|
54
|
|
|
*/ |
|
55
|
2 |
|
protected function createSender(array $options, HttpClient $httpClient, LoopInterface $loop) |
|
56
|
|
|
{ |
|
57
|
2 |
|
$connector = $this->getProperty($httpClient, 'connector'); |
|
58
|
|
|
|
|
59
|
2 |
|
if (isset($options['proxy'])) { |
|
60
|
1 |
|
$resolver = $this->extractResolver($connector); |
|
61
|
1 |
|
$connector = (new SocksClient($options['proxy'], $loop, $connector, $resolver))->createConnector(); |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
return Sender::createFromLoopConnectors($loop, $connector); |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param array $options |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
2 |
|
protected function convertOptions(array $options) |
|
72
|
|
|
{ |
|
73
|
2 |
|
return $options; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param ConnectorInterface $connector |
|
78
|
|
|
* @return mixed|null |
|
79
|
|
|
*/ |
|
80
|
1 |
|
protected function extractResolver(ConnectorInterface $connector) |
|
81
|
|
|
{ |
|
82
|
1 |
|
if ($connector instanceof Connector || $connector instanceof DnsConnector) { |
|
83
|
|
|
return $this->getProperty($connector, 'resolver'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param object $object |
|
91
|
|
|
* @param string $desiredProperty |
|
92
|
|
|
* @return mixed |
|
93
|
|
|
*/ |
|
94
|
2 |
|
protected function getProperty($object, $desiredProperty) |
|
95
|
|
|
{ |
|
96
|
2 |
|
$reflection = new ReflectionObject($object); |
|
97
|
2 |
|
$property = $reflection->getProperty($desiredProperty); |
|
98
|
2 |
|
$property->setAccessible(true); |
|
99
|
2 |
|
return $property->getValue($object); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|