4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Helper;
14 use Symfony\Component\Console\Output\ConsoleOutputInterface;
15 use Symfony\Component\Console\Output\OutputInterface;
16 use Symfony\Component\Process\Exception\ProcessFailedException;
17 use Symfony\Component\Process\Process;
18 use Symfony\Component\Process\ProcessBuilder;
21 * The ProcessHelper class provides helpers to run external processes.
23 * @author Fabien Potencier <fabien@symfony.com>
25 class ProcessHelper extends Helper
28 * Runs an external process.
30 * @param OutputInterface $output An OutputInterface instance
31 * @param string|array|Process $cmd An instance of Process or an array of arguments to escape and run or a command to run
32 * @param string|null $error An error message that must be displayed if something went wrong
33 * @param callable|null $callback A PHP callback to run whenever there is some
34 * output available on STDOUT or STDERR
35 * @param int $verbosity The threshold for verbosity
37 * @return Process The process that ran
39 public function run(OutputInterface $output, $cmd, $error = null, $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
41 if ($output instanceof ConsoleOutputInterface) {
42 $output = $output->getErrorOutput();
45 $formatter = $this->getHelperSet()->get('debug_formatter');
48 $process = ProcessBuilder::create($cmd)->getProcess();
49 } elseif ($cmd instanceof Process) {
52 $process = new Process($cmd);
55 if ($verbosity <= $output->getVerbosity()) {
56 $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine())));
59 if ($output->isDebug()) {
60 $callback = $this->wrapCallback($output, $process, $callback);
63 $process->run($callback);
65 if ($verbosity <= $output->getVerbosity()) {
66 $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode());
67 $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful()));
70 if (!$process->isSuccessful() && null !== $error) {
71 $output->writeln(sprintf('<error>%s</error>', $this->escapeString($error)));
80 * This is identical to run() except that an exception is thrown if the process
81 * exits with a non-zero exit code.
83 * @param OutputInterface $output An OutputInterface instance
84 * @param string|Process $cmd An instance of Process or a command to run
85 * @param string|null $error An error message that must be displayed if something went wrong
86 * @param callable|null $callback A PHP callback to run whenever there is some
87 * output available on STDOUT or STDERR
89 * @return Process The process that ran
91 * @throws ProcessFailedException
95 public function mustRun(OutputInterface $output, $cmd, $error = null, $callback = null)
97 $process = $this->run($output, $cmd, $error, $callback);
99 if (!$process->isSuccessful()) {
100 throw new ProcessFailedException($process);
107 * Wraps a Process callback to add debugging output.
109 * @param OutputInterface $output An OutputInterface interface
110 * @param Process $process The Process
111 * @param callable|null $callback A PHP callable
115 public function wrapCallback(OutputInterface $output, Process $process, $callback = null)
117 if ($output instanceof ConsoleOutputInterface) {
118 $output = $output->getErrorOutput();
121 $formatter = $this->getHelperSet()->get('debug_formatter');
125 return function ($type, $buffer) use ($output, $process, $callback, $formatter, $that) {
126 $output->write($formatter->progress(spl_object_hash($process), $that->escapeString($buffer), Process::ERR === $type));
128 if (null !== $callback) {
129 call_user_func($callback, $type, $buffer);
135 * This method is public for PHP 5.3 compatibility, it should be private.
139 public function escapeString($str)
141 return str_replace('<', '\\<', $str);
147 public function getName()