/**
* @param Process $process
* @param bool $mustRun
* @param bool $quiet
*
* @return int|string
* @throws \Exception
*/
protected function runProcess(Process $process, $mustRun = false, $quiet = true)
{
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$this->output->writeln("Running command: <info>" . $process->getCommandLine() . "</info>");
}
try {
$process->mustRun($quiet ? null : function ($type, $buffer) {
$this->output->write(preg_replace('/^/m', ' ', $buffer));
});
} catch (ProcessFailedException $e) {
if (!$mustRun) {
return $process->getExitCode();
}
// The default for ProcessFailedException is to print the entire
// STDOUT and STDERR. But if $quiet is disabled, then the user will
// have already seen the command's output. So we need to re-throw
// the exception with a much shorter message.
$message = "The command failed with the exit code: " . $process->getExitCode();
$message .= "\n\nFull command: " . $process->getCommandLine();
if ($quiet) {
$message .= "\n\nError output:\n" . $process->getErrorOutput();
}
throw new \Exception($message);
}
$output = $process->getOutput();
return $output ? rtrim($output) : true;
}