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