Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit b2ab2a4

Browse files
committed
up
1 parent 3548e12 commit b2ab2a4

File tree

4 files changed

+225
-146
lines changed

4 files changed

+225
-146
lines changed

src/Helpers/Cli.php

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -256,60 +256,4 @@ public static function stderr($text, $nl = true, $quit = -200)
256256
exit($code);
257257
}
258258
}
259-
260-
/**
261-
* run a command in background
262-
* @param string $cmd
263-
*/
264-
public static function execInBackground($cmd)
265-
{
266-
if (strpos(PHP_OS, 'Windows') === 0) {
267-
pclose(popen('start /B ' . $cmd, 'r'));
268-
} else {
269-
exec($cmd . ' > /dev/null &');
270-
}
271-
}
272-
273-
/**
274-
* Method to execute a command in the terminal
275-
* Uses :
276-
* 1. system
277-
* 2. passthru
278-
* 3. exec
279-
* 4. shell_exec
280-
* @param $command
281-
* @return array
282-
*/
283-
public static function exec($command)
284-
{
285-
$return_var = 1;
286-
287-
//system
288-
if (function_exists('system')) {
289-
ob_start();
290-
system($command, $return_var);
291-
$output = ob_get_contents();
292-
ob_end_clean();
293-
294-
// passthru
295-
} elseif (function_exists('passthru')) {
296-
ob_start();
297-
passthru($command, $return_var);
298-
$output = ob_get_contents();
299-
ob_end_clean();
300-
//exec
301-
} else if (function_exists('exec')) {
302-
exec($command, $output, $return_var);
303-
$output = implode("\n", $output);
304-
305-
//shell_exec
306-
} else if (function_exists('shell_exec')) {
307-
$output = shell_exec($command);
308-
} else {
309-
$output = 'Command execution not possible on this system';
310-
$return_var = 0;
311-
}
312-
313-
return array('output' => $output, 'status' => $return_var);
314-
}
315259
}

src/Helpers/EnvHelper.php

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,10 @@
1414
*/
1515
class EnvHelper
1616
{
17-
/**
18-
* Get PHP version
19-
* @return string
20-
*/
21-
public static function getVersion(): string
22-
{
23-
return defined('HHVM_VERSION') ? HHVM_VERSION : PHP_VERSION;
24-
}
25-
2617
////////////////////////////////////////
2718
/// system env
2819
////////////////////////////////////////
2920

30-
3121
/**
3222
* @return bool
3323
*/
@@ -50,6 +40,10 @@ public static function isLinux(): bool
5040
* @return bool
5141
*/
5242
public static function isWin(): bool
43+
{
44+
return self::isWindows();
45+
}
46+
public static function isWindows(): bool
5347
{
5448
return stripos(PHP_OS, 'WIN') !== false;
5549
}
@@ -62,10 +56,46 @@ public static function isMac(): bool
6256
return stripos(PHP_OS, 'Darwin') !== false;
6357
}
6458

59+
/**
60+
* @return bool
61+
*/
62+
public static function isRoot(): bool
63+
{
64+
return posix_getuid() === 0;
65+
}
66+
67+
/**
68+
* @return string
69+
*/
70+
public static function getHostname()
71+
{
72+
return php_uname('n');
73+
}
74+
75+
/**
76+
* @return string
77+
*/
78+
public static function getNullDevice()
79+
{
80+
if (self::isUnix()) {
81+
return '/dev/null';
82+
}
83+
84+
return 'NUL';
85+
}
6586
////////////////////////////////////////
6687
/// php env
6788
////////////////////////////////////////
6889

90+
/**
91+
* Get PHP version
92+
* @return string
93+
*/
94+
public static function getVersion(): string
95+
{
96+
return defined('HHVM_VERSION') ? HHVM_VERSION : PHP_VERSION;
97+
}
98+
6999
/**
70100
* isEmbed
71101
* @return boolean
@@ -137,7 +167,6 @@ public static function isPHP(): bool
137167
return !static::isHHVM();
138168
}
139169

140-
141170
/**
142171
* setStrict
143172
* @return void

src/Helpers/Sys.php

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/6/27
6+
* Time: 下午8:18
7+
*/
8+
9+
namespace Inhere\Library\Helpers;
10+
11+
/**
12+
* Class Sys
13+
* @package Inhere\Library\Helpers
14+
*/
15+
class Sys extends EnvHelper
16+
{
17+
/**
18+
* run a command in background
19+
* @param string $cmd
20+
*/
21+
public static function execInBackground($cmd)
22+
{
23+
if (self::isWindows()) {
24+
pclose(popen('start /B ' . $cmd, 'r'));
25+
} else {
26+
exec($cmd . ' > /dev/null &');
27+
}
28+
}
29+
30+
/**
31+
* @param string $command
32+
* @param null|string $logfile
33+
* @param null|string $user
34+
* @return mixed
35+
*/
36+
public static function exec($command, $logfile = null, $user = null)
37+
{
38+
// If should run as another user, we must be on *nix and must have sudo privileges.
39+
$suDo = '';
40+
41+
if ($user && self::isUnix() && self::isRoot()) {
42+
$suDo = "sudo -u $user";
43+
}
44+
45+
// Start execution. Run in foreground (will block).
46+
$logfile = $logfile ?: self::getNullDevice();
47+
48+
// Start execution. Run in foreground (will block).
49+
exec("$suDo $command 1>> \"$logfile\" 2>&1", $dummy, $retVal);
50+
51+
if ($retVal !== 0) {
52+
throw new \RuntimeException("command exited with status '$retVal'.");
53+
}
54+
55+
return $dummy;
56+
}
57+
58+
/**
59+
* Method to execute a command in the sys
60+
* Uses :
61+
* 1. system
62+
* 2. passthru
63+
* 3. exec
64+
* 4. shell_exec
65+
* @param $command
66+
* @return array
67+
*/
68+
public static function runCommand($command)
69+
{
70+
$return_var = 1;
71+
72+
//system
73+
if (function_exists('system')) {
74+
ob_start();
75+
system($command, $return_var);
76+
$output = ob_get_contents();
77+
ob_end_clean();
78+
79+
// passthru
80+
} elseif (function_exists('passthru')) {
81+
ob_start();
82+
passthru($command, $return_var);
83+
$output = ob_get_contents();
84+
ob_end_clean();
85+
//exec
86+
} else if (function_exists('exec')) {
87+
exec($command, $output, $return_var);
88+
$output = implode("\n", $output);
89+
90+
//shell_exec
91+
} else if (function_exists('shell_exec')) {
92+
$output = shell_exec($command);
93+
} else {
94+
$output = 'Command execution not possible on this system';
95+
$return_var = 0;
96+
}
97+
98+
return array('output' => $output, 'status' => $return_var);
99+
}
100+
101+
/**
102+
* @return string
103+
*/
104+
public static function getTempDir()
105+
{
106+
// @codeCoverageIgnoreStart
107+
if (function_exists('sys_get_temp_dir')) {
108+
$tmp = sys_get_temp_dir();
109+
} elseif (!empty($_SERVER['TMP'])) {
110+
$tmp = $_SERVER['TMP'];
111+
} elseif (!empty($_SERVER['TEMP'])) {
112+
$tmp = $_SERVER['TEMP'];
113+
} elseif (!empty($_SERVER['TMPDIR'])) {
114+
$tmp = $_SERVER['TMPDIR'];
115+
} else {
116+
$tmp = getcwd();
117+
}
118+
// @codeCoverageIgnoreEnd
119+
120+
return $tmp;
121+
}
122+
123+
/**
124+
* @param string $program
125+
* @return int|string
126+
*/
127+
public static function getCpuUsage($program)
128+
{
129+
if (!$program) {
130+
return -1;
131+
}
132+
133+
$info = exec('ps aux | grep ' . $program . ' | grep -v grep | grep -v su | awk {"print $3"}');
134+
135+
return $info;
136+
}
137+
138+
/**
139+
* @param $program
140+
* @return int|string
141+
*/
142+
public static function getMemUsage($program)
143+
{
144+
if (!$program) {
145+
return -1;
146+
}
147+
148+
$info = exec('ps aux | grep ' . $program . ' | grep -v grep | grep -v su | awk {"print $4"}');
149+
150+
return $info;
151+
}
152+
153+
154+
/**
155+
* 支持查看指定目录,默认当前目录
156+
* CLI:
157+
* php test.php -d=path
158+
* php test.php --dir=path
159+
* WEB:
160+
* /test.php?dir=path
161+
*/
162+
public static function gitCheck()
163+
{
164+
if (PHP_SAPI === 'cli') {
165+
$_GET = getopt('d::', ['dir::']);
166+
}
167+
168+
// 获取要查看的目录,没有则检测当前目录
169+
$dir = $_GET['d'] ?? ($_GET['dir'] ?? __DIR__);
170+
171+
if (!is_dir($dir)) {
172+
trigger_error($dir);
173+
}
174+
175+
ob_start();
176+
system("cd $dir && git branch -v");
177+
$c = ob_get_clean();
178+
179+
$result = preg_match('#\* (?<brName>[\S]+)(?:\s+)(?<logNum>[0-9a-z]{7})(?<ciText>.*)#i', $c, $data);
180+
$data['projectName'] = basename($dir);
181+
182+
// var_dump($c,$result, $data);
183+
return ($result === 1) ? $data : null;
184+
}
185+
}

0 commit comments

Comments
 (0)