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
|
6 |
|
return \WyriHaximus\React\futurePromise($loop)->then(function () use ( |
47
|
6 |
|
$request, |
48
|
6 |
|
$options, |
49
|
6 |
|
$resolver, |
50
|
6 |
|
$httpClient, |
51
|
6 |
|
$loop |
52
|
|
|
) { |
53
|
6 |
|
$sender = $this->createSender($options, $resolver, $httpClient, $loop); |
54
|
6 |
|
return (new Browser($loop, $sender)) |
55
|
6 |
|
->withOptions($this->convertOptions($options)) |
56
|
6 |
|
->send($request); |
57
|
6 |
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $options |
62
|
|
|
* @param HttpClient $httpClient |
63
|
|
|
* @param LoopInterface $loop |
64
|
|
|
* @return Sender |
65
|
|
|
*/ |
66
|
6 |
|
protected function createSender(array $options, Resolver $resolver, HttpClient $httpClient, LoopInterface $loop) |
67
|
|
|
{ |
68
|
6 |
|
$connector = $this->getProperty($httpClient, 'connector'); |
69
|
|
|
|
70
|
6 |
|
if (isset($options['proxy'])) { |
71
|
5 |
|
switch (parse_url($options['proxy'], PHP_URL_SCHEME)) { |
72
|
5 |
|
case 'http': |
73
|
1 |
|
$connector = new HttpProxyClient($options['proxy'], $connector); |
74
|
1 |
|
break; |
75
|
4 |
|
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
|
3 |
|
case 'socks4': |
84
|
3 |
View Code Duplication |
case 'socks4a': |
|
|
|
|
85
|
2 |
|
$proxyClient = new SocksProxyClient( |
86
|
2 |
|
$options['proxy'], |
87
|
2 |
|
$loop, |
88
|
2 |
|
$connector, |
89
|
|
|
$resolver |
90
|
2 |
|
); |
91
|
2 |
|
$proxyClient->setProtocolVersion(4); |
92
|
2 |
|
$connector = $proxyClient->createConnector(); |
93
|
2 |
|
break; |
94
|
1 |
View Code Duplication |
case 'socks5': |
|
|
|
|
95
|
1 |
|
$proxyClient = new SocksProxyClient( |
96
|
1 |
|
$options['proxy'], |
97
|
1 |
|
$loop, |
98
|
1 |
|
$connector, |
99
|
|
|
$resolver |
100
|
1 |
|
); |
101
|
1 |
|
$proxyClient->setProtocolVersion(5); |
102
|
1 |
|
$connector = $proxyClient->createConnector(); |
103
|
1 |
|
break; |
104
|
5 |
|
} |
105
|
5 |
|
} |
106
|
|
|
|
107
|
6 |
|
return Sender::createFromLoopConnectors($loop, $connector); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $options |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
6 |
|
protected function convertOptions(array $options) |
115
|
|
|
{ |
116
|
6 |
|
return $options; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param object $object |
121
|
|
|
* @param string $desiredProperty |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
6 |
|
protected function getProperty($object, $desiredProperty) |
125
|
|
|
{ |
126
|
6 |
|
$reflection = new ReflectionObject($object); |
127
|
6 |
|
$property = $reflection->getProperty($desiredProperty); |
128
|
6 |
|
$property->setAccessible(true); |
129
|
6 |
|
return $property->getValue($object); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.