|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of Esi\IPQuery. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Eric Sizemore <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* This source file is subject to the MIT license. For the full copyright and |
|
11
|
|
|
* license information, please view the LICENSE file that was distributed with |
|
12
|
|
|
* this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Esi\IPQuery\HttpClient; |
|
16
|
|
|
|
|
17
|
|
|
use Http\Client\Common\HttpMethodsClient; |
|
18
|
|
|
use Http\Client\Common\HttpMethodsClientInterface; |
|
19
|
|
|
use Http\Client\Common\Plugin; |
|
20
|
|
|
use Http\Client\Common\Plugin\CachePlugin; |
|
21
|
|
|
use Http\Client\Common\PluginClientFactory; |
|
22
|
|
|
use Http\Discovery\Psr17FactoryDiscovery; |
|
23
|
|
|
use Http\Discovery\Psr18ClientDiscovery; |
|
24
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
25
|
|
|
use Psr\Http\Client\ClientInterface; |
|
26
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
27
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
|
28
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The HTTP client builder class. |
|
32
|
|
|
*/ |
|
33
|
|
|
final class Builder |
|
34
|
|
|
{ |
|
35
|
|
|
private ?CachePlugin $cachePlugin = null; |
|
36
|
|
|
|
|
37
|
|
|
private readonly ClientInterface $httpClient; |
|
38
|
|
|
|
|
39
|
|
|
private ?HttpMethodsClientInterface $httpMethodsClient = null; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array<Plugin> |
|
43
|
|
|
*/ |
|
44
|
|
|
private array $plugins = []; |
|
45
|
|
|
|
|
46
|
|
|
private readonly RequestFactoryInterface $requestFactory; |
|
47
|
|
|
|
|
48
|
|
|
private readonly StreamFactoryInterface $streamFactory; |
|
49
|
|
|
|
|
50
|
|
|
private readonly UriFactoryInterface $uriFactory; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Create a new client builder instance. |
|
54
|
|
|
*/ |
|
55
|
17 |
|
public function __construct( |
|
56
|
|
|
?ClientInterface $httpClient = null, |
|
57
|
|
|
?RequestFactoryInterface $requestFactory = null, |
|
58
|
|
|
?StreamFactoryInterface $streamFactory = null, |
|
59
|
|
|
?UriFactoryInterface $uriFactory = null |
|
60
|
|
|
) { |
|
61
|
17 |
|
$this->httpClient = $httpClient ?? Psr18ClientDiscovery::find(); |
|
|
|
|
|
|
62
|
17 |
|
$this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); |
|
|
|
|
|
|
63
|
17 |
|
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); |
|
|
|
|
|
|
64
|
17 |
|
$this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory(); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @template T |
|
69
|
|
|
* |
|
70
|
|
|
* @param array<array-key, T> $config |
|
|
|
|
|
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function addCache(CacheItemPoolInterface $cacheItemPool, array $config = []): void |
|
73
|
|
|
{ |
|
74
|
3 |
|
$this->cachePlugin = CachePlugin::clientCache($cacheItemPool, $this->streamFactory, $config); |
|
75
|
3 |
|
$this->httpMethodsClient = null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
9 |
|
public function addPlugin(Plugin $plugin): void |
|
79
|
|
|
{ |
|
80
|
9 |
|
$this->plugins[] = $plugin; |
|
81
|
9 |
|
$this->httpMethodsClient = null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
10 |
|
public function getHttpClient(): HttpMethodsClientInterface |
|
85
|
|
|
{ |
|
86
|
10 |
|
if (\is_null($this->httpMethodsClient)) { |
|
87
|
10 |
|
$plugins = $this->plugins; |
|
88
|
|
|
|
|
89
|
10 |
|
if (!\is_null($this->cachePlugin)) { |
|
90
|
2 |
|
$plugins[] = $this->cachePlugin; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
10 |
|
$this->httpMethodsClient = new HttpMethodsClient( |
|
94
|
10 |
|
(new PluginClientFactory())->createClient($this->httpClient, $plugins), |
|
95
|
10 |
|
$this->requestFactory, |
|
96
|
10 |
|
$this->streamFactory |
|
97
|
10 |
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
10 |
|
return $this->httpMethodsClient; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
public function getRequestFactory(): RequestFactoryInterface |
|
104
|
|
|
{ |
|
105
|
2 |
|
return $this->requestFactory; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
public function getStreamFactory(): StreamFactoryInterface |
|
109
|
|
|
{ |
|
110
|
2 |
|
return $this->streamFactory; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
8 |
|
public function getUriFactory(): UriFactoryInterface |
|
114
|
|
|
{ |
|
115
|
8 |
|
return $this->uriFactory; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
public function removeCache(): void |
|
119
|
|
|
{ |
|
120
|
2 |
|
$this->cachePlugin = null; |
|
121
|
2 |
|
$this->httpMethodsClient = null; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param class-string<Plugin> $pluginClassName |
|
|
|
|
|
|
126
|
|
|
*/ |
|
127
|
9 |
|
public function removePlugin(string $pluginClassName): void |
|
128
|
|
|
{ |
|
129
|
9 |
|
$pluginRemoved = false; |
|
130
|
|
|
|
|
131
|
9 |
|
foreach ($this->plugins as $key => $plugin) { |
|
132
|
8 |
|
if ($plugin instanceof $pluginClassName) { |
|
133
|
2 |
|
unset($this->plugins[$key]); |
|
134
|
|
|
|
|
135
|
2 |
|
$pluginRemoved = true; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
9 |
|
if ($pluginRemoved) { |
|
140
|
2 |
|
$this->httpMethodsClient = null; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|