1
|
|
|
<?php |
2
|
|
|
namespace EventStore\Http; |
3
|
|
|
|
4
|
|
|
use Doctrine\Common\Cache\ApcCache; |
5
|
|
|
use Doctrine\Common\Cache\Cache; |
6
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
7
|
|
|
use Exception as PhpException; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\ClientInterface; |
10
|
|
|
use GuzzleHttp\Exception\ClientException as GuzzleClientException; |
11
|
|
|
use GuzzleHttp\Exception\RequestException as GuzzleRequestException; |
12
|
|
|
use GuzzleHttp\Handler\CurlMultiHandler; |
13
|
|
|
use GuzzleHttp\HandlerStack; |
14
|
|
|
use GuzzleHttp\Pool; |
15
|
|
|
use Kevinrob\GuzzleCache\CacheMiddleware; |
16
|
|
|
use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage; |
17
|
|
|
use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy; |
18
|
|
|
use Psr\Http\Message\RequestInterface; |
19
|
|
|
|
20
|
|
|
final class GuzzleHttpClient implements HttpClientInterface |
21
|
|
|
{ |
22
|
26 |
|
public function __construct(ClientInterface $client = null) |
23
|
|
|
{ |
24
|
26 |
|
$this->client = $client ?: new Client([ |
|
|
|
|
25
|
26 |
|
'handler' => new CurlMultiHandler(), |
26
|
|
|
]); |
27
|
26 |
|
} |
28
|
|
|
|
29
|
|
|
public static function withFilesystemCache($path) |
30
|
|
|
{ |
31
|
|
|
return self::withDoctrineCache( |
32
|
|
|
new FilesystemCache($path) |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function withApcCache() |
37
|
|
|
{ |
38
|
|
|
return self::withDoctrineCache( |
39
|
|
|
new ApcCache() |
|
|
|
|
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function withDoctrineCache(Cache $doctrineCache) |
44
|
|
|
{ |
45
|
|
|
$stack = new HandlerStack(new CurlMultiHandler()); |
46
|
|
|
|
47
|
|
|
$stack->push( |
48
|
|
|
new CacheMiddleware( |
49
|
|
|
new PublicCacheStrategy( |
50
|
|
|
new DoctrineCacheStorage( |
51
|
|
|
$doctrineCache |
52
|
|
|
) |
53
|
|
|
) |
54
|
|
|
), |
55
|
|
|
'cache' |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$client = new Client([ |
59
|
|
|
'handler' => $stack |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
return new self($client); |
63
|
|
|
} |
64
|
|
|
|
65
|
7 |
|
public function sendRequestBatch(array $requests) |
66
|
|
|
{ |
67
|
7 |
|
$responses = Pool::batch( |
68
|
7 |
|
$this->client, |
69
|
|
|
$requests |
70
|
|
|
); |
71
|
|
|
|
72
|
7 |
|
foreach ($responses as $response) { |
73
|
7 |
|
if ($response instanceof PhpException) { |
74
|
7 |
|
throw $response; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
7 |
|
return $responses; |
79
|
|
|
} |
80
|
|
|
|
81
|
26 |
|
public function send(RequestInterface $request) |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
26 |
|
return $this->client->send($request); |
85
|
1 |
|
} catch (GuzzleClientException $e) { |
86
|
|
|
throw new Exception\ClientException($e->getMessage(), $e->getCode(), $e); |
87
|
1 |
|
} catch (GuzzleRequestException $e) { |
88
|
1 |
|
throw new Exception\RequestException($e->getMessage(), $e->getCode(), $e); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: