1111
1212use RuntimeException ;
1313use Toolkit \Sys \Proc \ProcWrapper ;
14- use function chdir ;
14+ use Toolkit \ Sys \ Util \ ShellUtil ;
1515use function exec ;
16- use function function_exists ;
17- use function implode ;
1816use function is_file ;
19- use function ob_start ;
2017use function preg_match ;
21- use function preg_replace ;
22- use function shell_exec ;
23- use function system ;
24- use function trim ;
2518use const DIRECTORY_SEPARATOR ;
2619
2720/**
3225class 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 /**
0 commit comments