AbstractPipes.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. * @return bool
  51. */
  52. protected function hasSystemCallBeenInterrupted()
  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. * @return array|null
  79. *
  80. * @throws InvalidArgumentException When an input iterator yields a non supported value
  81. */
  82. protected function write()
  83. {
  84. if (!isset($this->pipes[0])) {
  85. return null;
  86. }
  87. $input = $this->input;
  88. if ($input instanceof \Iterator) {
  89. if (!$input->valid()) {
  90. $input = null;
  91. } elseif (\is_resource($input = $input->current())) {
  92. stream_set_blocking($input, 0);
  93. } elseif (!isset($this->inputBuffer[0])) {
  94. if (!\is_string($input)) {
  95. if (!is_scalar($input)) {
  96. 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)));
  97. }
  98. $input = (string) $input;
  99. }
  100. $this->inputBuffer = $input;
  101. $this->input->next();
  102. $input = null;
  103. } else {
  104. $input = null;
  105. }
  106. }
  107. $r = $e = [];
  108. $w = [$this->pipes[0]];
  109. // let's have a look if something changed in streams
  110. if (false === @stream_select($r, $w, $e, 0, 0)) {
  111. return null;
  112. }
  113. foreach ($w as $stdin) {
  114. if (isset($this->inputBuffer[0])) {
  115. $written = fwrite($stdin, $this->inputBuffer);
  116. $this->inputBuffer = substr($this->inputBuffer, $written);
  117. if (isset($this->inputBuffer[0])) {
  118. return [$this->pipes[0]];
  119. }
  120. }
  121. if ($input) {
  122. for (;;) {
  123. $data = fread($input, self::CHUNK_SIZE);
  124. if (!isset($data[0])) {
  125. break;
  126. }
  127. $written = fwrite($stdin, $data);
  128. $data = substr($data, $written);
  129. if (isset($data[0])) {
  130. $this->inputBuffer = $data;
  131. return [$this->pipes[0]];
  132. }
  133. }
  134. if (feof($input)) {
  135. if ($this->input instanceof \Iterator) {
  136. $this->input->next();
  137. } else {
  138. $this->input = null;
  139. }
  140. }
  141. }
  142. }
  143. // no input to read on resource, buffer is empty
  144. if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) {
  145. $this->input = null;
  146. fclose($this->pipes[0]);
  147. unset($this->pipes[0]);
  148. } elseif (!$w) {
  149. return [$this->pipes[0]];
  150. }
  151. return null;
  152. }
  153. /**
  154. * @internal
  155. */
  156. public function handleError($type, $msg)
  157. {
  158. $this->lastError = $msg;
  159. }
  160. }