1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* (c) Anton Medvedev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Deployer; |
12
|
|
|
|
13
|
|
|
use Deployer\Exception\ConfigurationException; |
14
|
|
|
use Deployer\Utility\Httpie; |
15
|
|
|
|
16
|
|
|
use function Deployer\Support\array_merge_alternate; |
17
|
|
|
use function Deployer\Support\is_closure; |
18
|
|
|
use function Deployer\Support\normalize_line_endings; |
19
|
|
|
|
20
|
|
|
class Configuration implements \ArrayAccess |
21
|
|
|
{ |
22
|
|
|
private ?Configuration $parent; |
23
|
|
|
private array $values = []; |
24
|
|
|
|
25
|
|
|
public function __construct(?Configuration $parent = null) |
26
|
|
|
{ |
27
|
|
|
$this->parent = $parent; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function update(array $values): void |
31
|
|
|
{ |
32
|
|
|
$this->values = array_merge($this->values, $values); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function bind(Configuration $parent): void |
36
|
|
|
{ |
37
|
|
|
$this->parent = $parent; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function set(string $name, mixed $value): void |
41
|
|
|
{ |
42
|
|
|
$this->values[$name] = $value; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function has(string $name): bool |
46
|
|
|
{ |
47
|
|
|
$ok = array_key_exists($name, $this->values); |
48
|
|
|
if ($ok) { |
49
|
|
|
return true; |
50
|
|
|
} |
51
|
|
|
if ($this->parent) { |
52
|
|
|
return $this->parent->has($name); |
53
|
|
|
} |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function hasOwn(string $name): bool |
58
|
|
|
{ |
59
|
|
|
return array_key_exists($name, $this->values); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function add(string $name, array $array): void |
63
|
|
|
{ |
64
|
|
|
if ($this->has($name)) { |
65
|
|
|
$config = $this->get($name); |
66
|
|
|
if (!is_array($config)) { |
67
|
|
|
throw new ConfigurationException("Config option \"$name\" isn't array."); |
68
|
|
|
} |
69
|
|
|
$this->set($name, array_merge_alternate($config, $array)); |
70
|
|
|
} else { |
71
|
|
|
$this->set($name, $array); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function get(string $name, mixed $default = null): mixed |
76
|
|
|
{ |
77
|
|
|
if (array_key_exists($name, $this->values)) { |
78
|
|
|
if (is_closure($this->values[$name])) { |
79
|
|
|
return $this->values[$name] = $this->parse(call_user_func($this->values[$name])); |
80
|
|
|
} else { |
81
|
|
|
return $this->parse($this->values[$name]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($this->parent) { |
86
|
|
|
$rawValue = $this->parent->fetch($name); |
87
|
|
|
if ($rawValue !== null) { |
88
|
|
|
if (is_closure($rawValue)) { |
89
|
|
|
return $this->values[$name] = $this->parse(call_user_func($rawValue)); |
90
|
|
|
} else { |
91
|
|
|
return $this->values[$name] = $this->parse($rawValue); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (func_num_args() >= 2) { |
97
|
|
|
return $this->parse($default); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
throw new ConfigurationException("Config option \"$name\" does not exist."); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function fetch(string $name): mixed |
104
|
|
|
{ |
105
|
|
|
if (array_key_exists($name, $this->values)) { |
106
|
|
|
return $this->values[$name]; |
107
|
|
|
} |
108
|
|
|
if ($this->parent) { |
109
|
|
|
return $this->parent->fetch($name); |
110
|
|
|
} |
111
|
|
|
return null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function parse(mixed $value): mixed |
115
|
|
|
{ |
116
|
|
|
if (is_string($value)) { |
117
|
|
|
$normalizedValue = normalize_line_endings($value); |
118
|
|
|
return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*\}\}/', function (array $matches) { |
119
|
|
|
return $this->get($matches[1]); |
120
|
|
|
}, $normalizedValue); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $value; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function keys(): array |
127
|
|
|
{ |
128
|
|
|
return array_keys($this->values); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $offset |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
#[\ReturnTypeWillChange] |
136
|
|
|
public function offsetExists($offset) |
137
|
|
|
{ |
138
|
|
|
return $this->has($offset); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $offset |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
|
|
#[\ReturnTypeWillChange] |
146
|
|
|
public function offsetGet($offset) |
147
|
|
|
{ |
148
|
|
|
return $this->get($offset); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $offset |
153
|
|
|
* @param mixed $value |
154
|
|
|
*/ |
155
|
|
|
#[\ReturnTypeWillChange] |
156
|
|
|
public function offsetSet($offset, $value): void |
157
|
|
|
{ |
158
|
|
|
$this->set($offset, $value); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param mixed $offset |
163
|
|
|
*/ |
164
|
|
|
#[\ReturnTypeWillChange] |
165
|
|
|
public function offsetUnset($offset): void |
166
|
|
|
{ |
167
|
|
|
unset($this->values[$offset]); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function load(): void |
171
|
|
|
{ |
172
|
|
|
if (!Deployer::isWorker()) { |
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$values = Httpie::get(MASTER_ENDPOINT . '/load') |
177
|
|
|
->setopt(CURLOPT_CONNECTTIMEOUT, 0) |
178
|
|
|
->setopt(CURLOPT_TIMEOUT, 0) |
179
|
|
|
->jsonBody([ |
180
|
|
|
'host' => $this->get('alias'), |
181
|
|
|
]) |
182
|
|
|
->getJson(); |
183
|
|
|
$this->update($values); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function save(): void |
187
|
|
|
{ |
188
|
|
|
if (!Deployer::isWorker()) { |
189
|
|
|
return; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
Httpie::get(MASTER_ENDPOINT . '/save') |
193
|
|
|
->setopt(CURLOPT_CONNECTTIMEOUT, 0) |
194
|
|
|
->setopt(CURLOPT_TIMEOUT, 0) |
195
|
|
|
->jsonBody([ |
196
|
|
|
'host' => $this->get('alias'), |
197
|
|
|
'config' => $this->persist(), |
198
|
|
|
]) |
199
|
|
|
->getJson(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function persist(): array |
203
|
|
|
{ |
204
|
|
|
$values = []; |
205
|
|
|
foreach ($this->values as $key => $value) { |
206
|
|
|
if (is_closure($value)) { |
207
|
|
|
continue; |
208
|
|
|
} |
209
|
|
|
$values[$key] = $value; |
210
|
|
|
} |
211
|
|
|
return $values; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|