|
1
|
|
|
<?php |
|
2
|
|
|
namespace Aoe\Varnish\System; |
|
3
|
|
|
|
|
4
|
|
|
use GuzzleHttp\Client; |
|
5
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
|
6
|
|
|
use GuzzleHttp\Psr7\Response; |
|
7
|
|
|
use GuzzleHttp\RequestOptions; |
|
8
|
|
|
|
|
9
|
|
|
class Http |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Client |
|
13
|
|
|
*/ |
|
14
|
|
|
private $client; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var PromiseInterface[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private $promises = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param Client $client |
|
23
|
|
|
*/ |
|
24
|
2 |
|
public function __construct(Client $client) |
|
25
|
|
|
{ |
|
26
|
2 |
|
$this->client = $client; |
|
27
|
2 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $method |
|
31
|
|
|
* @param string $url |
|
32
|
|
|
* @param array $headers |
|
33
|
|
|
* @param integer $timeout |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function request($method, $url, $headers = [], $timeout = 0) |
|
36
|
|
|
{ |
|
37
|
2 |
|
$this->promises[] = $this->client->requestAsync($method, $url, [ |
|
38
|
2 |
|
RequestOptions::HEADERS => $headers, |
|
39
|
2 |
|
RequestOptions::TIMEOUT => $timeout |
|
40
|
2 |
|
]); |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return array |
|
45
|
|
|
* @throws \Exception|\Throwable |
|
46
|
|
|
*/ |
|
47
|
|
|
public function wait() |
|
48
|
|
|
{ |
|
49
|
|
|
$phrases = []; |
|
50
|
|
|
$results = \GuzzleHttp\Promise\settle($this->promises)->wait(); |
|
51
|
|
|
foreach ($results as $result) { |
|
52
|
|
|
if ($result['state'] === 'fulfilled') { |
|
53
|
|
|
$response = $result['value']; |
|
54
|
|
|
if ($response instanceof Response) { |
|
55
|
|
|
$phrases[] = [ |
|
56
|
|
|
'reason' => $response->getReasonPhrase(), |
|
57
|
|
|
'success' => $response->getStatusCode() === 200 |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
} else { |
|
61
|
|
|
if ($result['state'] === 'rejected') { |
|
62
|
|
|
$reason = $result['reason']; |
|
63
|
|
|
if ($reason instanceof \Exception) { |
|
64
|
|
|
$reason = $reason->getMessage(); |
|
65
|
|
|
} |
|
66
|
|
|
$phrases[] = [ |
|
67
|
|
|
'reason' => $reason, |
|
68
|
|
|
'success' => false |
|
69
|
|
|
]; |
|
70
|
|
|
} else { |
|
71
|
|
|
$phrases[] = [ |
|
72
|
|
|
'reason' => 'unknown exception', |
|
73
|
|
|
'success' => false |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
return $phrases; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|