PageRenderTime 49ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/src/Symfony/Component/Process/Process.php

https://github.com/tumf/tepco
PHP | 264 lines | 134 code | 33 blank | 97 comment | 16 complexity | 0fbb6c9a4d369ce9f236ff4851019aec MD5 | raw file
  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. class Process
  18. {
  19. protected $commandline;
  20. protected $cwd;
  21. protected $env;
  22. protected $stdin;
  23. protected $timeout;
  24. protected $options;
  25. protected $exitcode;
  26. protected $status;
  27. protected $stdout;
  28. protected $stderr;
  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. * @throws \RuntimeException When proc_open is not installed
  40. */
  41. public function __construct($commandline, $cwd = null, array $env = array(), $stdin = null, $timeout = 60, array $options = array())
  42. {
  43. if (!function_exists('proc_open')) {
  44. throw new \RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
  45. }
  46. $this->commandline = $commandline;
  47. $this->cwd = null === $cwd ? getcwd() : $cwd;
  48. $this->env = array();
  49. foreach ($env as $key => $value) {
  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. * Runs the process.
  58. *
  59. * The callback receives the type of output (out or err) and
  60. * some bytes from the output in real-time. It allows to have feedback
  61. * from the independent process during execution.
  62. *
  63. * If you don't provide a callback, the STDOUT and STDERR are available only after
  64. * the process is finished via the getOutput() and getErrorOutput() methods.
  65. *
  66. * @param Closure|string|array $callback A PHP callback to run whenever there is some
  67. * output available on STDOUT or STDERR
  68. *
  69. * @return integer The exit status code
  70. *
  71. * @throws \RuntimeException When process can't be launch or is stopped
  72. */
  73. public function run($callback = null)
  74. {
  75. if (null === $callback) {
  76. $this->stdout = '';
  77. $this->stderr = '';
  78. $that = $this;
  79. $callback = function ($type, $line) use ($that)
  80. {
  81. if ('out' == $type) {
  82. $that->addOutput($line);
  83. } else {
  84. $that->addErrorOutput($line);
  85. }
  86. };
  87. }
  88. $descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
  89. $process = proc_open($this->commandline, $descriptors, $pipes, $this->cwd, $this->env, $this->options);
  90. stream_set_blocking($pipes[1], false);
  91. stream_set_blocking($pipes[2], false);
  92. if (!is_resource($process)) {
  93. throw new \RuntimeException('Unable to launch a new process.');
  94. }
  95. if (null !== $this->stdin) {
  96. fwrite($pipes[0], (binary) $this->stdin);
  97. }
  98. fclose($pipes[0]);
  99. while (true) {
  100. $r = $pipes;
  101. $w = null;
  102. $e = null;
  103. $n = @stream_select($r, $w, $e, $this->timeout);
  104. if (false === $n) {
  105. break;
  106. } elseif ($n === 0) {
  107. proc_terminate($process);
  108. throw new \RuntimeException('The process timed out.');
  109. } elseif ($n > 0) {
  110. $called = false;
  111. while (true) {
  112. $c = false;
  113. if ($line = (binary) fgets($pipes[1], 1024)) {
  114. $called = $c = true;
  115. call_user_func($callback, 'out', $line);
  116. }
  117. if ($line = fgets($pipes[2], 1024)) {
  118. $called = $c = true;
  119. call_user_func($callback, 'err', $line);
  120. }
  121. if (!$c) {
  122. break;
  123. }
  124. }
  125. if (!$called) {
  126. break;
  127. }
  128. }
  129. }
  130. $this->status = proc_get_status($process);
  131. proc_close($process);
  132. if ($this->status['signaled']) {
  133. throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->status['stopsig']));
  134. }
  135. return $this->exitcode = $this->status['exitcode'];
  136. }
  137. /**
  138. * Returns the output of the process (STDOUT).
  139. *
  140. * This only returns the output if you have not supplied a callback
  141. * to the run() method.
  142. *
  143. * @return string The process output
  144. */
  145. public function getOutput()
  146. {
  147. return $this->stdout;
  148. }
  149. /**
  150. * Returns the error output of the process (STDERR).
  151. *
  152. * This only returns the error output if you have not supplied a callback
  153. * to the run() method.
  154. *
  155. * @return string The process error output
  156. */
  157. public function getErrorOutput()
  158. {
  159. return $this->stderr;
  160. }
  161. /**
  162. * Returns the exit code returned by the process.
  163. *
  164. * @return integer The exit status code
  165. */
  166. public function getExitCode()
  167. {
  168. return $this->exitcode;
  169. }
  170. /**
  171. * Checks if the process ended successfully.
  172. *
  173. * @return Boolean true if the process ended successfully, false otherwise
  174. */
  175. public function isSuccessful()
  176. {
  177. return 0 == $this->exitcode;
  178. }
  179. /**
  180. * Returns true if the child process has been terminated by an uncaught signal.
  181. *
  182. * It always returns false on Windows.
  183. *
  184. * @return Boolean
  185. */
  186. public function hasBeenSignaled()
  187. {
  188. return $this->status['signaled'];
  189. }
  190. /**
  191. * Returns the number of the signal that caused the child process to terminate its execution.
  192. *
  193. * It is only meaningful if hasBeenSignaled() returns true.
  194. *
  195. * @return integer
  196. */
  197. public function getTermSignal()
  198. {
  199. return $this->status['termsig'];
  200. }
  201. /**
  202. * Returns true if the child process has been stopped by a signal.
  203. *
  204. * It always returns false on Windows.
  205. *
  206. * @return Boolean
  207. */
  208. public function hasBeenStopped()
  209. {
  210. return $this->status['stopped'];
  211. }
  212. /**
  213. * Returns the number of the signal that caused the child process to stop its execution
  214. *
  215. * It is only meaningful if hasBeenStopped() returns true.
  216. *
  217. * @return integer
  218. */
  219. public function getStopSignal()
  220. {
  221. return $this->status['stopsig'];
  222. }
  223. public function addOutput($line)
  224. {
  225. $this->stdout .= $line;
  226. }
  227. public function addErrorOutput($line)
  228. {
  229. $this->stderr .= $line;
  230. }
  231. }