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;
20 * The ProcessHelper class provides helpers to run external processes.
22 * @author Fabien Potencier <fabien@symfony.com>
24 class ProcessHelper extends Helper
27 * Runs an external process.
29 * @param OutputInterface $output An OutputInterface instance
30 * @param string|array|Process $cmd An instance of Process or an array of arguments to escape and run or a command to run
31 * @param string|null $error An error message that must be displayed if something went wrong
32 * @param callable|null $callback A PHP callback to run whenever there is some
33 * output available on STDOUT or STDERR
34 * @param int $verbosity The threshold for verbosity
36 * @return Process The process that ran
38 public function run(OutputInterface $output, $cmd, $error = null, callable $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
40 if ($output instanceof ConsoleOutputInterface) {
41 $output = $output->getErrorOutput();
44 $formatter = $this->getHelperSet()->get('debug_formatter');
46 if ($cmd instanceof Process) {
49 $process = new Process($cmd);
52 if ($verbosity <= $output->getVerbosity()) {
53 $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine())));
56 if ($output->isDebug()) {
57 $callback = $this->wrapCallback($output, $process, $callback);
60 $process->run($callback);
62 if ($verbosity <= $output->getVerbosity()) {
63 $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode());
64 $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful()));
67 if (!$process->isSuccessful() && null !== $error) {
68 $output->writeln(sprintf('<error>%s</error>', $this->escapeString($error)));
77 * This is identical to run() except that an exception is thrown if the process
78 * exits with a non-zero exit code.
80 * @param OutputInterface $output An OutputInterface instance
81 * @param string|Process $cmd An instance of Process or a command to run
82 * @param string|null $error An error message that must be displayed if something went wrong
83 * @param callable|null $callback A PHP callback to run whenever there is some
84 * output available on STDOUT or STDERR
86 * @return Process The process that ran
88 * @throws ProcessFailedException
92 public function mustRun(OutputInterface $output, $cmd, $error = null, callable $callback = null)
94 $process = $this->run($output, $cmd, $error, $callback);
96 if (!$process->isSuccessful()) {
97 throw new ProcessFailedException($process);
104 * Wraps a Process callback to add debugging output.
106 * @param OutputInterface $output An OutputInterface interface
107 * @param Process $process The Process
108 * @param callable|null $callback A PHP callable
112 public function wrapCallback(OutputInterface $output, Process $process, callable $callback = null)
114 if ($output instanceof ConsoleOutputInterface) {
115 $output = $output->getErrorOutput();
118 $formatter = $this->getHelperSet()->get('debug_formatter');
120 return function ($type, $buffer) use ($output, $process, $callback, $formatter) {
121 $output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), Process::ERR === $type));
123 if (null !== $callback) {
124 \call_user_func($callback, $type, $buffer);
129 private function escapeString($str)
131 return str_replace('<', '\\<', $str);
137 public function getName()