ProcessExecutor.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. use Symfony\Component\Process\Exception\RuntimeException;
  16. /**
  17. * @author Robert Schönthal <seroscho@googlemail.com>
  18. */
  19. class ProcessExecutor
  20. {
  21. protected static $timeout = 300;
  22. protected $captureOutput;
  23. protected $errorOutput;
  24. protected $io;
  25. public function __construct(IOInterface $io = null)
  26. {
  27. $this->io = $io;
  28. }
  29. /**
  30. * runs a process on the commandline
  31. *
  32. * @param string $command the command to execute
  33. * @param mixed $output the output will be written into this var if passed by ref
  34. * if a callable is passed it will be used as output handler
  35. * @param string $cwd the working directory
  36. * @return int statuscode
  37. */
  38. public function execute($command, &$output = null, $cwd = null)
  39. {
  40. if (func_num_args() > 1) {
  41. return $this->doExecute($command, $cwd, false, $output);
  42. }
  43. return $this->doExecute($command, $cwd, false);
  44. }
  45. /**
  46. * runs a process on the commandline in TTY mode
  47. *
  48. * @param string $command the command to execute
  49. * @param string $cwd the working directory
  50. * @return int statuscode
  51. */
  52. public function executeTty($command, $cwd = null)
  53. {
  54. return $this->doExecute($command, $cwd, true);
  55. }
  56. private function doExecute($command, $cwd, $tty, &$output = null)
  57. {
  58. if ($this->io && $this->io->isDebug()) {
  59. $safeCommand = preg_replace_callback('{://(?P<user>[^:/\s]+):(?P<password>[^@\s/]+)@}i', function ($m) {
  60. if (preg_match('{^[a-f0-9]{12,}$}', $m['user'])) {
  61. return '://***:***@';
  62. }
  63. return '://'.$m['user'].':***@';
  64. }, $command);
  65. $safeCommand = preg_replace("{--password (.*[^\\\\]\') }", '--password \'***\' ', $safeCommand);
  66. $this->io->writeError('Executing command ('.($cwd ?: 'CWD').'): '.$safeCommand);
  67. }
  68. // make sure that null translate to the proper directory in case the dir is a symlink
  69. // and we call a git command, because msysgit does not handle symlinks properly
  70. if (null === $cwd && Platform::isWindows() && false !== strpos($command, 'git') && getcwd()) {
  71. $cwd = realpath(getcwd());
  72. }
  73. $this->captureOutput = func_num_args() > 3;
  74. $this->errorOutput = null;
  75. // TODO in v3, commands should be passed in as arrays of cmd + args
  76. if (method_exists('Symfony\Component\Process\Process', 'fromShellCommandline')) {
  77. $process = Process::fromShellCommandline($command, $cwd, null, null, static::getTimeout());
  78. } else {
  79. $process = new Process($command, $cwd, null, null, static::getTimeout());
  80. }
  81. if (!Platform::isWindows() && $tty) {
  82. try {
  83. $process->setTty(true);
  84. } catch (RuntimeException $e) {
  85. // ignore TTY enabling errors
  86. }
  87. }
  88. $callback = is_callable($output) ? $output : array($this, 'outputHandler');
  89. $process->run($callback);
  90. if ($this->captureOutput && !is_callable($output)) {
  91. $output = $process->getOutput();
  92. }
  93. $this->errorOutput = $process->getErrorOutput();
  94. return $process->getExitCode();
  95. }
  96. public function splitLines($output)
  97. {
  98. $output = trim($output);
  99. return ((string) $output === '') ? array() : preg_split('{\r?\n}', $output);
  100. }
  101. /**
  102. * Get any error output from the last command
  103. *
  104. * @return string
  105. */
  106. public function getErrorOutput()
  107. {
  108. return $this->errorOutput;
  109. }
  110. public function outputHandler($type, $buffer)
  111. {
  112. if ($this->captureOutput) {
  113. return;
  114. }
  115. if (null === $this->io) {
  116. echo $buffer;
  117. return;
  118. }
  119. if (Process::ERR === $type) {
  120. $this->io->writeErrorRaw($buffer, false);
  121. } else {
  122. $this->io->writeRaw($buffer, false);
  123. }
  124. }
  125. public static function getTimeout()
  126. {
  127. return static::$timeout;
  128. }
  129. public static function setTimeout($timeout)
  130. {
  131. static::$timeout = $timeout;
  132. }
  133. /**
  134. * Escapes a string to be used as a shell argument.
  135. *
  136. * @param string $argument The argument that will be escaped
  137. *
  138. * @return string The escaped argument
  139. */
  140. public static function escape($argument)
  141. {
  142. return self::escapeArgument($argument);
  143. }
  144. /**
  145. * Copy of ProcessUtils::escapeArgument() that is deprecated in Symfony 3.3 and removed in Symfony 4.
  146. *
  147. * @param string $argument
  148. *
  149. * @return string
  150. */
  151. private static function escapeArgument($argument)
  152. {
  153. //Fix for PHP bug #43784 escapeshellarg removes % from given string
  154. //Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
  155. //@see https://bugs.php.net/bug.php?id=43784
  156. //@see https://bugs.php.net/bug.php?id=49446
  157. if ('\\' === DIRECTORY_SEPARATOR) {
  158. if ((string) $argument === '') {
  159. return escapeshellarg($argument);
  160. }
  161. $escapedArgument = '';
  162. $quote = false;
  163. foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
  164. if ('"' === $part) {
  165. $escapedArgument .= '\\"';
  166. } elseif (self::isSurroundedBy($part, '%')) {
  167. // Avoid environment variable expansion
  168. $escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
  169. } else {
  170. // escape trailing backslash
  171. if ('\\' === substr($part, -1)) {
  172. $part .= '\\';
  173. }
  174. $quote = true;
  175. $escapedArgument .= $part;
  176. }
  177. }
  178. if ($quote) {
  179. $escapedArgument = '"'.$escapedArgument.'"';
  180. }
  181. return $escapedArgument;
  182. }
  183. return "'".str_replace("'", "'\\''", $argument)."'";
  184. }
  185. private static function isSurroundedBy($arg, $char)
  186. {
  187. return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1];
  188. }
  189. }