ProcessExecutor.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 Symfony\Component\Process\ProcessUtils;
  14. use Composer\IO\IOInterface;
  15. /**
  16. * @author Robert Schönthal <seroscho@googlemail.com>
  17. */
  18. class ProcessExecutor
  19. {
  20. protected static $timeout = 300;
  21. protected $captureOutput;
  22. protected $errorOutput;
  23. protected $io;
  24. public function __construct(IOInterface $io = null)
  25. {
  26. $this->io = $io;
  27. }
  28. /**
  29. * runs a process on the commandline
  30. *
  31. * @param string $command the command to execute
  32. * @param mixed $output the output will be written into this var if passed by ref
  33. * if a callable is passed it will be used as output handler
  34. * @param string $cwd the working directory
  35. * @return int statuscode
  36. */
  37. public function execute($command, &$output = null, $cwd = null)
  38. {
  39. if ($this->io && $this->io->isDebug()) {
  40. $safeCommand = preg_replace_callback('{(://)(?P<user>[^:/\s]+):(?P<password>[^@\s/]+)}i', function ($m) {
  41. if (preg_match('{^[a-f0-9]{12,}$}', $m['user'])) {
  42. return '://***:***';
  43. }
  44. return '://'.$m['user'].':***';
  45. }, $command);
  46. $this->io->writeError('Executing command ('.($cwd ?: 'CWD').'): '.$safeCommand);
  47. }
  48. // make sure that null translate to the proper directory in case the dir is a symlink
  49. // and we call a git command, because msysgit does not handle symlinks properly
  50. if (null === $cwd && Platform::isWindows() && false !== strpos($command, 'git') && getcwd()) {
  51. $cwd = realpath(getcwd());
  52. }
  53. $this->captureOutput = count(func_get_args()) > 1;
  54. $this->errorOutput = null;
  55. $process = new Process($command, $cwd, null, null, static::getTimeout());
  56. $callback = is_callable($output) ? $output : array($this, 'outputHandler');
  57. $process->run($callback);
  58. if ($this->captureOutput && !is_callable($output)) {
  59. $output = $process->getOutput();
  60. }
  61. $this->errorOutput = $process->getErrorOutput();
  62. return $process->getExitCode();
  63. }
  64. public function splitLines($output)
  65. {
  66. $output = trim($output);
  67. return ((string) $output === '') ? array() : preg_split('{\r?\n}', $output);
  68. }
  69. /**
  70. * Get any error output from the last command
  71. *
  72. * @return string
  73. */
  74. public function getErrorOutput()
  75. {
  76. return $this->errorOutput;
  77. }
  78. public function outputHandler($type, $buffer)
  79. {
  80. if ($this->captureOutput) {
  81. return;
  82. }
  83. echo $buffer;
  84. }
  85. public static function getTimeout()
  86. {
  87. return static::$timeout;
  88. }
  89. public static function setTimeout($timeout)
  90. {
  91. static::$timeout = $timeout;
  92. }
  93. /**
  94. * Escapes a string to be used as a shell argument.
  95. *
  96. * @param string $argument The argument that will be escaped
  97. *
  98. * @return string The escaped argument
  99. */
  100. public static function escape($argument)
  101. {
  102. return ProcessUtils::escapeArgument($argument);
  103. }
  104. }