UnixPipes.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /**
  13. * UnixPipes implementation uses unix pipes as handles.
  14. *
  15. * @author Romain Neutron <imprec@gmail.com>
  16. *
  17. * @internal
  18. */
  19. class UnixPipes extends AbstractPipes
  20. {
  21. private $ttyMode;
  22. private $ptyMode;
  23. private $haveReadSupport;
  24. public function __construct(?bool $ttyMode, bool $ptyMode, $input, bool $haveReadSupport)
  25. {
  26. $this->ttyMode = $ttyMode;
  27. $this->ptyMode = $ptyMode;
  28. $this->haveReadSupport = $haveReadSupport;
  29. parent::__construct($input);
  30. }
  31. public function __destruct()
  32. {
  33. $this->close();
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getDescriptors()
  39. {
  40. if (!$this->haveReadSupport) {
  41. $nullstream = fopen('/dev/null', 'c');
  42. return [
  43. ['pipe', 'r'],
  44. $nullstream,
  45. $nullstream,
  46. ];
  47. }
  48. if ($this->ttyMode) {
  49. return [
  50. ['file', '/dev/tty', 'r'],
  51. ['file', '/dev/tty', 'w'],
  52. ['file', '/dev/tty', 'w'],
  53. ];
  54. }
  55. if ($this->ptyMode && Process::isPtySupported()) {
  56. return [
  57. ['pty'],
  58. ['pty'],
  59. ['pty'],
  60. ];
  61. }
  62. return [
  63. ['pipe', 'r'],
  64. ['pipe', 'w'], // stdout
  65. ['pipe', 'w'], // stderr
  66. ];
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getFiles()
  72. {
  73. return [];
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function readAndWrite($blocking, $close = false)
  79. {
  80. $this->unblock();
  81. $w = $this->write();
  82. $read = $e = [];
  83. $r = $this->pipes;
  84. unset($r[0]);
  85. // let's have a look if something changed in streams
  86. set_error_handler([$this, 'handleError']);
  87. if (($r || $w) && false === stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
  88. restore_error_handler();
  89. // if a system call has been interrupted, forget about it, let's try again
  90. // otherwise, an error occurred, let's reset pipes
  91. if (!$this->hasSystemCallBeenInterrupted()) {
  92. $this->pipes = [];
  93. }
  94. return $read;
  95. }
  96. restore_error_handler();
  97. foreach ($r as $pipe) {
  98. // prior PHP 5.4 the array passed to stream_select is modified and
  99. // lose key association, we have to find back the key
  100. $read[$type = array_search($pipe, $this->pipes, true)] = '';
  101. do {
  102. $data = fread($pipe, self::CHUNK_SIZE);
  103. $read[$type] .= $data;
  104. } while (isset($data[0]) && ($close || isset($data[self::CHUNK_SIZE - 1])));
  105. if (!isset($read[$type][0])) {
  106. unset($read[$type]);
  107. }
  108. if ($close && feof($pipe)) {
  109. fclose($pipe);
  110. unset($this->pipes[$type]);
  111. }
  112. }
  113. return $read;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function haveReadSupport()
  119. {
  120. return $this->haveReadSupport;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function areOpen()
  126. {
  127. return (bool) $this->pipes;
  128. }
  129. }