Skip to content

Commit 8431327

Browse files
committed
feat(shell): add some new shell util methods
Signed-off-by: inhere <in.798@qq.com>
1 parent 1e5b3bb commit 8431327

File tree

5 files changed

+270
-125
lines changed

5 files changed

+270
-125
lines changed

src/Exec.php

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
use RuntimeException;
1313
use Toolkit\Sys\Proc\ProcWrapper;
1414
use function chdir;
15+
use function exec;
16+
use function function_exists;
1517
use function implode;
1618
use function ob_get_clean;
1719
use function ob_start;
1820
use function pclose;
1921
use function popen;
22+
use function shell_exec;
2023
use function system;
21-
use function exec;
24+
use function trim;
2225

2326
/**
2427
* Class Exec
@@ -122,4 +125,96 @@ public static function run(string $command, string $cwd = ''): array
122125
{
123126
return ProcWrapper::runCmd($command, $cwd);
124127
}
128+
129+
/**
130+
* Method to execute a command in the sys
131+
* Uses :
132+
* 1. system
133+
* X. passthru - will report error on windows
134+
* 3. exec
135+
* 4. shell_exec
136+
*
137+
* @param string $command
138+
* @param bool $returnStatus
139+
* @param string|null $cwd
140+
*
141+
* @return array|string
142+
*/
143+
public static function auto(string $command, bool $returnStatus = true, string $cwd = '')
144+
{
145+
$status = 1;
146+
147+
if ($cwd) {
148+
chdir($cwd);
149+
}
150+
151+
// system
152+
if (function_exists('system')) {
153+
ob_start();
154+
system($command, $status);
155+
$output = ob_get_clean();
156+
//exec
157+
} elseif (function_exists('exec')) {
158+
exec($command, $output, $status);
159+
$output = implode("\n", $output);
160+
161+
//shell_exec
162+
} elseif (function_exists('shell_exec')) {
163+
$output = shell_exec($command);
164+
} else {
165+
$status = -1;
166+
$output = 'Command execution not possible on this system';
167+
}
168+
169+
if ($returnStatus) {
170+
return [
171+
'output' => trim($output),
172+
'status' => $status
173+
];
174+
}
175+
176+
return trim($output);
177+
}
178+
179+
/**
180+
* @param string $command
181+
* @param string $logfile
182+
* @param string $user
183+
*
184+
* @return mixed
185+
* @throws RuntimeException
186+
*/
187+
public static function execWithSudo(string $command, string $logfile = '', string $user = '')
188+
{
189+
// If should run as another user, we must be on *nix and must have sudo privileges.
190+
$suDo = '';
191+
if ($user && SysEnv::isUnix() && SysEnv::isRoot()) {
192+
$suDo = "sudo -u $user";
193+
}
194+
195+
// Start execution. Run in foreground (will block).
196+
$logfile = $logfile ?: SysEnv::getNullDevice();
197+
198+
// Start execution. Run in foreground (will block).
199+
exec("$suDo $command 1>> \"$logfile\" 2>&1", $dummy, $retVal);
200+
201+
if ($retVal !== 0) {
202+
throw new RuntimeException("command exited with status '$retVal'.");
203+
}
204+
205+
return $dummy;
206+
}
207+
208+
/**
209+
* Quick exec an command and return output
210+
*
211+
* @param string $command
212+
* @param string $cwd
213+
*
214+
* @return string
215+
*/
216+
public static function getOutput(string $command, string $cwd = ''): string
217+
{
218+
return self::auto($command, false, $cwd);
219+
}
125220
}

src/PhpEnv.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Toolkit\Sys;
1111

1212
use Toolkit\Sys\Traits\PhpEnvTrait;
13-
use Toolkit\Stdlib\Helper\PhpHelper;
1413

1514
/**
1615
* Class Php - alias of the class PhpHelper

src/Sys.php

Lines changed: 21 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@
1111

1212
use RuntimeException;
1313
use Toolkit\Sys\Proc\ProcWrapper;
14-
use function chdir;
14+
use Toolkit\Sys\Util\ShellUtil;
1515
use function exec;
16-
use function function_exists;
17-
use function implode;
1816
use function is_file;
19-
use function ob_start;
2017
use function preg_match;
21-
use function preg_replace;
22-
use function shell_exec;
23-
use function system;
24-
use function trim;
2518
use const DIRECTORY_SEPARATOR;
2619

2720
/**
@@ -32,32 +25,16 @@
3225
class Sys extends SysEnv
3326
{
3427
/**
35-
* @param string $command
36-
* @param null|string $logfile
37-
* @param null|string $user
28+
* @param string $command
29+
* @param string $logfile
30+
* @param string $user
3831
*
3932
* @return mixed
4033
* @throws RuntimeException
4134
*/
42-
public static function exec($command, $logfile = null, $user = null)
35+
public static function execWithSudo(string $command, string $logfile = '', string $user = '')
4336
{
44-
// If should run as another user, we must be on *nix and must have sudo privileges.
45-
$suDo = '';
46-
if ($user && SysEnv::isUnix() && SysEnv::isRoot()) {
47-
$suDo = "sudo -u $user";
48-
}
49-
50-
// Start execution. Run in foreground (will block).
51-
$logfile = $logfile ?: SysEnv::getNullDevice();
52-
53-
// Start execution. Run in foreground (will block).
54-
exec("$suDo $command 1>> \"$logfile\" 2>&1", $dummy, $retVal);
55-
56-
if ($retVal !== 0) {
57-
throw new RuntimeException("command exited with status '$retVal'.");
58-
}
59-
60-
return $dummy;
37+
return \Toolkit\Sys\Exec::execWithSudo($command, $logfile, $user);
6138
}
6239

6340
/**
@@ -82,84 +59,47 @@ public static function run(string $command, string $cwd = ''): array
8259
* 3. exec
8360
* 4. shell_exec
8461
*
85-
* @param string $command
86-
* @param bool $returnStatus
87-
* @param string|null $cwd
62+
* @param string $command
63+
* @param bool $returnStatus
64+
* @param string $cwd
8865
*
8966
* @return array|string
9067
*/
9168
public static function execute(string $command, bool $returnStatus = true, string $cwd = '')
9269
{
93-
$status = 1;
94-
95-
if ($cwd) {
96-
chdir($cwd);
97-
}
98-
99-
// system
100-
if (function_exists('system')) {
101-
ob_start();
102-
system($command, $status);
103-
$output = ob_get_clean();
104-
//exec
105-
} elseif (function_exists('exec')) {
106-
exec($command, $output, $status);
107-
$output = implode("\n", $output);
108-
109-
//shell_exec
110-
} elseif (function_exists('shell_exec')) {
111-
$output = shell_exec($command);
112-
} else {
113-
$status = -1;
114-
$output = 'Command execution not possible on this system';
115-
}
116-
117-
if ($returnStatus) {
118-
return [
119-
'output' => trim($output),
120-
'status' => $status
121-
];
122-
}
123-
124-
return trim($output);
70+
return \Toolkit\Sys\Exec::auto($command, $returnStatus, $cwd);
12571
}
12672

12773
/**
12874
* get bash is available
12975
*
13076
* @return bool
77+
* @deprecated please use ShellUtil::shIsAvailable()
13178
*/
13279
public static function shIsAvailable(): bool
13380
{
134-
// $checkCmd = "/usr/bin/env bash -c 'echo OK'";
135-
// $shell = 'echo $0';
136-
$checkCmd = "sh -c 'echo OK'";
137-
138-
return self::execute($checkCmd, false) === 'OK';
81+
return ShellUtil::shIsAvailable();
13982
}
14083

14184
/**
14285
* get bash is available
14386
*
14487
* @return bool
88+
* @deprecated please use ShellUtil::bashIsAvailable()
14589
*/
14690
public static function bashIsAvailable(): bool
14791
{
148-
// $checkCmd = "/usr/bin/env bash -c 'echo OK'";
149-
// $shell = 'echo $0';
150-
$checkCmd = "bash -c 'echo OK'";
151-
152-
return self::execute($checkCmd, false) === 'OK';
92+
return ShellUtil::bashIsAvailable();
15393
}
15494

15595
/**
15696
* @return string
15797
*/
15898
public static function getOutsideIP(): string
15999
{
160-
[$code, $output] = self::run('ip addr | grep eth0');
100+
[$code, $out] = self::run('ip addr | grep eth0');
161101

162-
if ($code === 0 && $output && preg_match('#inet (.*)\/#', $output, $ms)) {
102+
if ($code === 0 && $out && preg_match('#inet (.*)\/#', $out, $ms)) {
163103
return $ms[1];
164104
}
165105

@@ -183,16 +123,16 @@ public static function getOutsideIP(): string
183123
public static function openBrowser(string $pageUrl): void
184124
{
185125
if (self::isMac()) {
186-
$cmd = "open \"{$pageUrl}\"";
126+
$cmd = "open \"$pageUrl\"";
187127
} elseif (self::isWin()) {
188128
// $cmd = 'cmd /c start';
189-
$cmd = "start {$pageUrl}";
129+
$cmd = "start $pageUrl";
190130
} else {
191-
$cmd = "x-www-browser \"{$pageUrl}\"";
131+
$cmd = "x-www-browser \"$pageUrl\"";
192132
}
193133

194134
// Show::info("Will open the page on browser:\n $pageUrl");
195-
self::execute($cmd);
135+
\Toolkit\Sys\Exec::auto($cmd);
196136
}
197137

198138
/**
@@ -212,48 +152,7 @@ public static function openBrowser(string $pageUrl): void
212152
*/
213153
public static function getScreenSize(bool $refresh = false)
214154
{
215-
static $size;
216-
if ($size !== null && !$refresh) {
217-
return $size;
218-
}
219-
220-
if (self::shIsAvailable()) {
221-
// try stty if available
222-
$stty = [];
223-
224-
if (exec('stty -a 2>&1', $stty) && preg_match(
225-
'/rows\s+(\d+);\s*columns\s+(\d+);/mi',
226-
implode(' ', $stty),
227-
$matches
228-
)
229-
) {
230-
return ($size = [$matches[2], $matches[1]]);
231-
}
232-
233-
// fallback to tput, which may not be updated on terminal resize
234-
if (($width = (int)exec('tput cols 2>&1')) > 0 && ($height = (int)exec('tput lines 2>&1')) > 0) {
235-
return ($size = [$width, $height]);
236-
}
237-
238-
// fallback to ENV variables, which may not be updated on terminal resize
239-
if (($width = (int)getenv('COLUMNS')) > 0 && ($height = (int)getenv('LINES')) > 0) {
240-
return ($size = [$width, $height]);
241-
}
242-
}
243-
244-
if (SysEnv::isWindows()) {
245-
$output = [];
246-
exec('mode con', $output);
247-
248-
if (isset($output[1]) && strpos($output[1], 'CON') !== false) {
249-
return ($size = [
250-
(int)preg_replace('~\D~', '', $output[3]),
251-
(int)preg_replace('~\D~', '', $output[4])
252-
]);
253-
}
254-
}
255-
256-
return ($size = false);
155+
return ShellUtil::getScreenSize($refresh);
257156
}
258157

259158
/**

src/SysEnv.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function getEnv(string $key, string $default = ''): string
6363
}
6464

6565
/**
66-
* @param string $key
66+
* @param string $key
6767
* @param string|int $value
6868
*
6969
* @return bool

0 commit comments

Comments
 (0)