WindowsPipes.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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\Pipes;
  11. use Symfony\Component\Process\Process;
  12. use Symfony\Component\Process\Exception\RuntimeException;
  13. /**
  14. * WindowsPipes implementation uses temporary files as handles.
  15. *
  16. * @see https://bugs.php.net/bug.php?id=51800
  17. * @see https://bugs.php.net/bug.php?id=65650
  18. *
  19. * @author Romain Neutron <imprec@gmail.com>
  20. *
  21. * @internal
  22. */
  23. class WindowsPipes extends AbstractPipes
  24. {
  25. /** @var array */
  26. private $files = array();
  27. /** @var array */
  28. private $fileHandles = array();
  29. /** @var array */
  30. private $readBytes = array(
  31. Process::STDOUT => 0,
  32. Process::STDERR => 0,
  33. );
  34. /** @var bool */
  35. private $disableOutput;
  36. public function __construct($disableOutput, $input)
  37. {
  38. $this->disableOutput = (bool) $disableOutput;
  39. if (!$this->disableOutput) {
  40. // Fix for PHP bug #51800: reading from STDOUT pipe hangs forever on Windows if the output is too big.
  41. // Workaround for this problem is to use temporary files instead of pipes on Windows platform.
  42. //
  43. // @see https://bugs.php.net/bug.php?id=51800
  44. $this->files = array(
  45. Process::STDOUT => tempnam(sys_get_temp_dir(), 'sf_proc_stdout'),
  46. Process::STDERR => tempnam(sys_get_temp_dir(), 'sf_proc_stderr'),
  47. );
  48. foreach ($this->files as $offset => $file) {
  49. $this->fileHandles[$offset] = fopen($this->files[$offset], 'rb');
  50. if (false === $this->fileHandles[$offset]) {
  51. throw new RuntimeException('A temporary file could not be opened to write the process output to, verify that your TEMP environment variable is writable');
  52. }
  53. }
  54. }
  55. if (is_resource($input)) {
  56. $this->input = $input;
  57. } else {
  58. $this->inputBuffer = $input;
  59. }
  60. }
  61. public function __destruct()
  62. {
  63. $this->close();
  64. $this->removeFiles();
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getDescriptors()
  70. {
  71. if ($this->disableOutput) {
  72. $nullstream = fopen('NUL', 'c');
  73. return array(
  74. array('pipe', 'r'),
  75. $nullstream,
  76. $nullstream,
  77. );
  78. }
  79. // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/bug.php?id=51800)
  80. // We're not using file handles as it can produce corrupted output https://bugs.php.net/bug.php?id=65650
  81. // So we redirect output within the commandline and pass the nul device to the process
  82. return array(
  83. array('pipe', 'r'),
  84. array('file', 'NUL', 'w'),
  85. array('file', 'NUL', 'w'),
  86. );
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getFiles()
  92. {
  93. return $this->files;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function readAndWrite($blocking, $close = false)
  99. {
  100. $this->write($blocking, $close);
  101. $read = array();
  102. $fh = $this->fileHandles;
  103. foreach ($fh as $type => $fileHandle) {
  104. if (0 !== fseek($fileHandle, $this->readBytes[$type])) {
  105. continue;
  106. }
  107. $data = '';
  108. $dataread = null;
  109. while (!feof($fileHandle)) {
  110. if (false !== $dataread = fread($fileHandle, self::CHUNK_SIZE)) {
  111. $data .= $dataread;
  112. }
  113. }
  114. if (0 < $length = strlen($data)) {
  115. $this->readBytes[$type] += $length;
  116. $read[$type] = $data;
  117. }
  118. if (false === $dataread || (true === $close && feof($fileHandle) && '' === $data)) {
  119. fclose($this->fileHandles[$type]);
  120. unset($this->fileHandles[$type]);
  121. }
  122. }
  123. return $read;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function areOpen()
  129. {
  130. return (bool) $this->pipes && (bool) $this->fileHandles;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function close()
  136. {
  137. parent::close();
  138. foreach ($this->fileHandles as $handle) {
  139. fclose($handle);
  140. }
  141. $this->fileHandles = array();
  142. }
  143. /**
  144. * Creates a new WindowsPipes instance.
  145. *
  146. * @param Process $process The process
  147. * @param $input
  148. *
  149. * @return WindowsPipes
  150. */
  151. public static function create(Process $process, $input)
  152. {
  153. return new static($process->isOutputDisabled(), $input);
  154. }
  155. /**
  156. * Removes temporary files
  157. */
  158. private function removeFiles()
  159. {
  160. foreach ($this->files as $filename) {
  161. if (file_exists($filename)) {
  162. @unlink($filename);
  163. }
  164. }
  165. $this->files = array();
  166. }
  167. /**
  168. * Writes input to stdin
  169. *
  170. * @param bool $blocking
  171. * @param bool $close
  172. */
  173. private function write($blocking, $close)
  174. {
  175. if (empty($this->pipes)) {
  176. return;
  177. }
  178. $this->unblock();
  179. $r = null !== $this->input ? array('input' => $this->input) : null;
  180. $w = isset($this->pipes[0]) ? array($this->pipes[0]) : null;
  181. $e = null;
  182. // let's have a look if something changed in streams
  183. if (false === $n = @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
  184. // if a system call has been interrupted, forget about it, let's try again
  185. // otherwise, an error occurred, let's reset pipes
  186. if (!$this->hasSystemCallBeenInterrupted()) {
  187. $this->pipes = array();
  188. }
  189. return;
  190. }
  191. // nothing has changed
  192. if (0 === $n) {
  193. return;
  194. }
  195. if (null !== $w && 0 < count($r)) {
  196. $data = '';
  197. while ($dataread = fread($r['input'], self::CHUNK_SIZE)) {
  198. $data .= $dataread;
  199. }
  200. $this->inputBuffer .= $data;
  201. if (false === $data || (true === $close && feof($r['input']) && '' === $data)) {
  202. // no more data to read on input resource
  203. // use an empty buffer in the next reads
  204. $this->input = null;
  205. }
  206. }
  207. if (null !== $w && 0 < count($w)) {
  208. while (strlen($this->inputBuffer)) {
  209. $written = fwrite($w[0], $this->inputBuffer, 2 << 18);
  210. if ($written > 0) {
  211. $this->inputBuffer = (string) substr($this->inputBuffer, $written);
  212. } else {
  213. break;
  214. }
  215. }
  216. }
  217. // no input to read on resource, buffer is empty and stdin still open
  218. if ('' === $this->inputBuffer && null === $this->input && isset($this->pipes[0])) {
  219. fclose($this->pipes[0]);
  220. unset($this->pipes[0]);
  221. }
  222. }
  223. }