PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/kriswallsmith/assetic/src/Assetic/Util/Process.php

https://bitbucket.org/hanutimes/hanutimes
PHP | 379 lines | 200 code | 45 blank | 134 comment | 25 complexity | d01fca4ba87a103c8a990e35018676e6 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * Copyright (c) 2004-2011 Fabien Potencier
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is furnished
  12. * to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. namespace Assetic\Util;
  26. /**
  27. * Process is a thin wrapper around proc_* functions to ease
  28. * start independent PHP processes.
  29. *
  30. * @author Fabien Potencier <fabien@symfony.com>
  31. *
  32. * @api
  33. */
  34. class Process
  35. {
  36. private $commandline;
  37. private $cwd;
  38. private $env;
  39. private $stdin;
  40. private $timeout;
  41. private $options;
  42. private $exitcode;
  43. private $status;
  44. private $stdout;
  45. private $stderr;
  46. /**
  47. * Constructor.
  48. *
  49. * @param string $commandline The command line to run
  50. * @param string $cwd The working directory
  51. * @param array $env The environment variables
  52. * @param string $stdin The STDIN content
  53. * @param integer $timeout The timeout in seconds
  54. * @param array $options An array of options for proc_open
  55. *
  56. * @throws \RuntimeException When proc_open is not installed
  57. *
  58. * @api
  59. */
  60. public function __construct($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())
  61. {
  62. if (!function_exists('proc_open')) {
  63. throw new \RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
  64. }
  65. $this->commandline = $commandline;
  66. $this->cwd = null === $cwd ? getcwd() : $cwd;
  67. if (null !== $env) {
  68. $this->env = array();
  69. foreach ($env as $key => $value) {
  70. $this->env[(binary) $key] = (binary) $value;
  71. }
  72. } else {
  73. $this->env = null;
  74. }
  75. $this->stdin = $stdin;
  76. $this->timeout = $timeout;
  77. $this->options = array_merge(array('suppress_errors' => true, 'binary_pipes' => true, 'bypass_shell' => false), $options);
  78. }
  79. /**
  80. * Runs the process.
  81. *
  82. * The callback receives the type of output (out or err) and
  83. * some bytes from the output in real-time. It allows to have feedback
  84. * from the independent process during execution.
  85. *
  86. * The STDOUT and STDERR are also available after the process is finished
  87. * via the getOutput() and getErrorOutput() methods.
  88. *
  89. * @param Closure|string|array $callback A PHP callback to run whenever there is some
  90. * output available on STDOUT or STDERR
  91. *
  92. * @return integer The exit status code
  93. *
  94. * @throws \RuntimeException When process can't be launch or is stopped
  95. *
  96. * @api
  97. */
  98. public function run($callback = null)
  99. {
  100. $this->stdout = '';
  101. $this->stderr = '';
  102. $that = $this;
  103. $callback = function ($type, $data) use ($that, $callback) {
  104. if ('out' == $type) {
  105. $that->addOutput($data);
  106. } else {
  107. $that->addErrorOutput($data);
  108. }
  109. if (null !== $callback) {
  110. call_user_func($callback, $type, $data);
  111. }
  112. };
  113. $descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
  114. $process = proc_open($this->commandline, $descriptors, $pipes, $this->cwd, $this->env, $this->options);
  115. if (!is_resource($process)) {
  116. throw new \RuntimeException('Unable to launch a new process.');
  117. }
  118. foreach ($pipes as $pipe) {
  119. stream_set_blocking($pipe, false);
  120. }
  121. if (null === $this->stdin) {
  122. fclose($pipes[0]);
  123. $writePipes = null;
  124. } else {
  125. $writePipes = array($pipes[0]);
  126. $stdinLen = strlen($this->stdin);
  127. $stdinOffset = 0;
  128. }
  129. unset($pipes[0]);
  130. while ($pipes || $writePipes) {
  131. $r = $pipes;
  132. $w = $writePipes;
  133. $e = null;
  134. $n = @stream_select($r, $w, $e, $this->timeout);
  135. if (false === $n) {
  136. break;
  137. } elseif ($n === 0) {
  138. proc_terminate($process);
  139. throw new \RuntimeException('The process timed out.');
  140. }
  141. if ($w) {
  142. $written = fwrite($writePipes[0], (binary) substr($this->stdin, $stdinOffset), 8192);
  143. if (false !== $written) {
  144. $stdinOffset += $written;
  145. }
  146. if ($stdinOffset >= $stdinLen) {
  147. fclose($writePipes[0]);
  148. $writePipes = null;
  149. }
  150. }
  151. foreach ($r as $pipe) {
  152. $type = array_search($pipe, $pipes);
  153. $data = fread($pipe, 8192);
  154. if (strlen($data) > 0) {
  155. call_user_func($callback, $type == 1 ? 'out' : 'err', $data);
  156. }
  157. if (false === $data || feof($pipe)) {
  158. fclose($pipe);
  159. unset($pipes[$type]);
  160. }
  161. }
  162. }
  163. $this->status = proc_get_status($process);
  164. $time = 0;
  165. while (1 == $this->status['running'] && $time < 1000000) {
  166. $time += 1000;
  167. usleep(1000);
  168. $this->status = proc_get_status($process);
  169. }
  170. $exitcode = proc_close($process);
  171. if ($this->status['signaled']) {
  172. throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->status['stopsig']));
  173. }
  174. return $this->exitcode = $this->status['running'] ? $exitcode : $this->status['exitcode'];
  175. }
  176. /**
  177. * Returns the output of the process (STDOUT).
  178. *
  179. * This only returns the output if you have not supplied a callback
  180. * to the run() method.
  181. *
  182. * @return string The process output
  183. *
  184. * @api
  185. */
  186. public function getOutput()
  187. {
  188. return $this->stdout;
  189. }
  190. /**
  191. * Returns the error output of the process (STDERR).
  192. *
  193. * This only returns the error output if you have not supplied a callback
  194. * to the run() method.
  195. *
  196. * @return string The process error output
  197. *
  198. * @api
  199. */
  200. public function getErrorOutput()
  201. {
  202. return $this->stderr;
  203. }
  204. /**
  205. * Returns the exit code returned by the process.
  206. *
  207. * @return integer The exit status code
  208. *
  209. * @api
  210. */
  211. public function getExitCode()
  212. {
  213. return $this->exitcode;
  214. }
  215. /**
  216. * Checks if the process ended successfully.
  217. *
  218. * @return Boolean true if the process ended successfully, false otherwise
  219. *
  220. * @api
  221. */
  222. public function isSuccessful()
  223. {
  224. return 0 == $this->exitcode;
  225. }
  226. /**
  227. * Returns true if the child process has been terminated by an uncaught signal.
  228. *
  229. * It always returns false on Windows.
  230. *
  231. * @return Boolean
  232. *
  233. * @api
  234. */
  235. public function hasBeenSignaled()
  236. {
  237. return $this->status['signaled'];
  238. }
  239. /**
  240. * Returns the number of the signal that caused the child process to terminate its execution.
  241. *
  242. * It is only meaningful if hasBeenSignaled() returns true.
  243. *
  244. * @return integer
  245. *
  246. * @api
  247. */
  248. public function getTermSignal()
  249. {
  250. return $this->status['termsig'];
  251. }
  252. /**
  253. * Returns true if the child process has been stopped by a signal.
  254. *
  255. * It always returns false on Windows.
  256. *
  257. * @return Boolean
  258. *
  259. * @api
  260. */
  261. public function hasBeenStopped()
  262. {
  263. return $this->status['stopped'];
  264. }
  265. /**
  266. * Returns the number of the signal that caused the child process to stop its execution
  267. *
  268. * It is only meaningful if hasBeenStopped() returns true.
  269. *
  270. * @return integer
  271. *
  272. * @api
  273. */
  274. public function getStopSignal()
  275. {
  276. return $this->status['stopsig'];
  277. }
  278. public function addOutput($line)
  279. {
  280. $this->stdout .= $line;
  281. }
  282. public function addErrorOutput($line)
  283. {
  284. $this->stderr .= $line;
  285. }
  286. public function getCommandLine()
  287. {
  288. return $this->commandline;
  289. }
  290. public function setCommandLine($commandline)
  291. {
  292. $this->commandline = $commandline;
  293. }
  294. public function getTimeout()
  295. {
  296. return $this->timeout;
  297. }
  298. public function setTimeout($timeout)
  299. {
  300. $this->timeout = $timeout;
  301. }
  302. public function getWorkingDirectory()
  303. {
  304. return $this->cwd;
  305. }
  306. public function setWorkingDirectory($cwd)
  307. {
  308. $this->cwd = $cwd;
  309. }
  310. public function getEnv()
  311. {
  312. return $this->env;
  313. }
  314. public function setEnv(array $env)
  315. {
  316. $this->env = $env;
  317. }
  318. public function getStdin()
  319. {
  320. return $this->stdin;
  321. }
  322. public function setStdin($stdin)
  323. {
  324. $this->stdin = $stdin;
  325. }
  326. public function getOptions()
  327. {
  328. return $this->options;
  329. }
  330. public function setOptions(array $options)
  331. {
  332. $this->options = $options;
  333. }
  334. }