WindowsPipes.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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(), 'out_sf_proc'),
  46. Process::STDERR => tempnam(sys_get_temp_dir(), 'err_sf_proc'),
  47. );
  48. foreach ($this->files as $offset => $file) {
  49. if (false === $file || false === $this->fileHandles[$offset] = @fopen($file, 'rb')) {
  50. throw new RuntimeException('A temporary file could not be opened to write the process output to, verify that your TEMP environment variable is writable');
  51. }
  52. }
  53. }
  54. parent::__construct($input);
  55. }
  56. public function __destruct()
  57. {
  58. $this->close();
  59. $this->removeFiles();
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getDescriptors()
  65. {
  66. if ($this->disableOutput) {
  67. $nullstream = fopen('NUL', 'c');
  68. return array(
  69. array('pipe', 'r'),
  70. $nullstream,
  71. $nullstream,
  72. );
  73. }
  74. // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/bug.php?id=51800)
  75. // We're not using file handles as it can produce corrupted output https://bugs.php.net/bug.php?id=65650
  76. // So we redirect output within the commandline and pass the nul device to the process
  77. return array(
  78. array('pipe', 'r'),
  79. array('file', 'NUL', 'w'),
  80. array('file', 'NUL', 'w'),
  81. );
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getFiles()
  87. {
  88. return $this->files;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function readAndWrite($blocking, $close = false)
  94. {
  95. $this->unblock();
  96. $w = $this->write();
  97. $read = $r = $e = array();
  98. if ($blocking) {
  99. if ($w) {
  100. @stream_select($r, $w, $e, 0, Process::TIMEOUT_PRECISION * 1E6);
  101. } elseif ($this->fileHandles) {
  102. usleep(Process::TIMEOUT_PRECISION * 1E6);
  103. }
  104. }
  105. foreach ($this->fileHandles as $type => $fileHandle) {
  106. $data = stream_get_contents($fileHandle, -1, $this->readBytes[$type]);
  107. if (isset($data[0])) {
  108. $this->readBytes[$type] += strlen($data);
  109. $read[$type] = $data;
  110. }
  111. if ($close) {
  112. fclose($fileHandle);
  113. unset($this->fileHandles[$type]);
  114. }
  115. }
  116. return $read;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function areOpen()
  122. {
  123. return $this->pipes && $this->fileHandles;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function close()
  129. {
  130. parent::close();
  131. foreach ($this->fileHandles as $handle) {
  132. fclose($handle);
  133. }
  134. $this->fileHandles = array();
  135. }
  136. /**
  137. * Creates a new WindowsPipes instance.
  138. *
  139. * @param Process $process The process
  140. * @param $input
  141. *
  142. * @return WindowsPipes
  143. */
  144. public static function create(Process $process, $input)
  145. {
  146. return new static($process->isOutputDisabled(), $input);
  147. }
  148. /**
  149. * Removes temporary files.
  150. */
  151. private function removeFiles()
  152. {
  153. foreach ($this->files as $filename) {
  154. if (file_exists($filename)) {
  155. @unlink($filename);
  156. }
  157. }
  158. $this->files = array();
  159. }
  160. }