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 React\Promise\Deferred; |
22
|
|
|
use React\SocketClient\TimeoutConnector; |
23
|
|
|
use React\Stream\Stream; |
|
|
|
|
24
|
|
|
use ReflectionObject; |
25
|
|
|
use React\Stream\ReadableStreamInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class RequestFactory |
29
|
|
|
* |
30
|
|
|
* @package WyriHaximus\React\Guzzle\HttpClient |
31
|
|
|
*/ |
32
|
|
|
class RequestFactory |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
* @param RequestInterface $request |
37
|
|
|
* @param array $options |
38
|
|
|
* @param $resolver Resolver |
39
|
|
|
* @param HttpClient $httpClient |
40
|
|
|
* @param LoopInterface $loop |
41
|
|
|
* @return \React\Promise\Promise |
42
|
|
|
*/ |
43
|
6 |
|
public function create( |
44
|
|
|
RequestInterface $request, |
45
|
|
|
array $options, |
46
|
|
|
Resolver $resolver, |
47
|
|
|
HttpClient $httpClient, |
48
|
|
|
LoopInterface $loop |
49
|
|
|
) { |
50
|
6 |
|
$options = $this->convertOptions($options); |
51
|
|
|
|
52
|
6 |
|
if (isset($options['delay'])) { |
53
|
|
|
$promise = \WyriHaximus\React\timedPromise($loop, $options['delay']); |
54
|
|
|
} |
55
|
6 |
|
if (!isset($promise)) { |
56
|
6 |
|
$promise = \WyriHaximus\React\futurePromise($loop); |
57
|
6 |
|
} |
58
|
|
|
|
59
|
|
|
return $promise->then(function () use ( |
60
|
6 |
|
$request, |
61
|
6 |
|
$options, |
62
|
6 |
|
$resolver, |
63
|
6 |
|
$httpClient, |
64
|
6 |
|
$loop |
65
|
1 |
|
) { |
66
|
6 |
|
$sender = $this->createSender($options, $resolver, $httpClient, $loop); |
67
|
6 |
|
return (new Browser($loop, $sender)) |
68
|
6 |
|
->withOptions($options) |
69
|
|
|
->send($request)->then(function ($response) use ($loop, $options) { |
70
|
|
|
if (!isset($options['sink'])) { |
71
|
|
|
return \React\Promise\resolve($response); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return \React\Promise\resolve($this->sink($loop, $response, $options['sink'])); |
75
|
6 |
|
}); |
76
|
6 |
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
protected function sink($loop, $response, $target) |
80
|
1 |
|
{ |
81
|
|
|
$deferred = new Deferred(); |
82
|
|
|
$writeStream = fopen($target, 'w'); |
83
|
|
|
stream_set_blocking($writeStream, 0); |
84
|
|
|
$saveToStream = new Stream($writeStream, $loop); |
85
|
|
|
|
86
|
|
|
$saveToStream->on( |
87
|
|
|
'end', |
88
|
|
|
function () use ($deferred, $response) { |
89
|
|
|
$deferred->resolve($response); |
90
|
|
|
} |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$body = $response->getBody(); |
94
|
|
|
if ($body instanceof ReadableStreamInterface) { |
95
|
|
|
$body->pipe($saveToStream); |
96
|
|
|
} else { |
97
|
|
|
$saveToStream->end($body->getContents()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $deferred->promise(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $options |
105
|
|
|
* @param HttpClient $httpClient |
106
|
|
|
* @param LoopInterface $loop |
107
|
|
|
* @return Sender |
108
|
|
|
*/ |
109
|
6 |
|
protected function createSender(array $options, Resolver $resolver, HttpClient $httpClient, LoopInterface $loop) |
110
|
|
|
{ |
111
|
6 |
|
$connector = $this->getProperty($httpClient, 'connector'); |
112
|
|
|
|
113
|
6 |
|
if (isset($options['proxy'])) { |
114
|
5 |
|
switch (parse_url($options['proxy'], PHP_URL_SCHEME)) { |
115
|
5 |
|
case 'http': |
116
|
1 |
|
$connector = new HttpProxyClient($options['proxy'], $connector); |
117
|
1 |
|
break; |
118
|
4 |
|
case 'socks': |
119
|
1 |
|
$connector = $this->createSocksProxy( |
120
|
1 |
|
$options['proxy'], |
121
|
1 |
|
$loop, |
122
|
1 |
|
$connector, |
123
|
|
|
$resolver |
124
|
1 |
|
); |
125
|
1 |
|
break; |
126
|
3 |
|
case 'socks4': |
127
|
3 |
|
case 'socks4a': |
128
|
2 |
|
$connector = $this->createSocksProxy( |
129
|
2 |
|
$options['proxy'], |
130
|
2 |
|
$loop, |
131
|
2 |
|
$connector, |
132
|
2 |
|
$resolver, |
133
|
|
|
4 |
134
|
2 |
|
); |
135
|
2 |
|
break; |
136
|
1 |
|
case 'socks5': |
137
|
1 |
|
$connector = $this->createSocksProxy( |
138
|
1 |
|
$options['proxy'], |
139
|
1 |
|
$loop, |
140
|
1 |
|
$connector, |
141
|
1 |
|
$resolver, |
142
|
|
|
5 |
143
|
1 |
|
); |
144
|
1 |
|
break; |
145
|
5 |
|
} |
146
|
5 |
|
} |
147
|
|
|
|
148
|
6 |
|
if (isset($options['connect_timeout'])) { |
149
|
|
|
$connector = new TimeoutConnector($connector, $options['connect_timeout'], $loop); |
150
|
1 |
|
} |
151
|
|
|
|
152
|
6 |
|
return Sender::createFromLoopConnectors($loop, $connector); |
153
|
|
|
} |
154
|
|
|
|
155
|
4 |
|
protected function createSocksProxy( |
156
|
|
|
$url, |
157
|
|
|
$loop, |
158
|
|
|
$connector, |
159
|
|
|
$resolver, |
160
|
|
|
$version = null |
161
|
|
|
) { |
162
|
4 |
|
$proxyClient = new SocksProxyClient( |
163
|
4 |
|
$url, |
164
|
4 |
|
$loop, |
165
|
4 |
|
$connector, |
166
|
|
|
$resolver |
167
|
4 |
|
); |
168
|
4 |
|
if ($version !== null) { |
169
|
3 |
|
$proxyClient->setProtocolVersion($version); |
170
|
3 |
|
} |
171
|
4 |
|
return $proxyClient->createConnector(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param array $options |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
6 |
|
protected function convertOptions(array $options) |
179
|
|
|
{ |
180
|
|
|
// provides backwards compatibility for Guzzle 3-5. |
181
|
6 |
|
if (isset($options['client'])) { |
182
|
|
|
$options = array_merge($options, $options['client']); |
183
|
|
|
unset($options['client']); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
// provides for backwards compatibility for Guzzle 3-5 |
187
|
6 |
|
if (isset($options['save_to'])) { |
188
|
|
|
$options['sink'] = $options['save_to']; |
189
|
|
|
unset($options['save_to']); |
190
|
|
|
} |
191
|
|
|
|
192
|
6 |
|
if (isset($options['allow_redirects'])) { |
193
|
|
|
$this->convertRedirectOption($options); |
194
|
|
|
} |
195
|
|
|
|
196
|
6 |
|
return $options; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
protected function convertRedirectOption(&$options) |
200
|
|
|
{ |
201
|
|
|
$option = $options['allow_redirects']; |
202
|
|
|
unset($options['allow_redirects']); |
203
|
|
|
|
204
|
|
|
if (is_bool($option)) { |
205
|
|
|
$options['followRedirects'] = $option; |
206
|
|
|
return; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if (is_array($option)) { |
210
|
|
|
if (isset($option['max'])) { |
211
|
|
|
$options['maxRedirects'] = $option['max']; |
212
|
|
|
} |
213
|
|
|
$options['followRedirects'] = true; |
214
|
|
|
return; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param object $object |
220
|
|
|
* @param string $desiredProperty |
221
|
|
|
* @return mixed |
222
|
|
|
*/ |
223
|
6 |
|
protected function getProperty($object, $desiredProperty) |
224
|
|
|
{ |
225
|
6 |
|
$reflection = new ReflectionObject($object); |
226
|
6 |
|
$property = $reflection->getProperty($desiredProperty); |
227
|
6 |
|
$property->setAccessible(true); |
228
|
6 |
|
return $property->getValue($object); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: