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