AbstractPipes.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. * stream_select() returns false when the `select` system call is interrupted by an incoming signal.
  49. */
  50. protected function hasSystemCallBeenInterrupted(): bool
  51. {
  52. $lastError = $this->lastError;
  53. $this->lastError = null;
  54. if (null === $lastError) {
  55. return false;
  56. }
  57. if (false !== stripos($lastError, 'interrupted system call')) {
  58. return true;
  59. }
  60. // on applications with a different locale than english, the message above is not found because
  61. // it's translated. So we also check for the SOCKET_EINTR constant which is defined under
  62. // Windows and UNIX-like platforms (if available on the platform).
  63. return \defined('SOCKET_EINTR') && str_starts_with($lastError, 'stream_select(): Unable to select ['.\SOCKET_EINTR.']');
  64. }
  65. /**
  66. * Unblocks streams.
  67. */
  68. protected function unblock(): void
  69. {
  70. if (!$this->blocked) {
  71. return;
  72. }
  73. foreach ($this->pipes as $pipe) {
  74. stream_set_blocking($pipe, false);
  75. }
  76. if (\is_resource($this->input)) {
  77. stream_set_blocking($this->input, false);
  78. }
  79. $this->blocked = false;
  80. }
  81. /**
  82. * Writes input to stdin.
  83. *
  84. * @throws InvalidArgumentException When an input iterator yields a non supported value
  85. */
  86. protected function write(): ?array
  87. {
  88. if (!isset($this->pipes[0])) {
  89. return null;
  90. }
  91. $input = $this->input;
  92. if ($input instanceof \Iterator) {
  93. if (!$input->valid()) {
  94. $input = null;
  95. } elseif (\is_resource($input = $input->current())) {
  96. stream_set_blocking($input, false);
  97. } elseif (!isset($this->inputBuffer[0])) {
  98. if (!\is_string($input)) {
  99. if (!\is_scalar($input)) {
  100. 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)));
  101. }
  102. $input = (string) $input;
  103. }
  104. $this->inputBuffer = $input;
  105. $this->input->next();
  106. $input = null;
  107. } else {
  108. $input = null;
  109. }
  110. }
  111. $r = $e = [];
  112. $w = [$this->pipes[0]];
  113. // let's have a look if something changed in streams
  114. if (false === @stream_select($r, $w, $e, 0, 0)) {
  115. return null;
  116. }
  117. foreach ($w as $stdin) {
  118. if (isset($this->inputBuffer[0])) {
  119. if (false === $written = @fwrite($stdin, $this->inputBuffer)) {
  120. return $this->closeBrokenInputPipe();
  121. }
  122. $this->inputBuffer = substr($this->inputBuffer, $written);
  123. if (isset($this->inputBuffer[0]) && isset($this->pipes[0])) {
  124. return [$this->pipes[0]];
  125. }
  126. }
  127. if ($input) {
  128. while (true) {
  129. $data = fread($input, self::CHUNK_SIZE);
  130. if (!isset($data[0])) {
  131. break;
  132. }
  133. if (false === $written = @fwrite($stdin, $data)) {
  134. return $this->closeBrokenInputPipe();
  135. }
  136. $data = substr($data, $written);
  137. if (isset($data[0])) {
  138. $this->inputBuffer = $data;
  139. return isset($this->pipes[0]) ? [$this->pipes[0]] : null;
  140. }
  141. }
  142. if (feof($input)) {
  143. if ($this->input instanceof \Iterator) {
  144. $this->input->next();
  145. } else {
  146. $this->input = null;
  147. }
  148. }
  149. }
  150. }
  151. // no input to read on resource, buffer is empty
  152. if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) {
  153. $this->input = null;
  154. fclose($this->pipes[0]);
  155. unset($this->pipes[0]);
  156. } elseif (!$w) {
  157. return [$this->pipes[0]];
  158. }
  159. return null;
  160. }
  161. private function closeBrokenInputPipe(): void
  162. {
  163. $this->lastError = error_get_last()['message'] ?? null;
  164. if (\is_resource($this->pipes[0] ?? null)) {
  165. fclose($this->pipes[0]);
  166. }
  167. unset($this->pipes[0]);
  168. $this->input = null;
  169. $this->inputBuffer = '';
  170. }
  171. /**
  172. * @internal
  173. */
  174. public function handleError(int $type, string $msg): void
  175. {
  176. $this->lastError = $msg;
  177. }
  178. }