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$ logfilenull ,  $ usernull )
35+  public  static  function  execWithSudo ( string   $ commandstring   $ 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  ($ userisUnix () && SysEnv::isRoot ()) {
47-  $ suDo"sudo -u  $ user ;
48-  }
49- 
50-  // Start execution. Run in foreground (will block). 
51-  $ logfile$ logfilegetNullDevice ();
52- 
53-  // Start execution. Run in foreground (will block). 
54-  exec ("$ suDo  $ command 1>>  \"$ logfile 2>&1 " , $ dummy$ retVal
55- 
56-  if  ($ retVal0 ) {
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  $ commandbool  $ returnStatustrue , string  $ cwd'' )
9269 {
93-  $ status1 ;
94- 
95-  if  ($ cwd
96-  chdir ($ cwd
97-  }
98- 
99-  // system 
100-  if  (function_exists ('system ' )) {
101-  ob_start ();
102-  system ($ command$ status
103-  $ outputob_get_clean ();
104-  //exec 
105-  } elseif  (function_exists ('exec ' )) {
106-  exec ($ command$ output$ status
107-  $ outputimplode ("\n" , $ output
108- 
109-  //shell_exec 
110-  } elseif  (function_exists ('shell_exec ' )) {
111-  $ outputshell_exec ($ command
112-  } else  {
113-  $ status1 ;
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 ($ checkCmdfalse ) === '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 ($ checkCmdfalse ) === '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  ($ code0  && $ output preg_match ('#inet (.*)\/# ' , $ output $ ms
102+  if  ($ code0  && $ out preg_match ('#inet (.*)\/# ' , $ out $ ms
163103 return  $ ms1 ];
164104 }
165105
@@ -183,16 +123,16 @@ public static function getOutsideIP(): string
183123 public  static  function  openBrowser (string  $ pageUrlvoid 
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  $ refreshfalse )
214154 {
215-  static  $ size
216-  if  ($ sizenull  && !$ 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 ' , $ sttypreg_match (
225-  '/rows\s+(\d+);\s*columns\s+(\d+);/mi ' ,
226-  implode ('  ' , $ stty
227-  $ matches
228-  )
229-  ) {
230-  return  ($ size$ matches2 ], $ matches1 ]]);
231-  }
232- 
233-  // fallback to tput, which may not be updated on terminal resize 
234-  if  (($ widthint )exec ('tput cols 2>&1 ' )) > 0  && ($ heightint )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  (($ widthint )getenv ('COLUMNS ' )) > 0  && ($ heightint )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 ($ output1 ]) && strpos ($ output1 ], 'CON ' ) !== false ) {
249-  return  ($ size
250-  (int )preg_replace ('~\D~ ' , '' , $ output3 ]),
251-  (int )preg_replace ('~\D~ ' , '' , $ output4 ])
252-  ]);
253-  }
254-  }
255- 
256-  return  ($ sizefalse );
155+  return  ShellUtil::getScreenSize ($ refresh
257156 }
258157
259158 /** 
0 commit comments