Process.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Process;
  11. /**
  12. * Process is a thin wrapper around proc_* functions to ease
  13. * start independent PHP processes.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @api
  18. */
  19. class Process
  20. {
  21. private $commandline;
  22. private $cwd;
  23. private $env;
  24. private $stdin;
  25. private $timeout;
  26. private $options;
  27. private $exitcode;
  28. private $status;
  29. private $stdout;
  30. private $stderr;
  31. /**
  32. * Constructor.
  33. *
  34. * @param string $commandline The command line to run
  35. * @param string $cwd The working directory
  36. * @param array $env The environment variables
  37. * @param string $stdin The STDIN content
  38. * @param integer $timeout The timeout in seconds
  39. * @param array $options An array of options for proc_open
  40. *
  41. * @throws \RuntimeException When proc_open is not installed
  42. *
  43. * @api
  44. */
  45. public function __construct($commandline, $cwd = null, array $env = array(), $stdin = null, $timeout = 60, array $options = array())
  46. {
  47. if (!function_exists('proc_open')) {
  48. throw new \RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
  49. }
  50. $this->commandline = $commandline;
  51. $this->cwd = null === $cwd ? getcwd() : $cwd;
  52. $this->env = array();
  53. foreach ($env as $key => $value) {
  54. $this->env[(binary) $key] = (binary) $value;
  55. }
  56. $this->stdin = $stdin;
  57. $this->timeout = $timeout;
  58. $this->options = array_merge($options, array('suppress_errors' => true, 'binary_pipes' => true));
  59. }
  60. /**
  61. * Runs the process.
  62. *
  63. * The callback receives the type of output (out or err) and
  64. * some bytes from the output in real-time. It allows to have feedback
  65. * from the independent process during execution.
  66. *
  67. * The STDOUT and STDERR are also available after the process is finished
  68. * via the getOutput() and getErrorOutput() methods.
  69. *
  70. * @param Closure|string|array $callback A PHP callback to run whenever there is some
  71. * output available on STDOUT or STDERR
  72. *
  73. * @return integer The exit status code
  74. *
  75. * @throws \RuntimeException When process can't be launch or is stopped
  76. *
  77. * @api
  78. */
  79. public function run($callback = null)
  80. {
  81. $this->stdout = '';
  82. $this->stderr = '';
  83. $that = $this;
  84. $callback = function ($type, $line) use ($that, $callback)
  85. {
  86. if ('out' == $type) {
  87. $that->addOutput($line);
  88. } else {
  89. $that->addErrorOutput($line);
  90. }
  91. if (null !== $callback) {
  92. call_user_func($callback, $type, $line);
  93. }
  94. };
  95. $descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
  96. $process = proc_open($this->commandline, $descriptors, $pipes, $this->cwd, $this->env, $this->options);
  97. stream_set_blocking($pipes[1], false);
  98. stream_set_blocking($pipes[2], false);
  99. if (!is_resource($process)) {
  100. throw new \RuntimeException('Unable to launch a new process.');
  101. }
  102. if (null !== $this->stdin) {
  103. fwrite($pipes[0], (binary) $this->stdin);
  104. }
  105. fclose($pipes[0]);
  106. while (true) {
  107. $r = $pipes;
  108. $w = null;
  109. $e = null;
  110. $n = @stream_select($r, $w, $e, $this->timeout);
  111. if (false === $n) {
  112. break;
  113. } elseif ($n === 0) {
  114. proc_terminate($process);
  115. throw new \RuntimeException('The process timed out.');
  116. } elseif ($n > 0) {
  117. $called = false;
  118. while (true) {
  119. $c = false;
  120. if ($line = (binary) fgets($pipes[1], 1024)) {
  121. $called = $c = true;
  122. call_user_func($callback, 'out', $line);
  123. }
  124. if ($line = fgets($pipes[2], 1024)) {
  125. $called = $c = true;
  126. call_user_func($callback, 'err', $line);
  127. }
  128. if (!$c) {
  129. break;
  130. }
  131. }
  132. if (!$called) {
  133. break;
  134. }
  135. }
  136. }
  137. $this->status = proc_get_status($process);
  138. proc_close($process);
  139. if ($this->status['signaled']) {
  140. throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->status['stopsig']));
  141. }
  142. return $this->exitcode = $this->status['exitcode'];
  143. }
  144. /**
  145. * Returns the output of the process (STDOUT).
  146. *
  147. * This only returns the output if you have not supplied a callback
  148. * to the run() method.
  149. *
  150. * @return string The process output
  151. *
  152. * @api
  153. */
  154. public function getOutput()
  155. {
  156. return $this->stdout;
  157. }
  158. /**
  159. * Returns the error output of the process (STDERR).
  160. *
  161. * This only returns the error output if you have not supplied a callback
  162. * to the run() method.
  163. *
  164. * @return string The process error output
  165. *
  166. * @api
  167. */
  168. public function getErrorOutput()
  169. {
  170. return $this->stderr;
  171. }
  172. /**
  173. * Returns the exit code returned by the process.
  174. *
  175. * @return integer The exit status code
  176. *
  177. * @api
  178. */
  179. public function getExitCode()
  180. {
  181. return $this->exitcode;
  182. }
  183. /**
  184. * Checks if the process ended successfully.
  185. *
  186. * @return Boolean true if the process ended successfully, false otherwise
  187. *
  188. * @api
  189. */
  190. public function isSuccessful()
  191. {
  192. return 0 == $this->exitcode;
  193. }
  194. /**
  195. * Returns true if the child process has been terminated by an uncaught signal.
  196. *
  197. * It always returns false on Windows.
  198. *
  199. * @return Boolean
  200. *
  201. * @api
  202. */
  203. public function hasBeenSignaled()
  204. {
  205. return $this->status['signaled'];
  206. }
  207. /**
  208. * Returns the number of the signal that caused the child process to terminate its execution.
  209. *
  210. * It is only meaningful if hasBeenSignaled() returns true.
  211. *
  212. * @return integer
  213. *
  214. * @api
  215. */
  216. public function getTermSignal()
  217. {
  218. return $this->status['termsig'];
  219. }
  220. /**
  221. * Returns true if the child process has been stopped by a signal.
  222. *
  223. * It always returns false on Windows.
  224. *
  225. * @return Boolean
  226. *
  227. * @api
  228. */
  229. public function hasBeenStopped()
  230. {
  231. return $this->status['stopped'];
  232. }
  233. /**
  234. * Returns the number of the signal that caused the child process to stop its execution
  235. *
  236. * It is only meaningful if hasBeenStopped() returns true.
  237. *
  238. * @return integer
  239. *
  240. * @api
  241. */
  242. public function getStopSignal()
  243. {
  244. return $this->status['stopsig'];
  245. }
  246. public function addOutput($line)
  247. {
  248. $this->stdout .= $line;
  249. }
  250. public function addErrorOutput($line)
  251. {
  252. $this->stderr .= $line;
  253. }
  254. public function getCommandLine()
  255. {
  256. return $this->commandline;
  257. }
  258. public function setCommandLine($commandline)
  259. {
  260. $this->commandline = $commandline;
  261. }
  262. }