Skip to content

Commit cbddb26

Browse files
authored
Merge pull request #856 from crazywhalecc/fix/windows-cmd
Fix WindowsCmd execution, use popen instead of proc_open
2 parents 5f62925 + 8d84a95 commit cbddb26

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/SPC/util/shell/WindowsCmd.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace SPC\util\shell;
66

7+
use SPC\exception\ExecutionException;
78
use SPC\exception\SPCInternalException;
89
use ZM\Logger\ConsoleColor;
910

@@ -65,6 +66,53 @@ public function getLastCommand(): string
6566
return $this->last_cmd;
6667
}
6768

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+
68116
protected function logCommandInfo(string $cmd): void
69117
{
70118
// write executed command to the log file using fwrite

0 commit comments

Comments
 (0)