refactor common exec code out

This commit is contained in:
DubbleClick 2025-06-19 09:14:39 +07:00
parent b42409efd1
commit 804468f7b9

View File

@ -44,14 +44,7 @@ class UnixShell
{
/* @phpstan-ignore-next-line */
logger()->info(ConsoleColor::yellow('[EXEC] ') . ConsoleColor::green($cmd));
logger()->debug('Executed at: ' . debug_backtrace()[0]['file'] . ':' . debug_backtrace()[0]['line']);
$env_str = $this->getEnvString();
if (!empty($env_str)) {
$cmd = "{$env_str} {$cmd}";
}
if ($this->cd !== null) {
$cmd = 'cd ' . escapeshellarg($this->cd) . ' && ' . $cmd;
}
$cmd = $this->getExecString($cmd);
if (!$this->debug) {
$cmd .= ' 1>/dev/null 2>&1';
}
@ -99,10 +92,7 @@ class UnixShell
/* @phpstan-ignore-next-line */
logger()->debug(ConsoleColor::blue('[EXEC] ') . ConsoleColor::gray($cmd));
}
logger()->debug('Executed at: ' . debug_backtrace()[0]['file'] . ':' . debug_backtrace()[0]['line']);
if ($this->cd !== null) {
$cmd = 'cd ' . escapeshellarg($this->cd) . ' && ' . $cmd;
}
$cmd = $this->getExecString($cmd);
exec($cmd, $out, $code);
return [$code, $out];
}
@ -126,4 +116,21 @@ class UnixShell
}
return trim($str);
}
/**
* @param string $cmd
* @return string
*/
private function getExecString(string $cmd): string
{
logger()->debug('Executed at: ' . debug_backtrace()[0]['file'] . ':' . debug_backtrace()[0]['line']);
$env_str = $this->getEnvString();
if (!empty($env_str)) {
$cmd = "{$env_str} {$cmd}";
}
if ($this->cd !== null) {
$cmd = 'cd ' . escapeshellarg($this->cd) . ' && ' . $cmd;
}
return $cmd;
}
}