Process.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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, $data) use ($that, $callback)
  89. {
  90. if ('out' == $type) {
  91. $that->addOutput($data);
  92. } else {
  93. $that->addErrorOutput($data);
  94. }
  95. if (null !== $callback) {
  96. call_user_func($callback, $type, $data);
  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. if (!is_resource($process)) {
  108. throw new \RuntimeException('Unable to launch a new process.');
  109. }
  110. foreach ($pipes as $pipe) {
  111. stream_set_blocking($pipe, false);
  112. }
  113. if (null === $this->stdin) {
  114. fclose($pipes[0]);
  115. $writePipes = null;
  116. } else {
  117. $writePipes = array($pipes[0]);
  118. $stdinLen = strlen($this->stdin);
  119. $stdinOffset = 0;
  120. }
  121. unset($pipes[0]);
  122. while ($pipes || $writePipes) {
  123. $r = $pipes;
  124. $w = $writePipes;
  125. $e = null;
  126. $n = @stream_select($r, $w, $e, $this->timeout);
  127. if (false === $n) {
  128. break;
  129. } elseif ($n === 0) {
  130. proc_terminate($process);
  131. throw new \RuntimeException('The process timed out.');
  132. }
  133. if ($w) {
  134. $written = fwrite($writePipes[0], (binary) substr($this->stdin, $stdinOffset), 8192);
  135. if (false !== $written) {
  136. $stdinOffset += $written;
  137. }
  138. if ($stdinOffset >= $stdinLen) {
  139. fclose($writePipes[0]);
  140. $writePipes = null;
  141. }
  142. }
  143. foreach ($r as $pipe) {
  144. $type = array_search($pipe, $pipes);
  145. $data = fread($pipe, 8192);
  146. if (strlen($data) > 0) {
  147. call_user_func($callback, $type == 1 ? 'out' : 'err', $data);
  148. }
  149. if (false === $data || feof($pipe)) {
  150. fclose($pipe);
  151. unset($pipes[$type]);
  152. }
  153. }
  154. }
  155. $this->status = proc_get_status($process);
  156. proc_close($process);
  157. if ($this->status['signaled']) {
  158. throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->status['stopsig']));
  159. }
  160. return $this->exitcode = $this->status['exitcode'];
  161. }
  162. /**
  163. * Returns the output of the process (STDOUT).
  164. *
  165. * This only returns the output if you have not supplied a callback
  166. * to the run() method.
  167. *
  168. * @return string The process output
  169. *
  170. * @api
  171. */
  172. public function getOutput()
  173. {
  174. return $this->stdout;
  175. }
  176. /**
  177. * Returns the error output of the process (STDERR).
  178. *
  179. * This only returns the error output if you have not supplied a callback
  180. * to the run() method.
  181. *
  182. * @return string The process error output
  183. *
  184. * @api
  185. */
  186. public function getErrorOutput()
  187. {
  188. return $this->stderr;
  189. }
  190. /**
  191. * Returns the exit code returned by the process.
  192. *
  193. * @return integer The exit status code
  194. *
  195. * @api
  196. */
  197. public function getExitCode()
  198. {
  199. return $this->exitcode;
  200. }
  201. /**
  202. * Checks if the process ended successfully.
  203. *
  204. * @return Boolean true if the process ended successfully, false otherwise
  205. *
  206. * @api
  207. */
  208. public function isSuccessful()
  209. {
  210. return 0 == $this->exitcode;
  211. }
  212. /**
  213. * Returns true if the child process has been terminated by an uncaught signal.
  214. *
  215. * It always returns false on Windows.
  216. *
  217. * @return Boolean
  218. *
  219. * @api
  220. */
  221. public function hasBeenSignaled()
  222. {
  223. return $this->status['signaled'];
  224. }
  225. /**
  226. * Returns the number of the signal that caused the child process to terminate its execution.
  227. *
  228. * It is only meaningful if hasBeenSignaled() returns true.
  229. *
  230. * @return integer
  231. *
  232. * @api
  233. */
  234. public function getTermSignal()
  235. {
  236. return $this->status['termsig'];
  237. }
  238. /**
  239. * Returns true if the child process has been stopped by a signal.
  240. *
  241. * It always returns false on Windows.
  242. *
  243. * @return Boolean
  244. *
  245. * @api
  246. */
  247. public function hasBeenStopped()
  248. {
  249. return $this->status['stopped'];
  250. }
  251. /**
  252. * Returns the number of the signal that caused the child process to stop its execution
  253. *
  254. * It is only meaningful if hasBeenStopped() returns true.
  255. *
  256. * @return integer
  257. *
  258. * @api
  259. */
  260. public function getStopSignal()
  261. {
  262. return $this->status['stopsig'];
  263. }
  264. public function addOutput($line)
  265. {
  266. $this->stdout .= $line;
  267. }
  268. public function addErrorOutput($line)
  269. {
  270. $this->stderr .= $line;
  271. }
  272. public function getCommandLine()
  273. {
  274. return $this->commandline;
  275. }
  276. public function setCommandLine($commandline)
  277. {
  278. $this->commandline = $commandline;
  279. }
  280. public function getTimeout()
  281. {
  282. return $this->timeout;
  283. }
  284. public function setTimeout($timeout)
  285. {
  286. $this->timeout = $timeout;
  287. }
  288. public function getWorkingDirectory()
  289. {
  290. return $this->cwd;
  291. }
  292. public function setWorkingDirectory($cwd)
  293. {
  294. $this->cwd = $cwd;
  295. }
  296. public function getEnv()
  297. {
  298. return $this->env;
  299. }
  300. public function setEnv(array $env)
  301. {
  302. $this->env = $env;
  303. }
  304. public function getStdin()
  305. {
  306. return $this->stdin;
  307. }
  308. public function setStdin($stdin)
  309. {
  310. $this->stdin = $stdin;
  311. }
  312. public function getOptions()
  313. {
  314. return $this->options;
  315. }
  316. public function setOptions(array $options)
  317. {
  318. $this->options = $options;
  319. }
  320. }