PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

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