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 Unikorp\KongAdminApi\Node; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* configurator |
18
|
|
|
* |
19
|
|
|
* @author VEBER Arnaud <https://github.com/VEBERArnaud> |
20
|
|
|
*/ |
21
|
|
|
class Configurator implements ConfiguratorInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* base uri |
25
|
|
|
* @var string $baseUri |
26
|
|
|
*/ |
27
|
|
|
private $baseUri = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* headers |
31
|
|
|
* @var array $headers |
32
|
|
|
*/ |
33
|
|
|
private $headers = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* nodes |
37
|
|
|
* @var array $nodes |
38
|
|
|
*/ |
39
|
|
|
private $nodes = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* construct |
43
|
|
|
*/ |
44
|
1 |
|
public function __construct() |
45
|
|
|
{ |
46
|
1 |
|
$this->nodes = [ |
47
|
|
|
'api' => Node\Api::class, |
48
|
|
|
'cluster' => Node\Cluster::class, |
49
|
|
|
'consumer' => Node\Consumer::class, |
50
|
|
|
'information' => Node\Information::class, |
51
|
|
|
'plugin' => Node\Plugin::class, |
52
|
|
|
]; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* set base uri |
57
|
|
|
* |
58
|
|
|
* @param string $baseUri |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
1 |
|
public function setBaseUri(string $baseUri): void |
63
|
|
|
{ |
64
|
1 |
|
$this->baseUri = $baseUri; |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* get base uri |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
1 |
|
public function getBaseUri(): string |
73
|
|
|
{ |
74
|
1 |
|
return $this->baseUri; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* set headers |
79
|
|
|
* |
80
|
|
|
* @param array $headers |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
1 |
|
public function setHeaders(array $headers): void |
85
|
|
|
{ |
86
|
1 |
|
$this->headers = $headers; |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* get headers |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
1 |
|
public function getHeaders(): array |
95
|
|
|
{ |
96
|
1 |
|
return $this->headers; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* add node |
101
|
|
|
* |
102
|
|
|
* @param string $name |
103
|
|
|
* @param string $class |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
* |
107
|
|
|
* @throws \RuntimeException |
108
|
|
|
* @throws \InvalidArgumentException |
109
|
|
|
*/ |
110
|
3 |
|
public function addNode(string $name, string $class): void |
111
|
|
|
{ |
112
|
3 |
|
if (!empty($this->nodes[$name])) { |
113
|
1 |
|
throw new \RuntimeException(sprintf('Node for key `%1$s` already exists', $name)); |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
if (!class_exists($class)) { |
117
|
1 |
|
throw new \RuntimeException(sprintf('Node class `%1$s` does not exists', $class)); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
$this->nodes[$name] = $class; |
121
|
1 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* remove node |
125
|
|
|
* |
126
|
|
|
* @param string $name |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
1 |
|
public function removeNode(string $name): void |
131
|
|
|
{ |
132
|
1 |
|
if (isset($this->nodes[$name])) { |
133
|
1 |
|
unset($this->nodes[$name]); |
134
|
|
|
} |
135
|
1 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* get node |
139
|
|
|
* |
140
|
|
|
* @param string $name |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
* |
144
|
|
|
* @throws \RuntimeException |
145
|
|
|
*/ |
146
|
2 |
|
public function getNode(string $name): string |
147
|
|
|
{ |
148
|
2 |
|
if (empty($this->nodes[$name])) { |
149
|
1 |
|
throw new \RuntimeException(sprintf('Node for key `%1$s` does not exists', $name)); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
return $this->nodes[$name]; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|