/
Platform.php
261 lines (224 loc) · 7.63 KB
/
Platform.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Util;
use Composer\Pcre\Preg;
/**
* Platform helper for uniform platform-specific tests.
*
* @author Niels Keurentjes <niels.keurentjes@omines.com>
*/
class Platform
{
/** @var ?bool */
private static $isVirtualBoxGuest = null;
/** @var ?bool */
private static $isWindowsSubsystemForLinux = null;
/**
* getenv() equivalent but reads from the runtime global variables first
*
* @param string $name
* @return string|false
*/
public static function getEnv($name)
{
if (array_key_exists($name, $_SERVER)) {
return (string) $_SERVER[$name];
}
if (array_key_exists($name, $_ENV)) {
return (string) $_ENV[$name];
}
return getenv($name);
}
/**
* putenv() equivalent but updates the runtime global variables too
*
* @param string $name
* @param string $value
* @return void
*/
public static function putEnv($name, $value)
{
$value = (string) $value;
putenv($name . '=' . $value);
$_SERVER[$name] = $_ENV[$name] = $value;
}
/**
* putenv('X') equivalent but updates the runtime global variables too
*
* @param string $name
* @return void
*/
public static function clearEnv($name)
{
putenv($name);
unset($_SERVER[$name], $_ENV[$name]);
}
/**
* Parses tildes and environment variables in paths.
*
* @param string $path
* @return string
*/
public static function expandPath($path)
{
if (Preg::isMatch('#^~[\\/]#', $path)) {
return self::getUserDirectory() . substr($path, 1);
}
return Preg::replaceCallback('#^(\$|(?P<percent>%))(?P<var>\w++)(?(percent)%)(?P<path>.*)#', function ($matches) {
// Treat HOME as an alias for USERPROFILE on Windows for legacy reasons
if (Platform::isWindows() && $matches['var'] == 'HOME') {
return (Platform::getEnv('HOME') ?: Platform::getEnv('USERPROFILE')) . $matches['path'];
}
return Platform::getEnv($matches['var']) . $matches['path'];
}, $path);
}
/**
* @throws \RuntimeException If the user home could not reliably be determined
* @return string The formal user home as detected from environment parameters
*/
public static function getUserDirectory()
{
if (false !== ($home = self::getEnv('HOME'))) {
return $home;
}
if (self::isWindows() && false !== ($home = self::getEnv('USERPROFILE'))) {
return $home;
}
if (\function_exists('posix_getuid') && \function_exists('posix_getpwuid')) {
$info = posix_getpwuid(posix_getuid());
return $info['dir'];
}
throw new \RuntimeException('Could not determine user directory');
}
/**
* @return bool Whether the host machine is running on the Windows Subsystem for Linux (WSL)
*/
public static function isWindowsSubsystemForLinux()
{
if (null === self::$isWindowsSubsystemForLinux) {
self::$isWindowsSubsystemForLinux = false;
// while WSL will be hosted within windows, WSL itself cannot be windows based itself.
if (self::isWindows()) {
return self::$isWindowsSubsystemForLinux = false;
}
if (
!ini_get('open_basedir')
&& is_readable('/proc/version')
&& false !== stripos(Silencer::call('file_get_contents', '/proc/version'), 'microsoft')
&& !file_exists('/.dockerenv') // docker running inside WSL should not be seen as WSL
) {
return self::$isWindowsSubsystemForLinux = true;
}
}
return self::$isWindowsSubsystemForLinux;
}
/**
* @return bool Whether the host machine is running a Windows OS
*/
public static function isWindows()
{
return \defined('PHP_WINDOWS_VERSION_BUILD');
}
/**
* @param string $str
* @return int return a guaranteed binary length of the string, regardless of silly mbstring configs
*/
public static function strlen($str)
{
static $useMbString = null;
if (null === $useMbString) {
$useMbString = \function_exists('mb_strlen') && ini_get('mbstring.func_overload');
}
if ($useMbString) {
return mb_strlen($str, '8bit');
}
return \strlen($str);
}
/**
* @param ?resource $fd Open file descriptor or null to default to STDOUT
* @return bool
*/
public static function isTty($fd = null)
{
if ($fd === null) {
$fd = defined('STDOUT') ? STDOUT : fopen('php://stdout', 'w');
}
// detect msysgit/mingw and assume this is a tty because detection
// does not work correctly, see https://github.com/composer/composer/issues/9690
if (in_array(strtoupper(self::getEnv('MSYSTEM') ?: ''), array('MINGW32', 'MINGW64'), true)) {
return true;
}
// modern cross-platform function, includes the fstat
// fallback so if it is present we trust it
if (function_exists('stream_isatty')) {
return stream_isatty($fd);
}
// only trusting this if it is positive, otherwise prefer fstat fallback
if (function_exists('posix_isatty') && posix_isatty($fd)) {
return true;
}
$stat = @fstat($fd);
// Check if formatted mode is S_IFCHR
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
}
/**
* @return void
*/
public static function workaroundFilesystemIssues()
{
if (self::isVirtualBoxGuest()) {
usleep(200000);
}
}
/**
* Attempts detection of VirtualBox guest VMs
*
* This works based on the process' user being "vagrant", the COMPOSER_RUNTIME_ENV env var being set to "virtualbox", or lsmod showing the virtualbox guest additions are loaded
*
* @return bool
*/
private static function isVirtualBoxGuest()
{
if (null === self::$isVirtualBoxGuest) {
self::$isVirtualBoxGuest = false;
if (self::isWindows()) {
return self::$isVirtualBoxGuest;
}
if (function_exists('posix_getpwuid') && function_exists('posix_geteuid')) {
$processUser = posix_getpwuid(posix_geteuid());
if ($processUser && $processUser['name'] === 'vagrant') {
return self::$isVirtualBoxGuest = true;
}
}
if (self::getEnv('COMPOSER_RUNTIME_ENV') === 'virtualbox') {
return self::$isVirtualBoxGuest = true;
}
if (defined('PHP_OS_FAMILY') && PHP_OS_FAMILY === 'Linux') {
$process = new ProcessExecutor();
try {
if (0 === $process->execute('lsmod | grep vboxguest', $ignoredOutput)) {
return self::$isVirtualBoxGuest = true;
}
} catch (\Exception $e) {
// noop
}
}
}
return self::$isVirtualBoxGuest;
}
public static function getDevNull()
{
if (self::isWindows()) {
return 'NUL';
}
return '/dev/null';
}
}