Process.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 = null, $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. if (null !== $env) {
  53. $this->env = array();
  54. foreach ($env as $key => $value) {
  55. $this->env[(binary) $key] = (binary) $value;
  56. }
  57. } else {
  58. $this->env = null;
  59. }
  60. $this->stdin = $stdin;
  61. $this->timeout = $timeout;
  62. $this->options = array_merge(array('suppress_errors' => true, 'binary_pipes' => true, 'bypass_shell' => false), $options);
  63. }
  64. /**
  65. * Runs the process.
  66. *
  67. * The callback receives the type of output (out or err) and
  68. * some bytes from the output in real-time. It allows to have feedback
  69. * from the independent process during execution.
  70. *
  71. * The STDOUT and STDERR are also available after the process is finished
  72. * via the getOutput() and getErrorOutput() methods.
  73. *
  74. * @param Closure|string|array $callback A PHP callback to run whenever there is some
  75. * output available on STDOUT or STDERR
  76. *
  77. * @return integer The exit status code
  78. *
  79. * @throws \RuntimeException When process can't be launch or is stopped
  80. *
  81. * @api
  82. */
  83. public function run($callback = null)
  84. {
  85. $this->stdout = '';
  86. $this->stderr = '';
  87. $that = $this;
  88. $callback = function ($type, $line) use ($that, $callback)
  89. {
  90. if ('out' == $type) {
  91. $that->addOutput($line);
  92. } else {
  93. $that->addErrorOutput($line);
  94. }
  95. if (null !== $callback) {
  96. call_user_func($callback, $type, $line);
  97. }
  98. };
  99. // Workaround for http://bugs.php.net/bug.php?id=51800
  100. if (strstr(PHP_OS, 'WIN')) {
  101. $stderrPipeMode = 'a';
  102. } else {
  103. $stderrPipeMode = 'w';
  104. }
  105. $descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', $stderrPipeMode));
  106. $process = proc_open($this->commandline, $descriptors, $pipes, $this->cwd, $this->env, $this->options);
  107. stream_set_blocking($pipes[1], false);
  108. stream_set_blocking($pipes[2], false);
  109. if (!is_resource($process)) {
  110. throw new \RuntimeException('Unable to launch a new process.');
  111. }
  112. if (null !== $this->stdin) {
  113. fwrite($pipes[0], (binary) $this->stdin);
  114. }
  115. fclose($pipes[0]);
  116. while (true) {
  117. $r = $pipes;
  118. $w = null;
  119. $e = null;
  120. $n = @stream_select($r, $w, $e, $this->timeout);
  121. if (false === $n) {
  122. break;
  123. } elseif ($n === 0) {
  124. proc_terminate($process);
  125. throw new \RuntimeException('The process timed out.');
  126. } elseif ($n > 0) {
  127. $called = false;
  128. while (true) {
  129. $c = false;
  130. if ($line = (binary) fgets($pipes[1], 1024)) {
  131. $called = $c = true;
  132. call_user_func($callback, 'out', $line);
  133. }
  134. if ($line = fgets($pipes[2], 1024)) {
  135. $called = $c = true;
  136. call_user_func($callback, 'err', $line);
  137. }
  138. if (!$c) {
  139. break;
  140. }
  141. }
  142. if (!$called) {
  143. break;
  144. }
  145. }
  146. }
  147. $this->status = proc_get_status($process);
  148. proc_close($process);
  149. if ($this->status['signaled']) {
  150. throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->status['stopsig']));
  151. }
  152. return $this->exitcode = $this->status['exitcode'];
  153. }
  154. /**
  155. * Returns the output of the process (STDOUT).
  156. *
  157. * This only returns the output if you have not supplied a callback
  158. * to the run() method.
  159. *
  160. * @return string The process output
  161. *
  162. * @api
  163. */
  164. public function getOutput()
  165. {
  166. return $this->stdout;
  167. }
  168. /**
  169. * Returns the error output of the process (STDERR).
  170. *
  171. * This only returns the error output if you have not supplied a callback
  172. * to the run() method.
  173. *
  174. * @return string The process error output
  175. *
  176. * @api
  177. */
  178. public function getErrorOutput()
  179. {
  180. return $this->stderr;
  181. }
  182. /**
  183. * Returns the exit code returned by the process.
  184. *
  185. * @return integer The exit status code
  186. *
  187. * @api
  188. */
  189. public function getExitCode()
  190. {
  191. return $this->exitcode;
  192. }
  193. /**
  194. * Checks if the process ended successfully.
  195. *
  196. * @return Boolean true if the process ended successfully, false otherwise
  197. *
  198. * @api
  199. */
  200. public function isSuccessful()
  201. {
  202. return 0 == $this->exitcode;
  203. }
  204. /**
  205. * Returns true if the child process has been terminated by an uncaught signal.
  206. *
  207. * It always returns false on Windows.
  208. *
  209. * @return Boolean
  210. *
  211. * @api
  212. */
  213. public function hasBeenSignaled()
  214. {
  215. return $this->status['signaled'];
  216. }
  217. /**
  218. * Returns the number of the signal that caused the child process to terminate its execution.
  219. *
  220. * It is only meaningful if hasBeenSignaled() returns true.
  221. *
  222. * @return integer
  223. *
  224. * @api
  225. */
  226. public function getTermSignal()
  227. {
  228. return $this->status['termsig'];
  229. }
  230. /**
  231. * Returns true if the child process has been stopped by a signal.
  232. *
  233. * It always returns false on Windows.
  234. *
  235. * @return Boolean
  236. *
  237. * @api
  238. */
  239. public function hasBeenStopped()
  240. {
  241. return $this->status['stopped'];
  242. }
  243. /**
  244. * Returns the number of the signal that caused the child process to stop its execution
  245. *
  246. * It is only meaningful if hasBeenStopped() returns true.
  247. *
  248. * @return integer
  249. *
  250. * @api
  251. */
  252. public function getStopSignal()
  253. {
  254. return $this->status['stopsig'];
  255. }
  256. public function addOutput($line)
  257. {
  258. $this->stdout .= $line;
  259. }
  260. public function addErrorOutput($line)
  261. {
  262. $this->stderr .= $line;
  263. }
  264. public function getCommandLine()
  265. {
  266. return $this->commandline;
  267. }
  268. public function setCommandLine($commandline)
  269. {
  270. $this->commandline = $commandline;
  271. }
  272. public function getTimeout()
  273. {
  274. return $this->timeout;
  275. }
  276. public function setTimeout($timeout)
  277. {
  278. $this->timeout = $timeout;
  279. }
  280. public function getWorkingDirectory()
  281. {
  282. return $this->cwd;
  283. }
  284. public function setWorkingDirectory($cwd)
  285. {
  286. $this->cwd = $cwd;
  287. }
  288. public function getEnv()
  289. {
  290. return $this->env;
  291. }
  292. public function setEnv(array $env)
  293. {
  294. $this->env = $env;
  295. }
  296. public function getStdin()
  297. {
  298. return $this->stdin;
  299. }
  300. public function setStdin($stdin)
  301. {
  302. $this->stdin = $stdin;
  303. }
  304. public function getOptions()
  305. {
  306. return $this->options;
  307. }
  308. public function setOptions(array $options)
  309. {
  310. $this->options = $options;
  311. }
  312. }