ProcessHelper.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Helper;
  11. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Component\Process\Exception\ProcessFailedException;
  14. use Symfony\Component\Process\Process;
  15. use Symfony\Component\Process\ProcessBuilder;
  16. /**
  17. * The ProcessHelper class provides helpers to run external processes.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class ProcessHelper extends Helper
  22. {
  23. /**
  24. * Runs an external process.
  25. *
  26. * @param OutputInterface $output An OutputInterface instance
  27. * @param string|array|Process $cmd An instance of Process or an array of arguments to escape and run or a command to run
  28. * @param string|null $error An error message that must be displayed if something went wrong
  29. * @param callable|null $callback A PHP callback to run whenever there is some
  30. * output available on STDOUT or STDERR
  31. * @param int $verbosity The threshold for verbosity
  32. *
  33. * @return Process The process that ran
  34. */
  35. public function run(OutputInterface $output, $cmd, $error = null, $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
  36. {
  37. if ($output instanceof ConsoleOutputInterface) {
  38. $output = $output->getErrorOutput();
  39. }
  40. $formatter = $this->getHelperSet()->get('debug_formatter');
  41. if (\is_array($cmd)) {
  42. $process = ProcessBuilder::create($cmd)->getProcess();
  43. } elseif ($cmd instanceof Process) {
  44. $process = $cmd;
  45. } else {
  46. $process = new Process($cmd);
  47. }
  48. if ($verbosity <= $output->getVerbosity()) {
  49. $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine())));
  50. }
  51. if ($output->isDebug()) {
  52. $callback = $this->wrapCallback($output, $process, $callback);
  53. }
  54. $process->run($callback);
  55. if ($verbosity <= $output->getVerbosity()) {
  56. $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode());
  57. $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful()));
  58. }
  59. if (!$process->isSuccessful() && null !== $error) {
  60. $output->writeln(sprintf('<error>%s</error>', $this->escapeString($error)));
  61. }
  62. return $process;
  63. }
  64. /**
  65. * Runs the process.
  66. *
  67. * This is identical to run() except that an exception is thrown if the process
  68. * exits with a non-zero exit code.
  69. *
  70. * @param OutputInterface $output An OutputInterface instance
  71. * @param string|Process $cmd An instance of Process or a command to run
  72. * @param string|null $error An error message that must be displayed if something went wrong
  73. * @param callable|null $callback A PHP callback to run whenever there is some
  74. * output available on STDOUT or STDERR
  75. *
  76. * @return Process The process that ran
  77. *
  78. * @throws ProcessFailedException
  79. *
  80. * @see run()
  81. */
  82. public function mustRun(OutputInterface $output, $cmd, $error = null, $callback = null)
  83. {
  84. $process = $this->run($output, $cmd, $error, $callback);
  85. if (!$process->isSuccessful()) {
  86. throw new ProcessFailedException($process);
  87. }
  88. return $process;
  89. }
  90. /**
  91. * Wraps a Process callback to add debugging output.
  92. *
  93. * @param OutputInterface $output An OutputInterface interface
  94. * @param Process $process The Process
  95. * @param callable|null $callback A PHP callable
  96. *
  97. * @return callable
  98. */
  99. public function wrapCallback(OutputInterface $output, Process $process, $callback = null)
  100. {
  101. if ($output instanceof ConsoleOutputInterface) {
  102. $output = $output->getErrorOutput();
  103. }
  104. $formatter = $this->getHelperSet()->get('debug_formatter');
  105. $that = $this;
  106. return function ($type, $buffer) use ($output, $process, $callback, $formatter, $that) {
  107. $output->write($formatter->progress(spl_object_hash($process), $that->escapeString($buffer), Process::ERR === $type));
  108. if (null !== $callback) {
  109. \call_user_func($callback, $type, $buffer);
  110. }
  111. };
  112. }
  113. /**
  114. * This method is public for PHP 5.3 compatibility, it should be private.
  115. *
  116. * @internal
  117. */
  118. public function escapeString($str)
  119. {
  120. return str_replace('<', '\\<', $str);
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getName()
  126. {
  127. return 'process';
  128. }
  129. }