No Description

ProcessHelper.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\OutputInterface;
  12. use Symfony\Component\Process\Exception\ProcessFailedException;
  13. use Symfony\Component\Process\Process;
  14. use Symfony\Component\Process\ProcessBuilder;
  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, $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
  35. {
  36. $formatter = $this->getHelperSet()->get('debug_formatter');
  37. if (is_array($cmd)) {
  38. $process = ProcessBuilder::create($cmd)->getProcess();
  39. } elseif ($cmd instanceof Process) {
  40. $process = $cmd;
  41. } else {
  42. $process = new Process($cmd);
  43. }
  44. if ($verbosity <= $output->getVerbosity()) {
  45. $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine())));
  46. }
  47. if ($output->isDebug()) {
  48. $callback = $this->wrapCallback($output, $process, $callback);
  49. }
  50. $process->run($callback);
  51. if ($verbosity <= $output->getVerbosity()) {
  52. $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode());
  53. $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful()));
  54. }
  55. if (!$process->isSuccessful() && null !== $error) {
  56. $output->writeln(sprintf('<error>%s</error>', $this->escapeString($error)));
  57. }
  58. return $process;
  59. }
  60. /**
  61. * Runs the process.
  62. *
  63. * This is identical to run() except that an exception is thrown if the process
  64. * exits with a non-zero exit code.
  65. *
  66. * @param OutputInterface $output An OutputInterface instance
  67. * @param string|Process $cmd An instance of Process or a command to run
  68. * @param string|null $error An error message that must be displayed if something went wrong
  69. * @param callable|null $callback A PHP callback to run whenever there is some
  70. * output available on STDOUT or STDERR
  71. *
  72. * @return Process The process that ran
  73. *
  74. * @throws ProcessFailedException
  75. *
  76. * @see run()
  77. */
  78. public function mustRun(OutputInterface $output, $cmd, $error = null, $callback = null)
  79. {
  80. $process = $this->run($output, $cmd, $error, $callback);
  81. if (!$process->isSuccessful()) {
  82. throw new ProcessFailedException($process);
  83. }
  84. return $process;
  85. }
  86. /**
  87. * Wraps a Process callback to add debugging output.
  88. *
  89. * @param OutputInterface $output An OutputInterface interface
  90. * @param Process $process The Process
  91. * @param callable|null $callback A PHP callable
  92. *
  93. * @return callable
  94. */
  95. public function wrapCallback(OutputInterface $output, Process $process, $callback = null)
  96. {
  97. $formatter = $this->getHelperSet()->get('debug_formatter');
  98. $that = $this;
  99. return function ($type, $buffer) use ($output, $process, $callback, $formatter, $that) {
  100. $output->write($formatter->progress(spl_object_hash($process), $that->escapeString($buffer), Process::ERR === $type));
  101. if (null !== $callback) {
  102. call_user_func($callback, $type, $buffer);
  103. }
  104. };
  105. }
  106. /**
  107. * This method is public for PHP 5.3 compatibility, it should be private.
  108. *
  109. * @internal
  110. */
  111. public function escapeString($str)
  112. {
  113. return str_replace('<', '\\<', $str);
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function getName()
  119. {
  120. return 'process';
  121. }
  122. }