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

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

https://github.com/agustinc/SemdropsMobile
PHP | 372 lines | 206 code | 46 blank | 120 comment | 27 complexity | db6c18d5794118a2d6ede377308e8e83 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, $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. $time = 0;
  157. while (1 == $this->status['running'] && $time < 1000000) {
  158. $time += 1000;
  159. usleep(1000);
  160. $this->status = proc_get_status($process);
  161. }
  162. proc_close($process);
  163. if ($this->status['signaled']) {
  164. throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->status['stopsig']));
  165. }
  166. return $this->exitcode = $this->status['exitcode'];
  167. }
  168. /**
  169. * Returns the output of the process (STDOUT).
  170. *
  171. * This only returns the output if you have not supplied a callback
  172. * to the run() method.
  173. *
  174. * @return string The process output
  175. *
  176. * @api
  177. */
  178. public function getOutput()
  179. {
  180. return $this->stdout;
  181. }
  182. /**
  183. * Returns the error output of the process (STDERR).
  184. *
  185. * This only returns the error output if you have not supplied a callback
  186. * to the run() method.
  187. *
  188. * @return string The process error output
  189. *
  190. * @api
  191. */
  192. public function getErrorOutput()
  193. {
  194. return $this->stderr;
  195. }
  196. /**
  197. * Returns the exit code returned by the process.
  198. *
  199. * @return integer The exit status code
  200. *
  201. * @api
  202. */
  203. public function getExitCode()
  204. {
  205. return $this->exitcode;
  206. }
  207. /**
  208. * Checks if the process ended successfully.
  209. *
  210. * @return Boolean true if the process ended successfully, false otherwise
  211. *
  212. * @api
  213. */
  214. public function isSuccessful()
  215. {
  216. return 0 == $this->exitcode;
  217. }
  218. /**
  219. * Returns true if the child process has been terminated by an uncaught signal.
  220. *
  221. * It always returns false on Windows.
  222. *
  223. * @return Boolean
  224. *
  225. * @api
  226. */
  227. public function hasBeenSignaled()
  228. {
  229. return $this->status['signaled'];
  230. }
  231. /**
  232. * Returns the number of the signal that caused the child process to terminate its execution.
  233. *
  234. * It is only meaningful if hasBeenSignaled() returns true.
  235. *
  236. * @return integer
  237. *
  238. * @api
  239. */
  240. public function getTermSignal()
  241. {
  242. return $this->status['termsig'];
  243. }
  244. /**
  245. * Returns true if the child process has been stopped by a signal.
  246. *
  247. * It always returns false on Windows.
  248. *
  249. * @return Boolean
  250. *
  251. * @api
  252. */
  253. public function hasBeenStopped()
  254. {
  255. return $this->status['stopped'];
  256. }
  257. /**
  258. * Returns the number of the signal that caused the child process to stop its execution
  259. *
  260. * It is only meaningful if hasBeenStopped() returns true.
  261. *
  262. * @return integer
  263. *
  264. * @api
  265. */
  266. public function getStopSignal()
  267. {
  268. return $this->status['stopsig'];
  269. }
  270. public function addOutput($line)
  271. {
  272. $this->stdout .= $line;
  273. }
  274. public function addErrorOutput($line)
  275. {
  276. $this->stderr .= $line;
  277. }
  278. public function getCommandLine()
  279. {
  280. return $this->commandline;
  281. }
  282. public function setCommandLine($commandline)
  283. {
  284. $this->commandline = $commandline;
  285. }
  286. public function getTimeout()
  287. {
  288. return $this->timeout;
  289. }
  290. public function setTimeout($timeout)
  291. {
  292. $this->timeout = $timeout;
  293. }
  294. public function getWorkingDirectory()
  295. {
  296. return $this->cwd;
  297. }
  298. public function setWorkingDirectory($cwd)
  299. {
  300. $this->cwd = $cwd;
  301. }
  302. public function getEnv()
  303. {
  304. return $this->env;
  305. }
  306. public function setEnv(array $env)
  307. {
  308. $this->env = $env;
  309. }
  310. public function getStdin()
  311. {
  312. return $this->stdin;
  313. }
  314. public function setStdin($stdin)
  315. {
  316. $this->stdin = $stdin;
  317. }
  318. public function getOptions()
  319. {
  320. return $this->options;
  321. }
  322. public function setOptions(array $options)
  323. {
  324. $this->options = $options;
  325. }
  326. }