1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\SiteAlias; |
3
|
|
|
|
4
|
|
|
use Consolidation\Config\Config; |
5
|
|
|
use Consolidation\Config\ConfigInterface; |
6
|
|
|
use Consolidation\Config\Util\ArrayUtil; |
7
|
|
|
use Consolidation\SiteAlias\Util\FsUtils; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* An alias record is a configuration record containing well-known items. |
11
|
|
|
* |
12
|
|
|
* @see AliasRecordInterface for documentation |
13
|
|
|
*/ |
14
|
|
|
class AliasRecord extends Config implements AliasRecordInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @inheritdoc |
23
|
|
|
*/ |
24
|
|
|
public function __construct(array $data = null, $name = '', $env = '') |
25
|
|
|
{ |
26
|
|
|
parent::__construct($data); |
27
|
|
|
if (!empty($env)) { |
28
|
|
|
$name .= ".$env"; |
29
|
|
|
} |
30
|
|
|
$this->name = $name; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function getConfig(ConfigInterface $config, $key, $default = null) |
37
|
|
|
{ |
38
|
|
|
if ($this->has($key)) { |
39
|
|
|
return $this->get($key, $default); |
40
|
|
|
} |
41
|
|
|
return $config->get($key, $default); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
public function name() |
48
|
|
|
{ |
49
|
|
|
return $this->name; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function setName($name) |
56
|
|
|
{ |
57
|
|
|
$this->name = $name; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
|
|
public function hasRoot() |
64
|
|
|
{ |
65
|
|
|
return $this->has('root'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
* |
71
|
|
|
* @throws \Exception when the alias does not specify a root. |
72
|
|
|
*/ |
73
|
|
|
public function root() |
74
|
|
|
{ |
75
|
|
|
if (!$this->hasRoot()) { |
76
|
|
|
throw new \Exception('Site alias ' . $this->name . ' does not specify a root.'); |
77
|
|
|
} |
78
|
|
|
$root = $this->get('root'); |
79
|
|
|
if ($this->isLocal()) { |
80
|
|
|
return FsUtils::realpath($root); |
81
|
|
|
} |
82
|
|
|
return $root; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
|
|
public function uri() |
89
|
|
|
{ |
90
|
|
|
return $this->get('uri'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
|
|
public function setUri($uri) |
97
|
|
|
{ |
98
|
|
|
return $this->set('uri', $uri); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
*/ |
104
|
|
|
public function remoteHostWithUser() |
105
|
|
|
{ |
106
|
|
|
$result = $this->remoteHost(); |
107
|
|
|
if (!empty($result) && $this->hasRemoteUser()) { |
108
|
|
|
$result = $this->remoteUser() . '@' . $result; |
109
|
|
|
} |
110
|
|
|
return $result; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritdoc |
115
|
|
|
*/ |
116
|
|
|
public function remoteUser() |
117
|
|
|
{ |
118
|
|
|
return $this->get('user'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritdoc |
123
|
|
|
*/ |
124
|
|
|
public function hasRemoteUser() |
125
|
|
|
{ |
126
|
|
|
return $this->has('user'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritdoc |
131
|
|
|
*/ |
132
|
|
|
public function remoteHost() |
133
|
|
|
{ |
134
|
|
|
return $this->get('host'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritdoc |
139
|
|
|
*/ |
140
|
|
|
public function isRemote() |
141
|
|
|
{ |
142
|
|
|
return $this->has('host'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @inheritdoc |
147
|
|
|
*/ |
148
|
|
|
public function isLocal() |
149
|
|
|
{ |
150
|
|
|
return !$this->isRemote(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritdoc |
155
|
|
|
*/ |
156
|
|
|
public function isNone() |
157
|
|
|
{ |
158
|
|
|
return empty($this->root()) && $this->isLocal(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritdoc |
163
|
|
|
*/ |
164
|
|
|
public function localRoot() |
165
|
|
|
{ |
166
|
|
|
if ($this->isLocal() && $this->hasRoot()) { |
167
|
|
|
return $this->root(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* os returns the OS that this alias record points to. For local alias |
175
|
|
|
* records, PHP_OS will be returned. For remote alias records, the |
176
|
|
|
* value from the `os` element will be returned. If there is no `os` |
177
|
|
|
* element, then the default assumption is that the remote system is Linux. |
178
|
|
|
* |
179
|
|
|
* @return string |
180
|
|
|
* Linux |
181
|
|
|
* WIN* (e.g. WINNT) |
182
|
|
|
* CYGWIN |
183
|
|
|
* MINGW* (e.g. MINGW32) |
184
|
|
|
*/ |
185
|
|
|
public function os() |
186
|
|
|
{ |
187
|
|
|
if ($this->isLocal()) { |
188
|
|
|
return PHP_OS; |
189
|
|
|
} |
190
|
|
|
return $this->get('os', 'Linux'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @inheritdoc |
195
|
|
|
*/ |
196
|
|
|
public function exportConfig() |
197
|
|
|
{ |
198
|
|
|
return $this->remap($this->export()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Reconfigure data exported from the form it is expected to be in |
203
|
|
|
* inside an alias record to the form it is expected to be in when |
204
|
|
|
* inside a configuration file. |
205
|
|
|
*/ |
206
|
|
|
protected function remap($data) |
207
|
|
|
{ |
208
|
|
|
foreach ($this->remapOptionTable() as $from => $to) { |
209
|
|
|
if (isset($data[$from])) { |
210
|
|
|
unset($data[$from]); |
211
|
|
|
} |
212
|
|
|
$value = $this->get($from, null); |
213
|
|
|
if (isset($value)) { |
214
|
|
|
$data['options'][$to] = $value; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return new Config($data); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Fetch the parameter-specific options from the 'alias-parameters' section of the alias. |
223
|
|
|
* @param string $parameterName |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
protected function getParameterSpecificOptions($aliasData, $parameterName) |
|
|
|
|
227
|
|
|
{ |
228
|
|
|
if (!empty($parameterName) && $this->has("alias-parameters.{$parameterName}")) { |
229
|
|
|
return $this->get("alias-parameters.{$parameterName}"); |
230
|
|
|
} |
231
|
|
|
return []; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Convert the data in this record to the layout that was used |
236
|
|
|
* in the legacy code, for backwards compatiblity. |
237
|
|
|
*/ |
238
|
|
|
public function legacyRecord() |
239
|
|
|
{ |
240
|
|
|
$result = $this->exportConfig()->get('options', []); |
|
|
|
|
241
|
|
|
|
242
|
|
|
// Backend invoke needs a couple of critical items in specific locations. |
243
|
|
|
if ($this->has('paths.drush-script')) { |
244
|
|
|
$result['path-aliases']['%drush-script'] = $this->get('paths.drush-script'); |
245
|
|
|
} |
246
|
|
|
if ($this->has('ssh.options')) { |
247
|
|
|
$result['ssh-options'] = $this->get('ssh.options'); |
248
|
|
|
} |
249
|
|
|
return $result; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Conversion table from old to new option names. These all implicitly |
254
|
|
|
* go in `options`, although they can come from different locations. |
255
|
|
|
*/ |
256
|
|
|
protected function remapOptionTable() |
257
|
|
|
{ |
258
|
|
|
return [ |
259
|
|
|
'user' => 'remote-user', |
260
|
|
|
'host' => 'remote-host', |
261
|
|
|
'root' => 'root', |
262
|
|
|
'uri' => 'uri', |
263
|
|
|
]; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.