AbstractPipes.php 4.9 KB

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