2 namespace Consolidation\SiteAlias;
4 use Consolidation\Config\Config;
5 use Consolidation\Config\ConfigInterface;
6 use Consolidation\Config\Util\ArrayUtil;
7 use Consolidation\SiteAlias\Util\FsUtils;
10 * An alias record is a configuration record containing well-known items.
12 * @see AliasRecordInterface for documentation
14 class AliasRecord extends Config implements AliasRecordInterface
24 public function __construct(array $data = null, $name = '', $env = '')
26 parent::__construct($data);
36 public function getConfig(ConfigInterface $config, $key, $default = null)
38 if ($this->has($key)) {
39 return $this->get($key, $default);
41 return $config->get($key, $default);
47 public function name()
55 public function setName($name)
63 public function hasRoot()
65 return $this->has('root');
71 public function root()
73 $root = $this->get('root');
74 if ($this->isLocal()) {
75 return FsUtils::realpath($root);
85 return $this->get('uri');
91 public function setUri($uri)
93 return $this->set('uri', $uri);
99 public function remoteHostWithUser()
101 $result = $this->remoteHost();
102 if (!empty($result) && $this->hasRemoteUser()) {
103 $result = $this->remoteUser() . '@' . $result;
111 public function remoteUser()
113 return $this->get('user');
119 public function hasRemoteUser()
121 return $this->has('user');
127 public function remoteHost()
129 return $this->get('host');
135 public function isRemote()
137 return $this->has('host');
143 public function isLocal()
145 return !$this->isRemote();
151 public function isNone()
153 return empty($this->root()) && $this->isLocal();
159 public function localRoot()
161 if ($this->isLocal() && $this->hasRoot()) {
162 return $this->root();
169 * os returns the OS that this alias record points to. For local alias
170 * records, PHP_OS will be returned. For remote alias records, the
171 * value from the `os` element will be returned. If there is no `os`
172 * element, then the default assumption is that the remote system is Linux.
178 * MINGW* (e.g. MINGW32)
182 if ($this->isLocal()) {
185 return $this->get('os', 'Linux');
191 public function exportConfig()
193 return $this->remap($this->export());
197 * Reconfigure data exported from the form it is expected to be in
198 * inside an alias record to the form it is expected to be in when
199 * inside a configuration file.
201 protected function remap($data)
203 foreach ($this->remapOptionTable() as $from => $to) {
204 if (isset($data[$from])) {
207 $value = $this->get($from, null);
209 $data['options'][$to] = $value;
213 return new Config($data);
217 * Fetch the parameter-specific options from the 'alias-parameters' section of the alias.
218 * @param string $parameterName
221 protected function getParameterSpecificOptions($aliasData, $parameterName)
223 if (!empty($parameterName) && $this->has("alias-parameters.{$parameterName}")) {
224 return $this->get("alias-parameters.{$parameterName}");
230 * Convert the data in this record to the layout that was used
231 * in the legacy code, for backwards compatiblity.
233 public function legacyRecord()
235 $result = $this->exportConfig()->get('options', []);
237 // Backend invoke needs a couple of critical items in specific locations.
238 if ($this->has('paths.drush-script')) {
239 $result['path-aliases']['%drush-script'] = $this->get('paths.drush-script');
241 if ($this->has('ssh.options')) {
242 $result['ssh-options'] = $this->get('ssh.options');
248 * Conversion table from old to new option names. These all implicitly
249 * go in `options`, although they can come from different locations.
251 protected function remapOptionTable()
254 'user' => 'remote-user',
255 'host' => 'remote-host',