ProcessExecutor.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Composer\IO\IOInterface;
  13. use Symfony\Component\Process\Process;
  14. use Symfony\Component\Process\ProcessUtils;
  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 = func_num_args() > 1;
  54. $this->errorOutput = null;
  55. // TODO in v3, commands should be passed in as arrays of cmd + args
  56. if (method_exists('Symfony\Component\Process\Process', 'fromShellCommandline')) {
  57. $process = Process::fromShellCommandline($command, $cwd, null, null, static::getTimeout());
  58. } else {
  59. $process = new Process($command, $cwd, null, null, static::getTimeout());
  60. }
  61. $callback = is_callable($output) ? $output : array($this, 'outputHandler');
  62. $process->run($callback);
  63. if ($this->captureOutput && !is_callable($output)) {
  64. $output = $process->getOutput();
  65. }
  66. $this->errorOutput = $process->getErrorOutput();
  67. return $process->getExitCode();
  68. }
  69. public function splitLines($output)
  70. {
  71. $output = trim($output);
  72. return ((string) $output === '') ? array() : preg_split('{\r?\n}', $output);
  73. }
  74. /**
  75. * Get any error output from the last command
  76. *
  77. * @return string
  78. */
  79. public function getErrorOutput()
  80. {
  81. return $this->errorOutput;
  82. }
  83. public function outputHandler($type, $buffer)
  84. {
  85. if ($this->captureOutput) {
  86. return;
  87. }
  88. if (null === $this->io) {
  89. echo $buffer;
  90. return;
  91. }
  92. if (Process::ERR === $type) {
  93. $this->io->writeError($buffer, false);
  94. } else {
  95. $this->io->write($buffer, false);
  96. }
  97. }
  98. public static function getTimeout()
  99. {
  100. return static::$timeout;
  101. }
  102. public static function setTimeout($timeout)
  103. {
  104. static::$timeout = $timeout;
  105. }
  106. /**
  107. * Escapes a string to be used as a shell argument.
  108. *
  109. * @param string $argument The argument that will be escaped
  110. *
  111. * @return string The escaped argument
  112. */
  113. public static function escape($argument)
  114. {
  115. return self::escapeArgument($argument);
  116. }
  117. /**
  118. * Copy of ProcessUtils::escapeArgument() that is deprecated in Symfony 3.3 and removed in Symfony 4.
  119. *
  120. * @param string $argument
  121. *
  122. * @return string
  123. */
  124. private static function escapeArgument($argument)
  125. {
  126. //Fix for PHP bug #43784 escapeshellarg removes % from given string
  127. //Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
  128. //@see https://bugs.php.net/bug.php?id=43784
  129. //@see https://bugs.php.net/bug.php?id=49446
  130. if ('\\' === DIRECTORY_SEPARATOR) {
  131. if ('' === $argument) {
  132. return escapeshellarg($argument);
  133. }
  134. $escapedArgument = '';
  135. $quote = false;
  136. foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
  137. if ('"' === $part) {
  138. $escapedArgument .= '\\"';
  139. } elseif (self::isSurroundedBy($part, '%')) {
  140. // Avoid environment variable expansion
  141. $escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
  142. } else {
  143. // escape trailing backslash
  144. if ('\\' === substr($part, -1)) {
  145. $part .= '\\';
  146. }
  147. $quote = true;
  148. $escapedArgument .= $part;
  149. }
  150. }
  151. if ($quote) {
  152. $escapedArgument = '"'.$escapedArgument.'"';
  153. }
  154. return $escapedArgument;
  155. }
  156. return "'".str_replace("'", "'\\''", $argument)."'";
  157. }
  158. private static function isSurroundedBy($arg, $char)
  159. {
  160. return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1];
  161. }
  162. }