Process.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace Symfony\Components\Process;
  3. /*
  4. * This file is part of the symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Process is a thin wrapper around proc_* functions to ease
  13. * the forking processes from PHP.
  14. *
  15. * @package Symfony
  16. * @subpackage Components_Process
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. */
  19. class Process
  20. {
  21. protected $commandline;
  22. protected $cwd;
  23. protected $env;
  24. protected $stdin;
  25. protected $timeout;
  26. protected $options;
  27. protected $exitcode;
  28. protected $status;
  29. /**
  30. * Constructor.
  31. *
  32. * @param string $commandline The command line to run
  33. * @param string $cwd The working directory
  34. * @param array $env The environment variables
  35. * @param string $stdin The STDIN content
  36. * @param integer $timeout The timeout in seconds
  37. * @param array $options An array of options for proc_open
  38. */
  39. public function __construct($commandline, $cwd, array $env = array(), $stdin = null, $timeout = 60, array $options = array())
  40. {
  41. if (!function_exists('proc_open'))
  42. {
  43. throw new \RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
  44. }
  45. $this->commandline = $commandline;
  46. $this->cwd = null === $cwd ? getcwd() : $cwd;
  47. $this->env = array();
  48. foreach ($env as $key => $value)
  49. {
  50. $this->env[(binary) $key] = (binary) $value;
  51. }
  52. $this->stdin = $stdin;
  53. $this->timeout = $timeout;
  54. $this->options = array_merge($options, array('suppress_errors' => true, 'binary_pipes' => true));
  55. }
  56. /**
  57. * Forks and run the process.
  58. *
  59. * @param Closure|string|array $callback A PHP callback to run whenever there is some
  60. * output available on STDOUT or STDERR
  61. *
  62. * @return integer The exit status code
  63. */
  64. public function run($callback)
  65. {
  66. $descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
  67. $proccess = proc_open($this->commandline, $descriptors, $pipes, $this->cwd, $this->env, $this->options);
  68. stream_set_blocking($pipes[1], false);
  69. stream_set_blocking($pipes[2], false);
  70. if (!is_resource($proccess))
  71. {
  72. throw new \RuntimeException('Unable to launch a new process.');
  73. }
  74. if (!is_null($this->stdin))
  75. {
  76. fwrite($pipes[0], (binary) $this->stdin);
  77. }
  78. fclose($pipes[0]);
  79. while (true)
  80. {
  81. $r = $pipes;
  82. $w = null;
  83. $e = null;
  84. $n = @stream_select($r, $w, $e, $this->timeout);
  85. if ($n === false)
  86. {
  87. break;
  88. }
  89. elseif ($n === 0)
  90. {
  91. proc_terminate($proccess);
  92. throw new \RuntimeException('The process timed out.');
  93. }
  94. elseif ($n > 0)
  95. {
  96. $called = false;
  97. while (true)
  98. {
  99. $c = false;
  100. if ($line = (binary) fgets($pipes[1], 1024))
  101. {
  102. $called = $c = true;
  103. call_user_func($callback, 'out', $line);
  104. }
  105. if ($line = fgets($pipes[2], 1024))
  106. {
  107. $called = $c = true;
  108. call_user_func($callback, 'err', $line);
  109. }
  110. if (!$c)
  111. {
  112. break;
  113. }
  114. }
  115. if (!$called)
  116. {
  117. break;
  118. }
  119. }
  120. }
  121. $this->status = proc_get_status($proccess);
  122. proc_close($proccess);
  123. if ($this->status['signaled'])
  124. {
  125. throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->status['stopsig']));
  126. }
  127. return $this->exitcode = $this->status['exitcode'];
  128. }
  129. /**
  130. * Returns the exit code returned by the process.
  131. *
  132. * @return integer The exit status code
  133. */
  134. public function getExitCode()
  135. {
  136. return $this->exitcode;
  137. }
  138. /**
  139. * Returns true if the child process has been terminated by an uncaught signal.
  140. *
  141. * It always returns false on Windows.
  142. *
  143. * @return Boolean
  144. */
  145. public function hasBeenSignaled()
  146. {
  147. return $this->status['signaled'];
  148. }
  149. /**
  150. * Returns the number of the signal that caused the child process to terminate its execution.
  151. *
  152. * It is only meaningful if hasBeenSignaled() returns true.
  153. *
  154. * @return integer
  155. */
  156. public function getTermSignal()
  157. {
  158. return $this->status['termsig'];
  159. }
  160. /**
  161. * Returns true if the child process has been stopped by a signal.
  162. *
  163. * It always returns false on Windows.
  164. *
  165. * @return Boolean
  166. */
  167. public function hasBeenStopped()
  168. {
  169. return $this->status['stopped'];
  170. }
  171. /**
  172. * Returns the number of the signal that caused the child process to stop its execution
  173. *
  174. * It is only meaningful if hasBeenStopped() returns true.
  175. *
  176. * @return integer
  177. */
  178. public function getStopSignal()
  179. {
  180. return $this->status['stopsig'];
  181. }
  182. }