ProcessHelper.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * @final
  21. */
  22. class ProcessHelper extends Helper
  23. {
  24. /**
  25. * Runs an external process.
  26. *
  27. * @param mixed[]|\Symfony\Component\Process\Process $cmd An instance of Process or an array of the command and arguments
  28. * @param callable|null $callback A PHP callback to run whenever there is some
  29. * output available on STDOUT or STDERR
  30. * @param \Symfony\Component\Console\Output\OutputInterface $output
  31. * @param string|null $error
  32. * @param int $verbosity
  33. */
  34. public function run($output, $cmd, $error = null, $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
  35. {
  36. if (!class_exists(Process::class)) {
  37. throw new \LogicException('The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".');
  38. }
  39. if ($output instanceof ConsoleOutputInterface) {
  40. $output = $output->getErrorOutput();
  41. }
  42. $formatter = $this->getHelperSet()->get('debug_formatter');
  43. if ($cmd instanceof Process) {
  44. $cmd = [$cmd];
  45. }
  46. if (\is_string($cmd[0] ?? null)) {
  47. $process = new Process($cmd);
  48. $cmd = [];
  49. } elseif (($cmd[0] ?? null) instanceof Process) {
  50. $process = $cmd[0];
  51. unset($cmd[0]);
  52. } else {
  53. throw new \InvalidArgumentException(sprintf('Invalid command provided to "%s()": the command should be an array whose first element is either the path to the binary to run or a "Process" object.', __METHOD__));
  54. }
  55. if ($verbosity <= $output->getVerbosity()) {
  56. $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine())));
  57. }
  58. if ($output->isDebug()) {
  59. $callback = $this->wrapCallback($output, $process, $callback);
  60. }
  61. $process->run($callback, $cmd);
  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()));
  65. }
  66. if (!$process->isSuccessful() && null !== $error) {
  67. $output->writeln(sprintf('<error>%s</error>', $this->escapeString($error)));
  68. }
  69. return $process;
  70. }
  71. /**
  72. * Runs the process.
  73. *
  74. * This is identical to run() except that an exception is thrown if the process
  75. * exits with a non-zero exit code.
  76. *
  77. * @param mixed[]|\Symfony\Component\Process\Process $cmd An instance of Process or a command to run
  78. * @param callable|null $callback A PHP callback to run whenever there is some
  79. * output available on STDOUT or STDERR
  80. *
  81. * @throws ProcessFailedException
  82. *
  83. * @see run()
  84. * @param \Symfony\Component\Console\Output\OutputInterface $output
  85. * @param string|null $error
  86. */
  87. public function mustRun($output, $cmd, $error = null, $callback = null)
  88. {
  89. $process = $this->run($output, $cmd, $error, $callback);
  90. if (!$process->isSuccessful()) {
  91. throw new ProcessFailedException($process);
  92. }
  93. return $process;
  94. }
  95. /**
  96. * Wraps a Process callback to add debugging output.
  97. * @param \Symfony\Component\Console\Output\OutputInterface $output
  98. * @param \Symfony\Component\Process\Process $process
  99. * @param callable|null $callback
  100. */
  101. public function wrapCallback($output, $process, $callback = null)
  102. {
  103. if ($output instanceof ConsoleOutputInterface) {
  104. $output = $output->getErrorOutput();
  105. }
  106. $formatter = $this->getHelperSet()->get('debug_formatter');
  107. return function ($type, $buffer) use ($output, $process, $callback, $formatter) {
  108. $output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), Process::ERR === $type));
  109. if (null !== $callback) {
  110. $callback($type, $buffer);
  111. }
  112. };
  113. }
  114. /**
  115. * @param string $str
  116. */
  117. private function escapeString($str)
  118. {
  119. return str_replace('<', '\\<', $str);
  120. }
  121. public function getName()
  122. {
  123. return 'process';
  124. }
  125. }