WindowsPipes.php 5.5 KB

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