ProcessHelper.php 4.7 KB

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