No Description

AbstractPipes.php 4.9KB

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