ProcessExecutor.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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('{(://[^:/\s]+:)[^@\s/]+}i', '$1****', $command);
  41. $this->io->writeError('Executing command ('.($cwd ?: 'CWD').'): '.$safeCommand);
  42. }
  43. // make sure that null translate to the proper directory in case the dir is a symlink
  44. // and we call a git command, because msysgit does not handle symlinks properly
  45. if (null === $cwd && defined('PHP_WINDOWS_VERSION_BUILD') && false !== strpos($command, 'git') && getcwd()) {
  46. $cwd = realpath(getcwd());
  47. }
  48. $this->captureOutput = count(func_get_args()) > 1;
  49. $this->errorOutput = null;
  50. $process = new Process($command, $cwd, array('LANGUAGE' => 'C') + $_ENV + $_SERVER, null, static::getTimeout());
  51. $callback = is_callable($output) ? $output : array($this, 'outputHandler');
  52. $process->run($callback);
  53. if ($this->captureOutput && !is_callable($output)) {
  54. $output = $process->getOutput();
  55. }
  56. $this->errorOutput = $process->getErrorOutput();
  57. return $process->getExitCode();
  58. }
  59. public function splitLines($output)
  60. {
  61. $output = trim($output);
  62. return ((string) $output === '') ? array() : preg_split('{\r?\n}', $output);
  63. }
  64. /**
  65. * Get any error output from the last command
  66. *
  67. * @return string
  68. */
  69. public function getErrorOutput()
  70. {
  71. return $this->errorOutput;
  72. }
  73. public function outputHandler($type, $buffer)
  74. {
  75. if ($this->captureOutput) {
  76. return;
  77. }
  78. echo $buffer;
  79. }
  80. public static function getTimeout()
  81. {
  82. return static::$timeout;
  83. }
  84. public static function setTimeout($timeout)
  85. {
  86. static::$timeout = $timeout;
  87. }
  88. /**
  89. * Escapes a string to be used as a shell argument.
  90. *
  91. * @param string $argument The argument that will be escaped
  92. *
  93. * @return string The escaped argument
  94. */
  95. public static function escape($argument)
  96. {
  97. return ProcessUtils::escapeArgument($argument);
  98. }
  99. }