AbstractPipes.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\Exception\InvalidArgumentException;
  12. /**
  13. * @author Romain Neutron <imprec@gmail.com>
  14. *
  15. * @internal
  16. */
  17. abstract class AbstractPipes implements PipesInterface
  18. {
  19. public array $pipes = [];
  20. private string $inputBuffer = '';
  21. /** @var resource|string|\Iterator */
  22. private $input;
  23. private bool $blocked = true;
  24. private ?string $lastError = null;
  25. /**
  26. * @param resource|string|\Iterator $input
  27. */
  28. public function __construct($input)
  29. {
  30. if (\is_resource($input) || $input instanceof \Iterator) {
  31. $this->input = $input;
  32. } else {
  33. $this->inputBuffer = (string) $input;
  34. }
  35. }
  36. public function close(): void
  37. {
  38. foreach ($this->pipes as $pipe) {
  39. if (\is_resource($pipe)) {
  40. fclose($pipe);
  41. }
  42. }
  43. $this->pipes = [];
  44. }
  45. /**
  46. * Returns true if a system call has been interrupted.
  47. */
  48. protected function hasSystemCallBeenInterrupted(): bool
  49. {
  50. $lastError = $this->lastError;
  51. $this->lastError = null;
  52. // stream_select returns false when the `select` system call is interrupted by an incoming signal
  53. return null !== $lastError && false !== stripos($lastError, 'interrupted system call');
  54. }
  55. /**
  56. * Unblocks streams.
  57. */
  58. protected function unblock(): void
  59. {
  60. if (!$this->blocked) {
  61. return;
  62. }
  63. foreach ($this->pipes as $pipe) {
  64. stream_set_blocking($pipe, 0);
  65. }
  66. if (\is_resource($this->input)) {
  67. stream_set_blocking($this->input, 0);
  68. }
  69. $this->blocked = false;
  70. }
  71. /**
  72. * Writes input to stdin.
  73. *
  74. * @throws InvalidArgumentException When an input iterator yields a non supported value
  75. */
  76. protected function write(): ?array
  77. {
  78. if (!isset($this->pipes[0])) {
  79. return null;
  80. }
  81. $input = $this->input;
  82. if ($input instanceof \Iterator) {
  83. if (!$input->valid()) {
  84. $input = null;
  85. } elseif (\is_resource($input = $input->current())) {
  86. stream_set_blocking($input, 0);
  87. } elseif (!isset($this->inputBuffer[0])) {
  88. if (!\is_string($input)) {
  89. if (!\is_scalar($input)) {
  90. throw new InvalidArgumentException(sprintf('"%s" yielded a value of type "%s", but only scalars and stream resources are supported.', get_debug_type($this->input), get_debug_type($input)));
  91. }
  92. $input = (string) $input;
  93. }
  94. $this->inputBuffer = $input;
  95. $this->input->next();
  96. $input = null;
  97. } else {
  98. $input = null;
  99. }
  100. }
  101. $r = $e = [];
  102. $w = [$this->pipes[0]];
  103. // let's have a look if something changed in streams
  104. if (false === @stream_select($r, $w, $e, 0, 0)) {
  105. return null;
  106. }
  107. foreach ($w as $stdin) {
  108. if (isset($this->inputBuffer[0])) {
  109. $written = fwrite($stdin, $this->inputBuffer);
  110. $this->inputBuffer = substr($this->inputBuffer, $written);
  111. if (isset($this->inputBuffer[0])) {
  112. return [$this->pipes[0]];
  113. }
  114. }
  115. if ($input) {
  116. while (true) {
  117. $data = fread($input, self::CHUNK_SIZE);
  118. if (!isset($data[0])) {
  119. break;
  120. }
  121. $written = fwrite($stdin, $data);
  122. $data = substr($data, $written);
  123. if (isset($data[0])) {
  124. $this->inputBuffer = $data;
  125. return [$this->pipes[0]];
  126. }
  127. }
  128. if (feof($input)) {
  129. if ($this->input instanceof \Iterator) {
  130. $this->input->next();
  131. } else {
  132. $this->input = null;
  133. }
  134. }
  135. }
  136. }
  137. // no input to read on resource, buffer is empty
  138. if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) {
  139. $this->input = null;
  140. fclose($this->pipes[0]);
  141. unset($this->pipes[0]);
  142. } elseif (!$w) {
  143. return [$this->pipes[0]];
  144. }
  145. return null;
  146. }
  147. /**
  148. * @internal
  149. */
  150. public function handleError(int $type, string $msg): void
  151. {
  152. $this->lastError = $msg;
  153. }
  154. }