AbstractPipes.php 4.9 KB

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