Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / consolidation / site-alias / src / AliasRecord.php
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     public function root()
72     {
73         $root = $this->get('root');
74         if ($this->isLocal()) {
75             return FsUtils::realpath($root);
76         }
77         return $root;
78     }
79
80     /**
81      * @inheritdoc
82      */
83     public function uri()
84     {
85         return $this->get('uri');
86     }
87
88     /**
89      * @inheritdoc
90      */
91     public function setUri($uri)
92     {
93         return $this->set('uri', $uri);
94     }
95
96     /**
97      * @inheritdoc
98      */
99     public function remoteHostWithUser()
100     {
101         $result = $this->remoteHost();
102         if (!empty($result) && $this->hasRemoteUser()) {
103             $result = $this->remoteUser() . '@' . $result;
104         }
105         return $result;
106     }
107
108     /**
109      * @inheritdoc
110      */
111     public function remoteUser()
112     {
113         return $this->get('user');
114     }
115
116     /**
117      * @inheritdoc
118      */
119     public function hasRemoteUser()
120     {
121         return $this->has('user');
122     }
123
124     /**
125      * @inheritdoc
126      */
127     public function remoteHost()
128     {
129         return $this->get('host');
130     }
131
132     /**
133      * @inheritdoc
134      */
135     public function isRemote()
136     {
137         return $this->has('host');
138     }
139
140     /**
141      * @inheritdoc
142      */
143     public function isLocal()
144     {
145         return !$this->isRemote();
146     }
147
148     /**
149      * @inheritdoc
150      */
151     public function isNone()
152     {
153         return empty($this->root()) && $this->isLocal();
154     }
155
156     /**
157      * @inheritdoc
158      */
159     public function localRoot()
160     {
161         if ($this->isLocal() && $this->hasRoot()) {
162             return $this->root();
163         }
164
165         return false;
166     }
167
168     /**
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.
173      *
174      * @return string
175      *   Linux
176      *   WIN* (e.g. WINNT)
177      *   CYGWIN
178      *   MINGW* (e.g. MINGW32)
179      */
180     public function os()
181     {
182         if ($this->isLocal()) {
183             return PHP_OS;
184         }
185         return $this->get('os', 'Linux');
186     }
187
188     /**
189      * @inheritdoc
190      */
191     public function exportConfig()
192     {
193         return $this->remap($this->export());
194     }
195
196     /**
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.
200      */
201     protected function remap($data)
202     {
203         foreach ($this->remapOptionTable() as $from => $to) {
204             if (isset($data[$from])) {
205                 unset($data[$from]);
206             }
207             $value = $this->get($from, null);
208             if (isset($value)) {
209                 $data['options'][$to] = $value;
210             }
211         }
212
213         return new Config($data);
214     }
215
216     /**
217      * Fetch the parameter-specific options from the 'alias-parameters' section of the alias.
218      * @param string $parameterName
219      * @return array
220      */
221     protected function getParameterSpecificOptions($aliasData, $parameterName)
222     {
223         if (!empty($parameterName) && $this->has("alias-parameters.{$parameterName}")) {
224             return $this->get("alias-parameters.{$parameterName}");
225         }
226         return [];
227     }
228
229     /**
230      * Convert the data in this record to the layout that was used
231      * in the legacy code, for backwards compatiblity.
232      */
233     public function legacyRecord()
234     {
235         $result = $this->exportConfig()->get('options', []);
236
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');
240         }
241         if ($this->has('ssh.options')) {
242             $result['ssh-options'] = $this->get('ssh.options');
243         }
244         return $result;
245     }
246
247     /**
248      * Conversion table from old to new option names. These all implicitly
249      * go in `options`, although they can come from different locations.
250      */
251     protected function remapOptionTable()
252     {
253         return [
254             'user' => 'remote-user',
255             'host' => 'remote-host',
256             'root' => 'root',
257             'uri' => 'uri',
258         ];
259     }
260 }