1
|
|
|
<?php |
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\Server; |
9
|
|
|
|
10
|
|
|
use Deployer\Deployer; |
11
|
|
|
use Deployer\Type\DotArray; |
12
|
|
|
|
13
|
|
|
class Environment |
14
|
|
|
{ |
15
|
|
|
const DEPLOY_PATH = 'deploy_path'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Array of set values. |
19
|
|
|
* @var \Deployer\Type\DotArray |
20
|
|
|
*/ |
21
|
|
|
private $values = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Values represented by their keys here are protected, and cannot be |
25
|
|
|
* changed by calling the `set` method. |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $protectedNames = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor |
32
|
|
|
*/ |
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->values = new DotArray(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
53 |
|
* @param string $name |
40
|
|
|
* @param bool|int|string|array $value |
41
|
53 |
|
*/ |
42
|
53 |
|
public function set($name, $value) |
43
|
|
|
{ |
44
|
|
|
$this->checkIfNameIsProtected($name); |
45
|
|
|
$this->values[$name] = $value; |
46
|
|
|
} |
47
|
|
|
|
48
|
37 |
|
/** |
49
|
|
|
* @param string $name |
50
|
37 |
|
* @param bool|int|string|array $value |
51
|
37 |
|
*/ |
52
|
37 |
|
public function setAsProtected($name, $value) |
53
|
|
|
{ |
54
|
|
|
$this->set($name, $value); |
55
|
|
|
$this->protectedNames[] = $name; |
56
|
|
|
} |
57
|
|
|
|
58
|
25 |
|
/** |
59
|
|
|
* Checks whether the given name was registered as protected, or if there is |
60
|
25 |
|
* a protected parameter which would be overwritten. |
61
|
25 |
|
* @param string $name |
62
|
25 |
|
* @throws \RuntimeException if the value already exists and is protected. |
63
|
|
|
* @throws \RuntimeException if there's a protected parameter which would |
64
|
|
|
* be overwritten. |
65
|
|
|
*/ |
66
|
|
|
private function checkIfNameIsProtected($name) |
67
|
|
|
{ |
68
|
|
|
$length = strlen($name); |
69
|
|
|
|
70
|
|
|
foreach ($this->protectedNames as $protectedName) { |
71
|
|
|
$len = strlen($protectedName); |
72
|
37 |
|
if ($name === $protectedName) { |
73
|
|
|
throw new \RuntimeException("The parameter `$name` cannot be set, because it's protected."); |
74
|
37 |
|
} elseif ($len < $length && '.' === $name[$len] && 0 === strpos($name, $protectedName)) { |
75
|
|
|
throw new \RuntimeException("The parameter `$name` cannot be set, because `$protectedName` is protected."); |
76
|
37 |
|
} elseif ($len > $length && '.' === $protectedName[$length] && 0 === strpos($protectedName, $name)) { |
77
|
21 |
|
throw new \RuntimeException("The parameter `$name` could not be set, because a protected parameter named `$protectedName` already exists."); |
78
|
21 |
|
} |
79
|
2 |
|
} |
80
|
19 |
|
} |
81
|
2 |
|
|
82
|
17 |
|
/** |
83
|
1 |
|
* @param string $name |
84
|
|
|
* @param array $array |
85
|
37 |
|
*/ |
86
|
37 |
|
public function add($name, $array) |
87
|
|
|
{ |
88
|
|
|
if ($this->has($name)) { |
89
|
|
|
$config = $this->get($name); |
90
|
|
|
if (!is_array($config)) { |
91
|
|
|
throw new \RuntimeException("Configuration parameter `$name` isn't array."); |
92
|
|
|
} |
93
|
|
|
$this->set($name, array_merge($config, $array)); |
94
|
24 |
|
} else { |
95
|
|
|
$this->set($name, $array); |
96
|
24 |
|
} |
97
|
22 |
|
} |
98
|
22 |
|
|
99
|
18 |
|
/** |
100
|
11 |
|
* @param string $name |
101
|
11 |
|
* @param bool|int|string|array $default |
102
|
11 |
|
* @return bool|int|string|array |
103
|
5 |
|
* @throws \RuntimeException |
104
|
|
|
*/ |
105
|
11 |
|
public function get($name, $default = null) |
106
|
18 |
|
{ |
107
|
1 |
|
if ($this->values->hasKey($name)) { |
108
|
|
|
if ($this->isClosure($this->values[$name])) { |
109
|
18 |
|
$value = $this->values[$name] = call_user_func($this->values[$name]); |
110
|
|
|
} else { |
111
|
|
|
$value = $this->values[$name]; |
112
|
|
|
} |
113
|
|
|
} else { |
114
|
24 |
|
$config = Deployer::get()->config; |
115
|
|
|
|
116
|
|
|
if (isset($config[$name])) { |
117
|
|
|
if ($this->isClosure($config[$name])) { |
118
|
|
|
$value = $this->values[$name] = call_user_func($config[$name]); |
119
|
|
|
} else { |
120
|
|
|
$value = $this->values[$name] = $config[$name]; |
121
|
|
|
} |
122
|
|
|
} else { |
123
|
16 |
|
if (null === $default) { |
124
|
|
|
throw new \RuntimeException("Configuration parameter `$name` does not exists."); |
125
|
16 |
|
} else { |
126
|
|
|
$value = $default; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->parse($value); |
132
|
2 |
|
} |
133
|
|
|
|
134
|
2 |
|
/** |
135
|
|
|
* Checks if set var exists. |
136
|
|
|
* |
137
|
2 |
|
* @param string $name |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function has($name) |
141
|
|
|
{ |
142
|
|
|
return $this->values->hasKey($name); |
143
|
|
|
} |
144
|
15 |
|
|
145
|
|
|
/** |
146
|
15 |
|
* Parse set values. |
147
|
1 |
|
* |
148
|
1 |
|
* @param string $value |
149
|
15 |
|
* @return string |
150
|
15 |
|
*/ |
151
|
|
|
public function parse($value) |
152
|
|
|
{ |
153
|
|
|
if (is_string($value)) { |
154
|
|
|
$value = preg_replace_callback('/\{\{\s*([\w\.\/]+)\s*\}\}/', [$this, 'parseCallback'], $value); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $value; |
158
|
25 |
|
} |
159
|
|
|
|
160
|
25 |
|
/** |
161
|
22 |
|
* Replace set values callback for parse |
162
|
22 |
|
* |
163
|
|
|
* @param array $matches |
164
|
25 |
|
* @return mixed |
165
|
|
|
*/ |
166
|
|
|
private function parseCallback($matches) |
167
|
|
|
{ |
168
|
|
|
return isset($matches[1]) ? $this->get($matches[1]) : null; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param mixed $t |
173
|
16 |
|
* @return bool |
174
|
|
|
*/ |
175
|
16 |
|
private function isClosure($t) { |
176
|
|
|
return is_object($t) && ($t instanceof \Closure); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|