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 Drush\Utils\FsUtils; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* An alias record is a configuration record containing well-known items. |
11
|
|
|
* |
12
|
|
|
* NOTE: AliasRecord is implemented as a Config subclass; however, it |
13
|
|
|
* should not be used as a config. (A better implementation would be |
14
|
|
|
* "hasa" config, but that is less convenient, as we want all of the |
15
|
|
|
* same capabilities as a config object). |
16
|
|
|
* |
17
|
|
|
* If using an alias record as config is desired, use the 'exportConfig()' |
18
|
|
|
* method. |
19
|
|
|
* |
20
|
|
|
* Example remote alias: |
21
|
|
|
* |
22
|
|
|
* --- |
23
|
|
|
* host: www.myisp.org |
24
|
|
|
* user: www-data |
25
|
|
|
* root: /path/to/drupal |
26
|
|
|
* uri: mysite.org |
27
|
|
|
* |
28
|
|
|
* Example local alias with global and command-specific options: |
29
|
|
|
* |
30
|
|
|
* --- |
31
|
|
|
* root: /path/to/drupal |
32
|
|
|
* uri: mysite.org |
33
|
|
|
* options: |
34
|
|
|
* no-interaction: true |
35
|
|
|
* command: |
36
|
|
|
* user: |
37
|
|
|
* login: |
38
|
|
|
* options: |
39
|
|
|
* name: superuser |
40
|
|
|
*/ |
41
|
|
|
class AliasRecord extends Config |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $name; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* AliasRecord constructor |
50
|
|
|
* |
51
|
|
|
* @param array|null $data Initial data for alias record |
52
|
|
|
* @param string $name Alias name or site specification for this alias record |
53
|
|
|
* @param string $env Environment for this alias record. Will be appended to |
54
|
|
|
* the alias name, separated by a "." if provided. |
55
|
|
|
* @return type |
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
public function __construct(array $data = null, $name = '', $env = '') |
58
|
|
|
{ |
59
|
|
|
parent::__construct($data); |
60
|
|
|
if (!empty($env)) { |
61
|
|
|
$name .= ".$env"; |
62
|
|
|
} |
63
|
|
|
$this->name = $name; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get a value from the provided config option. Values stored in |
68
|
|
|
* this alias record will override the configuration values, if present. |
69
|
|
|
* |
70
|
|
|
* If multiple alias records need to be chained together in a more |
71
|
|
|
* complex priority arrangement, @see \Consolidation\Config\Config\ConfigOverlay. |
72
|
|
|
* |
73
|
|
|
* @param ConfigInterface $config The configuration object to pull fallback data from |
74
|
|
|
* @param string $key The data item to fetch |
75
|
|
|
* @param mixed $default The default value to return if there is no match |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getConfig(ConfigInterface $config, $key, $default = null) |
80
|
|
|
{ |
81
|
|
|
if ($this->has($key)) { |
82
|
|
|
return $this->get($key, $default); |
83
|
|
|
} |
84
|
|
|
return $config->get($key, $default); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return the name of this alias record. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function name() |
93
|
|
|
{ |
94
|
|
|
return $this->name; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Remember the name of this record |
99
|
|
|
* |
100
|
|
|
* @param string $name |
101
|
|
|
*/ |
102
|
|
|
public function setName($name) |
103
|
|
|
{ |
104
|
|
|
$this->name = $name; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Determine whether this alias has a root. |
109
|
|
|
*/ |
110
|
|
|
public function hasRoot() |
111
|
|
|
{ |
112
|
|
|
return $this->has('root'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the root |
117
|
|
|
*/ |
118
|
|
|
public function root() |
119
|
|
|
{ |
120
|
|
|
$root = FsUtils::realpath($this->get('root')); |
121
|
|
|
|
122
|
|
|
return $root; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the uri |
127
|
|
|
*/ |
128
|
|
|
public function uri() |
129
|
|
|
{ |
130
|
|
|
return $this->get('uri'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Record the uri |
135
|
|
|
* |
136
|
|
|
* @param string $uri |
137
|
|
|
*/ |
138
|
|
|
public function setUri($uri) |
139
|
|
|
{ |
140
|
|
|
return $this->set('uri', $uri); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Return user@host, or just host if there is no user. Returns |
145
|
|
|
* an empty string if there is no host. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function remoteHostWithUser() |
150
|
|
|
{ |
151
|
|
|
$result = $this->remoteHost(); |
152
|
|
|
if (!empty($result) && $this->hasRemoteUser()) { |
153
|
|
|
$result = $this->remoteUser() . '@' . $result; |
154
|
|
|
} |
155
|
|
|
return $result; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the remote user |
160
|
|
|
*/ |
161
|
|
|
public function remoteUser() |
162
|
|
|
{ |
163
|
|
|
return $this->get('user'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Return true if this alias record has a remote user |
168
|
|
|
*/ |
169
|
|
|
public function hasRemoteUser() |
170
|
|
|
{ |
171
|
|
|
return $this->has('user'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the remote host |
176
|
|
|
*/ |
177
|
|
|
public function remoteHost() |
178
|
|
|
{ |
179
|
|
|
return $this->get('host'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Return true if this alias record has a remote host that is not |
184
|
|
|
* the local host |
185
|
|
|
*/ |
186
|
|
|
public function isRemote() |
187
|
|
|
{ |
188
|
|
|
return !$this->isLocal(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Return true if this alias record is for the local system |
193
|
|
|
*/ |
194
|
|
|
public function isLocal() |
195
|
|
|
{ |
196
|
|
|
if ($host = $this->remoteHost()) { |
197
|
|
|
return $host == 'localhost' || $host == '127.0.0.1'; |
198
|
|
|
} |
199
|
|
|
return true; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Determine whether this alias does not represent any site. An |
204
|
|
|
* alias record must either be remote or have a root. |
205
|
|
|
*/ |
206
|
|
|
public function isNone() |
207
|
|
|
{ |
208
|
|
|
return empty($this->root()) && $this->isLocal(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Return the 'root' element of this alias if this alias record |
213
|
|
|
* is local. |
214
|
|
|
*/ |
215
|
|
|
public function localRoot() |
216
|
|
|
{ |
217
|
|
|
if (!$this->isRemote()) { |
218
|
|
|
return $this->root(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Export the configuration values in this alias record, and reconfigure |
226
|
|
|
* them so that the layout matches that of the global configuration object. |
227
|
|
|
*/ |
228
|
|
|
public function exportConfig() |
229
|
|
|
{ |
230
|
|
|
return $this->remap($this->export()); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Reconfigure data exported from the form it is expected to be in |
235
|
|
|
* inside an alias record to the form it is expected to be in when |
236
|
|
|
* inside a configuration file. |
237
|
|
|
*/ |
238
|
|
|
protected function remap($data) |
239
|
|
|
{ |
240
|
|
|
foreach ($this->remapOptionTable() as $from => $to) { |
241
|
|
|
if (isset($data[$from])) { |
242
|
|
|
unset($data[$from]); |
243
|
|
|
} |
244
|
|
|
$value = $this->get($from, null); |
245
|
|
|
if (isset($value)) { |
246
|
|
|
$data['options'][$to] = $value; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return new Config($data); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Fetch the parameter-specific options from the 'alias-parameters' section of the alias. |
255
|
|
|
* @param string $parameterName |
256
|
|
|
* @return array |
257
|
|
|
*/ |
258
|
|
|
protected function getParameterSpecificOptions($aliasData, $parameterName) |
|
|
|
|
259
|
|
|
{ |
260
|
|
|
if (!empty($parameterName) && $this->has("alias-parameters.{$parameterName}")) { |
261
|
|
|
return $this->get("alias-parameters.{$parameterName}"); |
262
|
|
|
} |
263
|
|
|
return []; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Convert the data in this record to the layout that was used |
268
|
|
|
* in the legacy code, for backwards compatiblity. |
269
|
|
|
*/ |
270
|
|
|
public function legacyRecord() |
271
|
|
|
{ |
272
|
|
|
$result = $this->exportConfig()->get('options', []); |
|
|
|
|
273
|
|
|
|
274
|
|
|
// Backend invoke needs a couple of critical items in specific locations. |
275
|
|
|
if ($this->has('paths.drush-script')) { |
276
|
|
|
$result['path-aliases']['%drush-script'] = $this->get('paths.drush-script'); |
277
|
|
|
} |
278
|
|
|
if ($this->has('ssh.options')) { |
279
|
|
|
$result['ssh-options'] = $this->get('ssh.options'); |
280
|
|
|
} |
281
|
|
|
return $result; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Conversion table from old to new option names. These all implicitly |
286
|
|
|
* go in `options`, although they can come from different locations. |
287
|
|
|
*/ |
288
|
|
|
protected function remapOptionTable() |
289
|
|
|
{ |
290
|
|
|
return [ |
291
|
|
|
'user' => 'remote-user', |
292
|
|
|
'host' => 'remote-host', |
293
|
|
|
'root' => 'root', |
294
|
|
|
'uri' => 'uri', |
295
|
|
|
]; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.