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
|
3 |
|
public function __construct(ConfiguratorInterface $configurator, HttpClient $httpClient = null) |
61
|
|
|
{ |
62
|
3 |
|
$this->configurator = $configurator; |
63
|
|
|
|
64
|
3 |
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
65
|
3 |
|
$this->messageFactory = MessageFactoryDiscovery::find(); |
66
|
|
|
|
67
|
3 |
|
$this->plugins[] = new Plugin\AddHostPlugin( |
68
|
3 |
|
UriFactoryDiscovery::find()->createUri($configurator->getBaseUri()) |
69
|
|
|
); |
70
|
3 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* get node |
74
|
|
|
* |
75
|
|
|
* @param string $name |
76
|
|
|
* |
77
|
|
|
* @return \Unikorp\KongAdminApi\NodeInterface |
78
|
|
|
*/ |
79
|
6 |
|
public function getNode(string $name): NodeInterface |
80
|
|
|
{ |
81
|
6 |
|
$node = $this->configurator->getNode($name); |
82
|
|
|
|
83
|
5 |
|
return new $node($this); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* add plugin |
88
|
|
|
* |
89
|
|
|
* @param \Http\Client\Common\Plugin $plugin |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
1 |
|
public function addPlugin(Plugin $plugin): void |
94
|
|
|
{ |
95
|
1 |
|
$this->plugins[] = $plugin; |
96
|
1 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* get http client |
100
|
|
|
* |
101
|
|
|
* @return \Http\Client\Common\HttpMethodsClient |
102
|
|
|
*/ |
103
|
1 |
|
public function getHttpClient(): HttpMethodsClient |
104
|
|
|
{ |
105
|
1 |
|
return new HttpMethodsClient( |
106
|
1 |
|
new PluginClient($this->httpClient, $this->plugins), |
107
|
1 |
|
$this->messageFactory |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|