11
11
12
12
use RuntimeException ;
13
13
use Toolkit \Sys \Proc \ProcWrapper ;
14
- use function chdir ;
14
+ use Toolkit \ Sys \ Util \ ShellUtil ;
15
15
use function exec ;
16
- use function function_exists ;
17
- use function implode ;
18
16
use function is_file ;
19
- use function ob_start ;
20
17
use function preg_match ;
21
- use function preg_replace ;
22
- use function shell_exec ;
23
- use function system ;
24
- use function trim ;
25
18
use const DIRECTORY_SEPARATOR ;
26
19
27
20
/**
32
25
class Sys extends SysEnv
33
26
{
34
27
/**
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
38
31
*
39
32
* @return mixed
40
33
* @throws RuntimeException
41
34
*/
42
- public static function exec ( $ command , $ logfile = null , $ user = null )
35
+ public static function execWithSudo ( string $ command , string $ logfile = '' , string $ user = '' )
43
36
{
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 );
61
38
}
62
39
63
40
/**
@@ -82,84 +59,47 @@ public static function run(string $command, string $cwd = ''): array
82
59
* 3. exec
83
60
* 4. shell_exec
84
61
*
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
88
65
*
89
66
* @return array|string
90
67
*/
91
68
public static function execute (string $ command , bool $ returnStatus = true , string $ cwd = '' )
92
69
{
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 );
125
71
}
126
72
127
73
/**
128
74
* get bash is available
129
75
*
130
76
* @return bool
77
+ * @deprecated please use ShellUtil::shIsAvailable()
131
78
*/
132
79
public static function shIsAvailable (): bool
133
80
{
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 ();
139
82
}
140
83
141
84
/**
142
85
* get bash is available
143
86
*
144
87
* @return bool
88
+ * @deprecated please use ShellUtil::bashIsAvailable()
145
89
*/
146
90
public static function bashIsAvailable (): bool
147
91
{
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 ();
153
93
}
154
94
155
95
/**
156
96
* @return string
157
97
*/
158
98
public static function getOutsideIP (): string
159
99
{
160
- [$ code , $ output ] = self ::run ('ip addr | grep eth0 ' );
100
+ [$ code , $ out ] = self ::run ('ip addr | grep eth0 ' );
161
101
162
- if ($ code === 0 && $ output && preg_match ('#inet (.*)\/# ' , $ output , $ ms )) {
102
+ if ($ code === 0 && $ out && preg_match ('#inet (.*)\/# ' , $ out , $ ms )) {
163
103
return $ ms [1 ];
164
104
}
165
105
@@ -183,16 +123,16 @@ public static function getOutsideIP(): string
183
123
public static function openBrowser (string $ pageUrl ): void
184
124
{
185
125
if (self ::isMac ()) {
186
- $ cmd = "open \"{ $ pageUrl} \"" ;
126
+ $ cmd = "open \"$ pageUrl \"" ;
187
127
} elseif (self ::isWin ()) {
188
128
// $cmd = 'cmd /c start';
189
- $ cmd = "start { $ pageUrl} " ;
129
+ $ cmd = "start $ pageUrl " ;
190
130
} else {
191
- $ cmd = "x-www-browser \"{ $ pageUrl} \"" ;
131
+ $ cmd = "x-www-browser \"$ pageUrl \"" ;
192
132
}
193
133
194
134
// Show::info("Will open the page on browser:\n $pageUrl");
195
- self :: execute ($ cmd );
135
+ \ Toolkit \ Sys \Exec:: auto ($ cmd );
196
136
}
197
137
198
138
/**
@@ -212,48 +152,7 @@ public static function openBrowser(string $pageUrl): void
212
152
*/
213
153
public static function getScreenSize (bool $ refresh = false )
214
154
{
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 );
257
156
}
258
157
259
158
/**
0 commit comments