|
4 | 4 |
|
5 | 5 | namespace SPC\util\shell;
|
6 | 6 |
|
| 7 | +use SPC\exception\ExecutionException; |
7 | 8 | use SPC\exception\SPCInternalException;
|
8 | 9 | use ZM\Logger\ConsoleColor;
|
9 | 10 |
|
@@ -65,6 +66,53 @@ public function getLastCommand(): string
|
65 | 66 | return $this->last_cmd;
|
66 | 67 | }
|
67 | 68 |
|
| 69 | + protected function passthru(string $cmd, bool $console_output = false, ?string $original_command = null): void |
| 70 | + { |
| 71 | + $file_res = null; |
| 72 | + if ($this->enable_log_file) { |
| 73 | + $file_res = fopen(SPC_SHELL_LOG, 'a'); |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + $process = popen($cmd . ' 2>&1', 'r'); |
| 78 | + if (!$process) { |
| 79 | + throw new ExecutionException( |
| 80 | + cmd: $original_command ?? $cmd, |
| 81 | + message: 'Failed to open process for command, popen() failed.', |
| 82 | + code: -1, |
| 83 | + cd: $this->cd, |
| 84 | + env: $this->env |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + while (($line = fgets($process)) !== false) { |
| 89 | + if ($console_output) { |
| 90 | + echo $line; |
| 91 | + } |
| 92 | + fwrite($file_res, $line); |
| 93 | + } |
| 94 | + |
| 95 | + $result_code = pclose($process); |
| 96 | + |
| 97 | + if ($result_code !== 0) { |
| 98 | + if ($file_res) { |
| 99 | + fwrite($file_res, "Command exited with non-zero code: {$result_code}\n"); |
| 100 | + } |
| 101 | + throw new ExecutionException( |
| 102 | + cmd: $original_command ?? $cmd, |
| 103 | + message: "Command exited with non-zero code: {$result_code}", |
| 104 | + code: $result_code, |
| 105 | + cd: $this->cd, |
| 106 | + env: $this->env, |
| 107 | + ); |
| 108 | + } |
| 109 | + } finally { |
| 110 | + if ($file_res) { |
| 111 | + fclose($file_res); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
68 | 116 | protected function logCommandInfo(string $cmd): void
|
69 | 117 | {
|
70 | 118 | // write executed command to the log file using fwrite
|
|
0 commit comments