UnixPipes.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /**
  32. * @return array
  33. */
  34. public function __sleep()
  35. {
  36. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  37. }
  38. public function __wakeup()
  39. {
  40. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  41. }
  42. public function __destruct()
  43. {
  44. $this->close();
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getDescriptors(): array
  50. {
  51. if (!$this->haveReadSupport) {
  52. $nullstream = fopen('/dev/null', 'c');
  53. return [
  54. ['pipe', 'r'],
  55. $nullstream,
  56. $nullstream,
  57. ];
  58. }
  59. if ($this->ttyMode) {
  60. return [
  61. ['file', '/dev/tty', 'r'],
  62. ['file', '/dev/tty', 'w'],
  63. ['file', '/dev/tty', 'w'],
  64. ];
  65. }
  66. if ($this->ptyMode && Process::isPtySupported()) {
  67. return [
  68. ['pty'],
  69. ['pty'],
  70. ['pty'],
  71. ];
  72. }
  73. return [
  74. ['pipe', 'r'],
  75. ['pipe', 'w'], // stdout
  76. ['pipe', 'w'], // stderr
  77. ];
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getFiles(): array
  83. {
  84. return [];
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function readAndWrite(bool $blocking, bool $close = false): array
  90. {
  91. $this->unblock();
  92. $w = $this->write();
  93. $read = $e = [];
  94. $r = $this->pipes;
  95. unset($r[0]);
  96. // let's have a look if something changed in streams
  97. set_error_handler([$this, 'handleError']);
  98. if (($r || $w) && false === stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
  99. restore_error_handler();
  100. // if a system call has been interrupted, forget about it, let's try again
  101. // otherwise, an error occurred, let's reset pipes
  102. if (!$this->hasSystemCallBeenInterrupted()) {
  103. $this->pipes = [];
  104. }
  105. return $read;
  106. }
  107. restore_error_handler();
  108. foreach ($r as $pipe) {
  109. // prior PHP 5.4 the array passed to stream_select is modified and
  110. // lose key association, we have to find back the key
  111. $read[$type = array_search($pipe, $this->pipes, true)] = '';
  112. do {
  113. $data = @fread($pipe, self::CHUNK_SIZE);
  114. $read[$type] .= $data;
  115. } while (isset($data[0]) && ($close || isset($data[self::CHUNK_SIZE - 1])));
  116. if (!isset($read[$type][0])) {
  117. unset($read[$type]);
  118. }
  119. if ($close && feof($pipe)) {
  120. fclose($pipe);
  121. unset($this->pipes[$type]);
  122. }
  123. }
  124. return $read;
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function haveReadSupport(): bool
  130. {
  131. return $this->haveReadSupport;
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function areOpen(): bool
  137. {
  138. return (bool) $this->pipes;
  139. }
  140. }