AbstractPipes.php 4.8 KB

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