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\HttpProxy\ProxyConnector as HttpProxyClient; |
16
|
|
|
use Clue\React\Socks\Client as SocksProxyClient; |
17
|
|
|
use Psr\Http\Message\RequestInterface; |
18
|
|
|
use React\Dns\Resolver\Resolver; |
19
|
|
|
use React\EventLoop\LoopInterface; |
20
|
|
|
use React\HttpClient\Client as HttpClient; |
21
|
|
|
use ReflectionObject; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class RequestFactory |
25
|
|
|
* |
26
|
|
|
* @package WyriHaximus\React\Guzzle\HttpClient |
27
|
|
|
*/ |
28
|
|
|
class RequestFactory |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @param RequestInterface $request |
33
|
|
|
* @param array $options |
34
|
|
|
* @param $resolver Resolver |
35
|
|
|
* @param HttpClient $httpClient |
36
|
|
|
* @param LoopInterface $loop |
37
|
|
|
* @return \React\Promise\Promise |
38
|
|
|
*/ |
39
|
|
|
public function create( |
40
|
|
|
RequestInterface $request, |
41
|
|
|
array $options, |
42
|
|
|
Resolver $resolver, |
43
|
|
|
HttpClient $httpClient, |
44
|
|
|
LoopInterface $loop |
45
|
|
|
) { |
46
|
3 |
|
return \WyriHaximus\React\futurePromise($loop)->then(function () use ( |
47
|
3 |
|
$request, |
48
|
3 |
|
$options, |
49
|
3 |
|
$resolver, |
50
|
3 |
|
$httpClient, |
51
|
3 |
|
$loop |
52
|
|
|
) { |
53
|
3 |
|
$sender = $this->createSender($options, $resolver, $httpClient, $loop); |
54
|
3 |
|
return (new Browser($loop, $sender)) |
55
|
3 |
|
->withOptions($this->convertOptions($options)) |
56
|
3 |
|
->send($request); |
57
|
3 |
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $options |
62
|
|
|
* @param HttpClient $httpClient |
63
|
|
|
* @param LoopInterface $loop |
64
|
|
|
* @return Sender |
65
|
|
|
*/ |
66
|
3 |
|
protected function createSender(array $options, Resolver $resolver, HttpClient $httpClient, LoopInterface $loop) |
67
|
|
|
{ |
68
|
3 |
|
$connector = $this->getProperty($httpClient, 'connector'); |
69
|
|
|
|
70
|
3 |
|
if (isset($options['proxy'])) { |
71
|
2 |
|
switch (parse_url($options['proxy'], PHP_URL_SCHEME)) { |
72
|
2 |
|
case 'http': |
73
|
1 |
|
$connector = new HttpProxyClient($options['proxy'], $connector); |
74
|
1 |
|
break; |
75
|
1 |
|
case 'socks': |
76
|
1 |
|
$connector = (new SocksProxyClient( |
77
|
1 |
|
$options['proxy'], |
78
|
1 |
|
$loop, |
79
|
1 |
|
$connector, |
80
|
1 |
|
$resolver |
81
|
1 |
|
))->createConnector(); |
82
|
1 |
|
break; |
83
|
2 |
|
} |
84
|
2 |
|
} |
85
|
|
|
|
86
|
3 |
|
return Sender::createFromLoopConnectors($loop, $connector); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $options |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
3 |
|
protected function convertOptions(array $options) |
94
|
|
|
{ |
95
|
3 |
|
return $options; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param object $object |
100
|
|
|
* @param string $desiredProperty |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
3 |
|
protected function getProperty($object, $desiredProperty) |
104
|
|
|
{ |
105
|
3 |
|
$reflection = new ReflectionObject($object); |
106
|
3 |
|
$property = $reflection->getProperty($desiredProperty); |
107
|
3 |
|
$property->setAccessible(true); |
108
|
3 |
|
return $property->getValue($object); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|