UnixPipes.php 3.8 KB

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