This repository was archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatform.php
More file actions
249 lines (212 loc) · 7.02 KB
/
Platform.php
File metadata and controls
249 lines (212 loc) · 7.02 KB
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
<?php namespace nyx\utils;
/**
* Platform
*
* Utilities related to the operating system PHP is running on, retrieving information about and executing
* system processes etc.
*
* Requires:
* - Function: shell_exec() (getting the shells available on this system)
* - Function: exec() (checking whether TTY is available on this system)
*
* @package Nyx\Utils\Platform
* @version 0.1.0
* @author Michal Chojnacki <m.chojnacki@muyo.io>
* @copyright 2012-2016 Nyx Dev Team
* @link http://docs.muyo.io/nyx/utils/platform.html
*/
class Platform
{
/**
* The traits of the Platform class.
*/
use traits\StaticallyExtendable;
/**
* Platform type constants.
*/
const TYPE_UNIX = 1;
const TYPE_WINDOWS = 2;
const TYPE_BSD = 3;
const TYPE_CYGWIN = 4;
const TYPE_DARWIN = 5;
/**
* @var int The platform PHP is running on.
*/
private static $type;
/**
* @var array An array of shell names and the paths to their binaries once populated or false when PHP is
* running on a system that does not support them (Windows).
*/
private static $shells;
/**
* @var bool Whether this platform has the 'stty' binary (always false on Windows).
*/
private static $hasStty;
/**
* Guesses and returns the platform PHP is running on. If it can't be determined, the default
* of self::TYPE_UNIX will be returned.
*
* @return int One of the platform TYPE_ constants defined in this class.
*/
public static function getType() : int
{
// Return the cached result if it's already available.
if (null !== static::$type) {
return static::$type;
}
$os = strtolower(php_uname("s"));
// Check in order of likeliness.
if (false !== strpos($os, 'unix')) {
return static::$type = self::TYPE_UNIX;
}
if (0 === strpos($os, 'win')) {
return static::$type = self::TYPE_WINDOWS;
}
if (false !== strpos($os, 'bsd')) {
return static::$type = self::TYPE_BSD;
}
if (false !== strpos($os, 'darwin')) {
return static::$type = self::TYPE_DARWIN;
}
if (false !== strpos($os, 'cygwin')) {
return static::$type = self::TYPE_CYGWIN;
}
// Use the default otherwise.
return static::$type = self::TYPE_UNIX;
}
/**
* Checks whether PHP is running on a Unix platform
*
* @return bool True when PHP is running on a Unix platform, false otherwise.
*/
public static function isUnix() : bool
{
// Return the cached result if it's already available.
if (null !== static::$type) {
return static::$type === self::TYPE_UNIX;
}
return static::getType() === self::TYPE_UNIX;
}
/**
* Checks whether PHP is running on a Windows platform
*
* @return bool True when PHP is running on a Windows platform, false otherwise.
*/
public static function isWindows() : bool
{
// Return the cached result if it's already available.
if (null !== static::$type) {
return static::$type === self::TYPE_WINDOWS;
}
return static::getType() === self::TYPE_WINDOWS;
}
/**
* Checks whether PHP is running on a BSD platform
*
* @return bool True when PHP is running on a BSD platform, false otherwise.
*/
public static function isBsd() : bool
{
// Return the cached result if it's already available.
if (null !== static::$type) {
return static::$type === self::TYPE_BSD;
}
return static::getType() === self::TYPE_BSD;
}
/**
* Checks whether PHP is running on a Darwin platform
*
* @return bool True when PHP is running on a Darwin platform, false otherwise.
*/
public static function isDarwin() : bool
{
// Return the cached result if it's already available.
if (null !== static::$type) {
return static::$type === self::TYPE_DARWIN;
}
return static::getType() === self::TYPE_DARWIN;
}
/**
* Checks whether PHP is running on a Cygwin platform
*
* @return bool True when PHP is running on a Cygwin platform, false otherwise.
*/
public static function isCygwin() : bool
{
// Return the cached result if it's already available.
if (null !== static::$type) {
return static::$type === self::TYPE_CYGWIN;
}
return static::getType() === self::TYPE_CYGWIN;
}
/**
* Returns the path to the given shell's binary or null when it is not available.
*
* @param string $name
* @return string|null
*/
public static function getShell(string $name)
{
return static::getShells()[$name] ?? null;
}
/**
* Checks whether a shell of the given name is available in the system.
*
* @param string $name
* @return bool
*/
public static function hasShell(string $name) : bool
{
return isset(static::getShells()[$name]);
}
/**
* Returns an array of shell names and the paths to their binaries once populated. Will return an empty
* array on unsupported platforms (Windows).
*
* @return array
*/
public static function getShells() : array
{
// Return the cached result if it's already available.
if (static::$shells !== null) {
return static::$shells;
}
// Ensure this method will be ran once at most.
static::$shells = [];
// Can't easily check on Windows even if Cygwin or the likes are available.
if (static::isWindows()) {
return static::$shells;
}
if (file_exists($file = '/etc/shells')) {
$cat = trim(shell_exec('cat '.$file.' 2> /dev/null'));
foreach (explode(PHP_EOL, $cat) as $path) {
// Ignore this line if it doesn't begin with a filepath.
if ($path[0] != '/') {
continue;
}
$name = substr($path, strrpos($path, '/') + 1);
static::$shells[$name] = $path;
}
}
return static::$shells;
}
/**
* Checks whether this platform has the 'stty' binary.
*
* @return bool True when 'stty' is available on this platform, false otherwise (always false on Windows).
*/
public static function hasStty() : bool
{
// Return the cached result if it's already available.
if (static::$hasStty !== null) {
return static::$hasStty;
}
// Definitely no Stty on Windows.
if (static::isWindows()) {
return static::$hasStty = false;
}
// Run a simple exec() call and check whether it returned with an error code.
exec('/usr/bin/env stty', $output, $exitCode);
return static::$hasStty = $exitCode === 0;
}
}