UnixPipes.php 3.6 KB

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