executable = $executable; } public function add_flag(string $value): void { $this->args[] = $value; } public function add_escaped_arg(string $value): void { $this->args[] = escapeshellarg($value); } public function generate(): string { $command = escapeshellarg($this->executable); if (!empty($this->args)) { $command .= " "; $command .= join(" ", $this->args); } return escapeshellcmd($command)." 2>&1"; } public function combineOutput(string $empty_output = ""): string { if (empty($this->output)) { return $empty_output; } else { return implode("\r\n", $this->output); } } public function execute(bool $fail_on_non_zero_return = false): int { $cmd = $this->generate(); exec($cmd, $this->output, $ret); $output = $this->combineOutput("nothing"); log_debug('command_builder', "Command `$cmd` returned $ret and outputted $output"); if ($fail_on_non_zero_return && (int)$ret !== (int)0) { throw new ServerError("Command `$cmd` failed, returning $ret and outputting $output"); } return $ret; } }