2 namespace Drush\SiteAlias;
4 use Consolidation\Config\Config;
5 use Consolidation\Config\ConfigInterface;
6 use Consolidation\Config\Util\ArrayUtil;
7 use Drush\Utils\FsUtils;
10 * An alias record is a configuration record containing well-known items.
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).
17 * If using an alias record as config is desired, use the 'exportConfig()'
20 * Example remote alias:
25 * root: /path/to/drupal
28 * Example local alias with global and command-specific options:
31 * root: /path/to/drupal
34 * no-interaction: true
41 class AliasRecord extends Config
49 * AliasRecord constructor
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.
57 public function __construct(array $data = null, $name = '', $env = '')
59 parent::__construct($data);
67 * Get a value from the provided config option. Values stored in
68 * this alias record will override the configuration values, if present.
70 * If multiple alias records need to be chained together in a more
71 * complex priority arrangement, @see \Consolidation\Config\Config\ConfigOverlay.
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
79 public function getConfig(ConfigInterface $config, $key, $default = null)
81 if ($this->has($key)) {
82 return $this->get($key, $default);
84 return $config->get($key, $default);
88 * Return the name of this alias record.
92 public function name()
98 * Remember the name of this record
100 * @param string $name
102 public function setName($name)
108 * Determine whether this alias has a root.
110 public function hasRoot()
112 return $this->has('root');
118 public function root()
120 $root = FsUtils::realpath($this->get('root'));
128 public function uri()
130 return $this->get('uri');
138 public function setUri($uri)
140 return $this->set('uri', $uri);
144 * Return user@host, or just host if there is no user. Returns
145 * an empty string if there is no host.
149 public function remoteHostWithUser()
151 $result = $this->remoteHost();
152 if (!empty($result) && $this->hasRemoteUser()) {
153 $result = $this->remoteUser() . '@' . $result;
159 * Get the remote user
161 public function remoteUser()
163 return $this->get('user');
167 * Return true if this alias record has a remote user
169 public function hasRemoteUser()
171 return $this->has('user');
175 * Get the remote host
177 public function remoteHost()
179 return $this->get('host');
183 * Return true if this alias record has a remote host that is not
186 public function isRemote()
188 return !$this->isLocal();
192 * Return true if this alias record is for the local system
194 public function isLocal()
196 if ($host = $this->remoteHost()) {
197 return $host == 'localhost' || $host == '127.0.0.1';
203 * Determine whether this alias does not represent any site. An
204 * alias record must either be remote or have a root.
206 public function isNone()
208 return empty($this->root()) && $this->isLocal();
212 * Return the 'root' element of this alias if this alias record
215 public function localRoot()
217 if (!$this->isRemote()) {
218 return $this->root();
225 * Export the configuration values in this alias record, and reconfigure
226 * them so that the layout matches that of the global configuration object.
228 public function exportConfig()
230 return $this->remap($this->export());
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.
238 protected function remap($data)
240 foreach ($this->remapOptionTable() as $from => $to) {
241 if (isset($data[$from])) {
244 $value = $this->get($from, null);
246 $data['options'][$to] = $value;
250 return new Config($data);
254 * Fetch the parameter-specific options from the 'alias-parameters' section of the alias.
255 * @param string $parameterName
258 protected function getParameterSpecificOptions($aliasData, $parameterName)
260 if (!empty($parameterName) && $this->has("alias-parameters.{$parameterName}")) {
261 return $this->get("alias-parameters.{$parameterName}");
267 * Convert the data in this record to the layout that was used
268 * in the legacy code, for backwards compatiblity.
270 public function legacyRecord()
272 $result = $this->exportConfig()->get('options', []);
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');
278 if ($this->has('ssh.options')) {
279 $result['ssh-options'] = $this->get('ssh.options');
285 * Conversion table from old to new option names. These all implicitly
286 * go in `options`, although they can come from different locations.
288 protected function remapOptionTable()
291 'user' => 'remote-user',
292 'host' => 'remote-host',