Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Helper; Chris@0: Chris@0: use Symfony\Component\Console\Output\ConsoleOutputInterface; Chris@0: use Symfony\Component\Console\Output\OutputInterface; Chris@0: use Symfony\Component\Process\Exception\ProcessFailedException; Chris@0: use Symfony\Component\Process\Process; Chris@0: Chris@0: /** Chris@0: * The ProcessHelper class provides helpers to run external processes. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class ProcessHelper extends Helper Chris@0: { Chris@0: /** Chris@0: * Runs an external process. Chris@0: * Chris@0: * @param OutputInterface $output An OutputInterface instance Chris@0: * @param string|array|Process $cmd An instance of Process or an array of arguments to escape and run or a command to run Chris@0: * @param string|null $error An error message that must be displayed if something went wrong Chris@0: * @param callable|null $callback A PHP callback to run whenever there is some Chris@0: * output available on STDOUT or STDERR Chris@0: * @param int $verbosity The threshold for verbosity Chris@0: * Chris@0: * @return Process The process that ran Chris@0: */ Chris@0: public function run(OutputInterface $output, $cmd, $error = null, callable $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE) Chris@0: { Chris@0: if ($output instanceof ConsoleOutputInterface) { Chris@0: $output = $output->getErrorOutput(); Chris@0: } Chris@0: Chris@0: $formatter = $this->getHelperSet()->get('debug_formatter'); Chris@0: Chris@14: if ($cmd instanceof Process) { Chris@0: $process = $cmd; Chris@0: } else { Chris@0: $process = new Process($cmd); Chris@0: } Chris@0: Chris@0: if ($verbosity <= $output->getVerbosity()) { Chris@0: $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine()))); Chris@0: } Chris@0: Chris@0: if ($output->isDebug()) { Chris@0: $callback = $this->wrapCallback($output, $process, $callback); Chris@0: } Chris@0: Chris@0: $process->run($callback); Chris@0: Chris@0: if ($verbosity <= $output->getVerbosity()) { Chris@0: $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode()); Chris@0: $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful())); Chris@0: } Chris@0: Chris@0: if (!$process->isSuccessful() && null !== $error) { Chris@0: $output->writeln(sprintf('%s', $this->escapeString($error))); Chris@0: } Chris@0: Chris@0: return $process; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Runs the process. Chris@0: * Chris@0: * This is identical to run() except that an exception is thrown if the process Chris@0: * exits with a non-zero exit code. Chris@0: * Chris@0: * @param OutputInterface $output An OutputInterface instance Chris@0: * @param string|Process $cmd An instance of Process or a command to run Chris@0: * @param string|null $error An error message that must be displayed if something went wrong Chris@0: * @param callable|null $callback A PHP callback to run whenever there is some Chris@0: * output available on STDOUT or STDERR Chris@0: * Chris@0: * @return Process The process that ran Chris@0: * Chris@0: * @throws ProcessFailedException Chris@0: * Chris@0: * @see run() Chris@0: */ Chris@0: public function mustRun(OutputInterface $output, $cmd, $error = null, callable $callback = null) Chris@0: { Chris@0: $process = $this->run($output, $cmd, $error, $callback); Chris@0: Chris@0: if (!$process->isSuccessful()) { Chris@0: throw new ProcessFailedException($process); Chris@0: } Chris@0: Chris@0: return $process; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Wraps a Process callback to add debugging output. Chris@0: * Chris@0: * @param OutputInterface $output An OutputInterface interface Chris@0: * @param Process $process The Process Chris@0: * @param callable|null $callback A PHP callable Chris@0: * Chris@0: * @return callable Chris@0: */ Chris@0: public function wrapCallback(OutputInterface $output, Process $process, callable $callback = null) Chris@0: { Chris@0: if ($output instanceof ConsoleOutputInterface) { Chris@0: $output = $output->getErrorOutput(); Chris@0: } Chris@0: Chris@0: $formatter = $this->getHelperSet()->get('debug_formatter'); Chris@0: Chris@0: return function ($type, $buffer) use ($output, $process, $callback, $formatter) { Chris@0: $output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), Process::ERR === $type)); Chris@0: Chris@0: if (null !== $callback) { Chris@17: \call_user_func($callback, $type, $buffer); Chris@0: } Chris@0: }; Chris@0: } Chris@0: Chris@0: private function escapeString($str) Chris@0: { Chris@0: return str_replace('<', '\\<', $str); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getName() Chris@0: { Chris@0: return 'process'; Chris@0: } Chris@0: }