ProcessExecutor.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Util;
  12. use Symfony\Component\Process\Process;
  13. use Composer\IO\IOInterface;
  14. /**
  15. * @author Robert Schönthal <seroscho@googlemail.com>
  16. */
  17. class ProcessExecutor
  18. {
  19. protected static $timeout = 300;
  20. protected $captureOutput;
  21. protected $errorOutput;
  22. protected $io;
  23. public function __construct(IOInterface $io = null)
  24. {
  25. $this->io = $io;
  26. }
  27. /**
  28. * runs a process on the commandline
  29. *
  30. * @param string $command the command to execute
  31. * @param mixed $output the output will be written into this var if passed by ref
  32. * if a callable is passed it will be used as output handler
  33. * @param string $cwd the working directory
  34. * @return int statuscode
  35. */
  36. public function execute($command, &$output = null, $cwd = null)
  37. {
  38. if ($this->io && $this->io->isDebug()) {
  39. $safeCommand = preg_replace('{(://[^:/\s]+:)[^@\s/]+}i', '$1****', $command);
  40. $this->io->write('Executing command ('.($cwd ?: 'CWD').'): '.$safeCommand);
  41. }
  42. // make sure that null translate to the proper directory in case the dir is a symlink
  43. // and we call a git command, because msysgit does not handle symlinks properly
  44. if (null === $cwd && defined('PHP_WINDOWS_VERSION_BUILD') && false !== strpos($command, 'git') && getcwd()) {
  45. $cwd = realpath(getcwd());
  46. }
  47. $this->captureOutput = count(func_get_args()) > 1;
  48. $this->errorOutput = null;
  49. $process = new Process($command, $cwd, null, null, static::getTimeout());
  50. $callback = is_callable($output) ? $output : array($this, 'outputHandler');
  51. $process->run($callback);
  52. if ($this->captureOutput && !is_callable($output)) {
  53. $output = $process->getOutput();
  54. }
  55. $this->errorOutput = $process->getErrorOutput();
  56. return $process->getExitCode();
  57. }
  58. public function splitLines($output)
  59. {
  60. $output = trim($output);
  61. return ((string) $output === '') ? array() : preg_split('{\r?\n}', $output);
  62. }
  63. /**
  64. * Get any error output from the last command
  65. *
  66. * @return string
  67. */
  68. public function getErrorOutput()
  69. {
  70. return $this->errorOutput;
  71. }
  72. public function outputHandler($type, $buffer)
  73. {
  74. if ($this->captureOutput) {
  75. return;
  76. }
  77. echo $buffer;
  78. }
  79. public static function getTimeout()
  80. {
  81. return static::$timeout;
  82. }
  83. public static function setTimeout($timeout)
  84. {
  85. static::$timeout = $timeout;
  86. }
  87. }