1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the KongAdminApi package. |
5
|
|
|
* |
6
|
|
|
* (c) Unikorp <https://github.com/unikorp> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Unikorp\KongAdminApi; |
13
|
|
|
|
14
|
|
|
use Http\Client\Common\HttpMethodsClient; |
15
|
|
|
use Http\Client\Common\Plugin; |
16
|
|
|
use Http\Client\Common\PluginClient; |
17
|
|
|
use Http\Client\HttpClient; |
18
|
|
|
use Http\Discovery\HttpClientDiscovery; |
19
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
20
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
21
|
|
|
use Unikorp\KongAdminApi\NodeInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* client |
25
|
|
|
* |
26
|
|
|
* @author VEBER Arnaud <https://github.com/VEBERArnaud> |
27
|
|
|
*/ |
28
|
|
|
class Client |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* configurator |
32
|
|
|
* @var \Unikorp\KongAdminApi\ConfiguratorInterface $configurator |
33
|
|
|
*/ |
34
|
|
|
private $configurator = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* http client |
38
|
|
|
* @var \Http\Client\HttpClient $httpClient |
39
|
|
|
*/ |
40
|
|
|
private $httpClient = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* message factory |
44
|
|
|
* @var \Http\Message\MessageFactory $messageFactory |
45
|
|
|
*/ |
46
|
|
|
private $messageFactory = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* plugins |
50
|
|
|
* @var array $plugins |
51
|
|
|
*/ |
52
|
|
|
private $plugins = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* constructor |
56
|
|
|
* |
57
|
|
|
* @param \Unikorm\KongAdminApi\ConfiguratorInterface $configurator |
58
|
|
|
* @param \Http\Client\HttpClient $httpClient |
59
|
|
|
*/ |
60
|
4 |
|
public function __construct(ConfiguratorInterface $configurator, HttpClient $httpClient = null) |
61
|
|
|
{ |
62
|
4 |
|
$this->configurator = $configurator; |
63
|
|
|
|
64
|
4 |
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
65
|
4 |
|
$this->messageFactory = MessageFactoryDiscovery::find(); |
66
|
|
|
|
67
|
4 |
|
$this->plugins[] = new Plugin\AddHostPlugin( |
68
|
4 |
|
UriFactoryDiscovery::find()->createUri($configurator->getBaseUri()) |
69
|
|
|
); |
70
|
|
|
|
71
|
4 |
|
$this->plugins[] = new Plugin\HeaderSetPlugin($configurator->getHeaders()); |
72
|
4 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* get node |
76
|
|
|
* |
77
|
|
|
* @param string $name |
78
|
|
|
* |
79
|
|
|
* @return \Unikorp\KongAdminApi\NodeInterface |
80
|
|
|
*/ |
81
|
10 |
|
public function getNode(string $name): NodeInterface |
82
|
|
|
{ |
83
|
10 |
|
$node = $this->configurator->getNode($name); |
84
|
|
|
|
85
|
9 |
|
return new $node($this); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* add plugin |
90
|
|
|
* |
91
|
|
|
* @param \Http\Client\Common\Plugin $plugin |
92
|
|
|
* |
93
|
|
|
* @return this |
94
|
|
|
*/ |
95
|
1 |
|
public function addPlugin(Plugin $plugin): self |
96
|
|
|
{ |
97
|
1 |
|
$this->plugins[] = $plugin; |
98
|
|
|
|
99
|
1 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* get http client |
104
|
|
|
* |
105
|
|
|
* @return \Http\Client\Common\HttpMethodsClient |
106
|
|
|
*/ |
107
|
1 |
|
public function getHttpClient(): HttpMethodsClient |
108
|
|
|
{ |
109
|
1 |
|
return new HttpMethodsClient( |
110
|
1 |
|
new PluginClient($this->httpClient, $this->plugins), |
111
|
1 |
|
$this->messageFactory |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|