-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSys.php
More file actions
278 lines (250 loc) · 6.87 KB
/
Copy pathSys.php
File metadata and controls
278 lines (250 loc) · 6.87 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php declare(strict_types=1);
/**
* This file is part of toolkit/sys-utils.
*
* @author https://github.com/inhere
* @link https://github.com/php-toolkit/sys-utils
* @license MIT
*/
namespace Toolkit\Sys;
use RuntimeException;
use Toolkit\Sys\Proc\ProcWrapper;
use Toolkit\Sys\Util\ShellUtil;
use function exec;
use function fgets;
use function is_file;
use function is_readable;
use function pclose;
use function popen;
use function preg_match;
use const DIRECTORY_SEPARATOR;
/**
* Class Sys
*
* @package Toolkit\Sys\Proc
*/
class Sys extends SysEnv
{
/**
* @param string $command
* @param string $logfile
* @param string $user
*
* @return mixed
* @throws RuntimeException
*/
public static function execWithSudo(string $command, string $logfile = '', string $user = ''): mixed
{
return \Toolkit\Sys\Exec::execWithSudo($command, $logfile, $user);
}
/**
* run a command. it is support windows
*
* @param string $command
* @param string $cwd
*
* @return array [$code, $output, $error]
* @throws RuntimeException
*/
public static function run(string $command, string $cwd = ''): array
{
return ProcWrapper::runCmd($command, $cwd);
}
/**
* Method to execute a command in the sys
* Uses :
* 1. system
* X. passthru - will report error on windows
* 3. exec
* 4. shell_exec
*
* @param string $command
* @param bool $returnStatus
* @param string $cwd
*
* @return array|string
*/
public static function execute(string $command, bool $returnStatus = true, string $cwd = ''): array|string
{
return \Toolkit\Sys\Exec::auto($command, $returnStatus, $cwd);
}
/**
* get bash is available
*
* @return bool
* @deprecated please use ShellUtil::shIsAvailable()
*/
public static function shIsAvailable(): bool
{
return ShellUtil::shIsAvailable();
}
/**
* get bash is available
*
* @return bool
* @deprecated please use ShellUtil::bashIsAvailable()
*/
public static function bashIsAvailable(): bool
{
return ShellUtil::bashIsAvailable();
}
/**
* @return string
*/
public static function getOutsideIP(): string
{
[$code, $out] = self::run('ip addr | grep eth0');
if ($code === 0 && $out && preg_match('#inet (.*)\/#', $out, $ms)) {
return $ms[1];
}
return 'unknown';
}
/**
* Open browser URL
*
* Mac:
* open 'https://swoft.org'
*
* Linux:
* x-www-browser 'https://swoft.org'
*
* Windows:
* cmd /c start https://swoft.org
*
* @param string $pageUrl
*/
public static function openBrowser(string $pageUrl): void
{
if (self::isMac()) {
$cmd = "open \"$pageUrl\"";
} elseif (self::isWin()) {
// $cmd = 'cmd /c start';
$cmd = "start $pageUrl";
} else {
$cmd = "x-www-browser \"$pageUrl\"";
}
// Show::info("Will open the page on browser:\n $pageUrl");
\Toolkit\Sys\Exec::auto($cmd);
}
/**
* get screen size
*
* ```php
* list($width, $height) = Sys::getScreenSize();
* ```
*
* @from Yii2
*
* @param boolean $refresh whether to force checking and not re-use cached size value.
* This is useful to detect changing window size while the application is running but may
* not get up to date values on every terminal.
*
* @return array|boolean An array of ($width, $height) or false when it was not able to determine size.
*/
public static function getScreenSize(bool $refresh = false): bool|array
{
return ShellUtil::getScreenSize($refresh);
}
/**
* @param string $program
*
* @return int|string
*/
public static function getCpuUsage(string $program): int|string
{
if (!$program) {
return -1;
}
return exec('ps aux | grep ' . $program . ' | grep -v grep | grep -v su | awk {"print $3"}');
}
/**
* @param string $program
*
* @return int|string
*/
public static function getMemUsage(string $program): int|string
{
if (!$program) {
return -1;
}
return exec('ps aux | grep ' . $program . ' | grep -v grep | grep -v su | awk {"print $4"}');
}
private static ?int $cpuNum = null;
/**
* @return int
* @refer https://gist.github.com/ezzatron/1321581
*/
public static function getCpuNum(): int
{
if (self::$cpuNum !== null) {
return self::$cpuNum;
}
$cpuNum = 1;
if (self::isWin()) {
$process = @popen('wmic cpu get NumberOfCores', 'rb');
if (false !== $process) {
fgets($process);
$cpuNum = (int)fgets($process);
pclose($process);
} else {
$cpuNum = (int)self::getEnvVal('NUMBER_OF_PROCESSORS', '1');
}
} elseif (is_readable('/proc/cpuinfo')) {
$cpuInfo = file_get_contents('/proc/cpuinfo');
preg_match_all('/^processor/m', $cpuInfo, $matches);
$cpuNum = count($matches[0]);
} else {
$process = @popen('sysctl -a | grep hw.ncpu', 'rb');
if (false !== $process) {
// 'hw.ncpu: 8'
$line = fgets($process);
pclose($process);
if (1 === preg_match('/hw.ncpu: (\d+)/', $line, $match)) {
$cpuNum = (int)$match[1];
}
}
}
return self::$cpuNum = $cpuNum;
}
/**
* find executable file by input
*
* Usage:
*
* ```php
* $phpBin = Sys::findExecutable('php');
* echo $phpBin; // "/usr/bin/php"
* ```
*
* @param string $name
* @param array $paths The dir paths for find bin file. if empty, will read from env $PATH
*
* @return string
*/
public static function findExecutable(string $name, array $paths = []): string
{
$isWin = self::isWindows();
$paths = $paths ?: self::getEnvPaths();
foreach ($paths as $path) {
$filename = $path . DIRECTORY_SEPARATOR . $name;
if (is_file($filename)) {
return $filename;
}
// maybe is exe file
if ($isWin && is_file($filename . '.exe')) {
return $filename . '.exe';
}
}
return "";
}
/**
* @param string $name
* @param array $paths
*
* @return bool
*/
public static function isExecutable(string $name, array $paths = []): bool
{
return self::findExecutable($name, $paths) !== "";
}
}