WindowsPipes.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. private $files = array();
  26. private $fileHandles = array();
  27. private $readBytes = array(
  28. Process::STDOUT => 0,
  29. Process::STDERR => 0,
  30. );
  31. private $haveReadSupport;
  32. public function __construct($input, $haveReadSupport)
  33. {
  34. $this->haveReadSupport = (bool) $haveReadSupport;
  35. if ($this->haveReadSupport) {
  36. // Fix for PHP bug #51800: reading from STDOUT pipe hangs forever on Windows if the output is too big.
  37. // Workaround for this problem is to use temporary files instead of pipes on Windows platform.
  38. //
  39. // @see https://bugs.php.net/bug.php?id=51800
  40. $pipes = array(
  41. Process::STDOUT => Process::OUT,
  42. Process::STDERR => Process::ERR,
  43. );
  44. $tmpCheck = false;
  45. $tmpDir = sys_get_temp_dir();
  46. $lastError = 'unknown reason';
  47. set_error_handler(function ($type, $msg) use (&$lastError) { $lastError = $msg; });
  48. for ($i = 0;; ++$i) {
  49. foreach ($pipes as $pipe => $name) {
  50. $file = sprintf('%s\\sf_proc_%02X.%s', $tmpDir, $i, $name);
  51. if (file_exists($file) && !unlink($file)) {
  52. continue 2;
  53. }
  54. $h = fopen($file, 'xb');
  55. if (!$h) {
  56. $error = $lastError;
  57. if ($tmpCheck || $tmpCheck = unlink(tempnam(false, 'sf_check_'))) {
  58. continue;
  59. }
  60. restore_error_handler();
  61. throw new RuntimeException(sprintf('A temporary file could not be opened to write the process output: %s', $error));
  62. }
  63. if (!$h || !$this->fileHandles[$pipe] = fopen($file, 'rb')) {
  64. continue 2;
  65. }
  66. if (isset($this->files[$pipe])) {
  67. unlink($this->files[$pipe]);
  68. }
  69. $this->files[$pipe] = $file;
  70. }
  71. break;
  72. }
  73. restore_error_handler();
  74. }
  75. parent::__construct($input);
  76. }
  77. public function __destruct()
  78. {
  79. $this->close();
  80. $this->removeFiles();
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getDescriptors()
  86. {
  87. if (!$this->haveReadSupport) {
  88. $nullstream = fopen('NUL', 'c');
  89. return array(
  90. array('pipe', 'r'),
  91. $nullstream,
  92. $nullstream,
  93. );
  94. }
  95. // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/bug.php?id=51800)
  96. // We're not using file handles as it can produce corrupted output https://bugs.php.net/bug.php?id=65650
  97. // So we redirect output within the commandline and pass the nul device to the process
  98. return array(
  99. array('pipe', 'r'),
  100. array('file', 'NUL', 'w'),
  101. array('file', 'NUL', 'w'),
  102. );
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getFiles()
  108. {
  109. return $this->files;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function readAndWrite($blocking, $close = false)
  115. {
  116. $this->unblock();
  117. $w = $this->write();
  118. $read = $r = $e = array();
  119. if ($blocking) {
  120. if ($w) {
  121. @stream_select($r, $w, $e, 0, Process::TIMEOUT_PRECISION * 1E6);
  122. } elseif ($this->fileHandles) {
  123. usleep(Process::TIMEOUT_PRECISION * 1E6);
  124. }
  125. }
  126. foreach ($this->fileHandles as $type => $fileHandle) {
  127. $data = stream_get_contents($fileHandle, -1, $this->readBytes[$type]);
  128. if (isset($data[0])) {
  129. $this->readBytes[$type] += strlen($data);
  130. $read[$type] = $data;
  131. }
  132. if ($close) {
  133. fclose($fileHandle);
  134. unset($this->fileHandles[$type]);
  135. }
  136. }
  137. return $read;
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function haveReadSupport()
  143. {
  144. return $this->haveReadSupport;
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function areOpen()
  150. {
  151. return $this->pipes && $this->fileHandles;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function close()
  157. {
  158. parent::close();
  159. foreach ($this->fileHandles as $handle) {
  160. fclose($handle);
  161. }
  162. $this->fileHandles = array();
  163. }
  164. /**
  165. * Removes temporary files.
  166. */
  167. private function removeFiles()
  168. {
  169. foreach ($this->files as $filename) {
  170. if (file_exists($filename)) {
  171. @unlink($filename);
  172. }
  173. }
  174. $this->files = array();
  175. }
  176. }