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\ApiNode::class, |
48
|
|
|
'certificate' => Node\CertificateNode::class, |
49
|
|
|
'cluster' => Node\ClusterNode::class, |
50
|
|
|
'consumer' => Node\ConsumerNode::class, |
51
|
|
|
'information' => Node\InformationNode::class, |
52
|
|
|
'plugin' => Node\PluginNode::class, |
53
|
|
|
'sni' => Node\SniNode::class, |
54
|
|
|
'target' => Node\TargetNode::class, |
55
|
|
|
'upstream' => Node\UpstreamNode::class, |
56
|
|
|
]; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* set base uri |
61
|
|
|
* |
62
|
|
|
* @param string $baseUri |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
1 |
|
public function setBaseUri(string $baseUri): void |
67
|
|
|
{ |
68
|
1 |
|
$this->baseUri = $baseUri; |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* get base uri |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
1 |
|
public function getBaseUri(): string |
77
|
|
|
{ |
78
|
1 |
|
return $this->baseUri; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* set headers |
83
|
|
|
* |
84
|
|
|
* @param array $headers |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
1 |
|
public function setHeaders(array $headers): void |
89
|
|
|
{ |
90
|
1 |
|
$this->headers = $headers; |
91
|
1 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* get headers |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
1 |
|
public function getHeaders(): array |
99
|
|
|
{ |
100
|
1 |
|
return $this->headers; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* add node |
105
|
|
|
* |
106
|
|
|
* @param string $name |
107
|
|
|
* @param string $class |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
* |
111
|
|
|
* @throws \RuntimeException |
112
|
|
|
* @throws \InvalidArgumentException |
113
|
|
|
*/ |
114
|
3 |
|
public function addNode(string $name, string $class): void |
115
|
|
|
{ |
116
|
3 |
|
if (!empty($this->nodes[$name])) { |
117
|
1 |
|
throw new \RuntimeException(sprintf('Node for key `%1$s` already exists', $name)); |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
if (!class_exists($class)) { |
121
|
1 |
|
throw new \RuntimeException(sprintf('Node class `%1$s` does not exists', $class)); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
$this->nodes[$name] = $class; |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* remove node |
129
|
|
|
* |
130
|
|
|
* @param string $name |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
1 |
|
public function removeNode(string $name): void |
135
|
|
|
{ |
136
|
1 |
|
if (isset($this->nodes[$name])) { |
137
|
1 |
|
unset($this->nodes[$name]); |
138
|
|
|
} |
139
|
1 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* get node |
143
|
|
|
* |
144
|
|
|
* @param string $name |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
* |
148
|
|
|
* @throws \RuntimeException |
149
|
|
|
*/ |
150
|
2 |
|
public function getNode(string $name): string |
151
|
|
|
{ |
152
|
2 |
|
if (empty($this->nodes[$name])) { |
153
|
1 |
|
throw new \RuntimeException(sprintf('Node for key `%1$s` does not exists', $name)); |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
return $this->nodes[$name]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|