ProcessHelper.php 5.5 KB

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