1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace CloudPlayDev\ConfluenceClient\HttpClient; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Http\Client\Common\HttpMethodsClient; |
8
|
|
|
use Http\Client\Common\HttpMethodsClientInterface; |
9
|
|
|
use Http\Client\Common\Plugin; |
10
|
|
|
use Http\Client\Common\PluginClientFactory; |
11
|
|
|
use Http\Discovery\Psr17FactoryDiscovery; |
12
|
|
|
use Http\Discovery\Psr18ClientDiscovery; |
13
|
|
|
use Psr\Http\Client\ClientInterface; |
14
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
15
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
16
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
17
|
|
|
|
18
|
|
|
class Builder |
19
|
|
|
{ |
20
|
|
|
private ClientInterface $httpClient; |
21
|
|
|
private RequestFactoryInterface $requestFactory; |
22
|
|
|
private StreamFactoryInterface $streamFactory; |
23
|
|
|
private UriFactoryInterface $uriFactory; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The currently registered plugins. |
28
|
|
|
* |
29
|
|
|
* @var Plugin[] |
30
|
|
|
*/ |
31
|
|
|
private array $plugins = []; |
32
|
|
|
|
33
|
|
|
private ?HttpMethodsClientInterface $pluginClient = null; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
ClientInterface $httpClient = null, |
37
|
|
|
RequestFactoryInterface $requestFactory = null, |
38
|
|
|
StreamFactoryInterface $streamFactory = null, |
39
|
|
|
UriFactoryInterface $uriFactory = null |
40
|
|
|
) |
41
|
|
|
{ |
42
|
|
|
$this->httpClient = $httpClient ?? Psr18ClientDiscovery::find(); |
43
|
|
|
$this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); |
44
|
|
|
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); |
45
|
|
|
$this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add a new plugin to the end of the plugin chain. |
50
|
|
|
* |
51
|
|
|
* @param Plugin $plugin |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function addPlugin(Plugin $plugin): void |
56
|
|
|
{ |
57
|
|
|
$this->plugins[] = $plugin; |
58
|
|
|
$this->pluginClient = null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Remove a plugin by its fully qualified class name (FQCN). |
63
|
|
|
* |
64
|
|
|
* @param string $fqcn |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function removePlugin(string $fqcn): void |
69
|
|
|
{ |
70
|
|
|
foreach ($this->plugins as $idx => $plugin) { |
71
|
|
|
if ($plugin instanceof $fqcn) { |
72
|
|
|
unset($this->plugins[$idx]); |
73
|
|
|
$this->pluginClient = null; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return HttpMethodsClientInterface |
80
|
|
|
*/ |
81
|
|
|
public function getHttpClient(): HttpMethodsClientInterface |
82
|
|
|
{ |
83
|
|
|
if (null === $this->pluginClient) { |
84
|
|
|
$this->pluginClient = new HttpMethodsClient( |
85
|
|
|
(new PluginClientFactory())->createClient($this->httpClient, $this->plugins), |
86
|
|
|
$this->requestFactory, |
87
|
|
|
$this->streamFactory |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->pluginClient; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the URI factory. |
96
|
|
|
* |
97
|
|
|
* @return UriFactoryInterface |
98
|
|
|
*/ |
99
|
|
|
public function getUriFactory(): UriFactoryInterface |
100
|
|
|
{ |
101
|
|
|
return $this->uriFactory; |
102
|
|
|
} |
103
|
|
|
} |