AbstractPipes.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 = array();
  20. private $inputBuffer = '';
  21. private $input;
  22. private $blocked = true;
  23. /**
  24. * @param resource|string|int|float|bool|\Iterator|null $input
  25. */
  26. public function __construct($input)
  27. {
  28. if (is_resource($input) || $input instanceof \Iterator) {
  29. $this->input = $input;
  30. } elseif (is_string($input)) {
  31. $this->inputBuffer = $input;
  32. } else {
  33. $this->inputBuffer = (string) $input;
  34. }
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function close()
  40. {
  41. foreach ($this->pipes as $pipe) {
  42. fclose($pipe);
  43. }
  44. $this->pipes = array();
  45. }
  46. /**
  47. * Returns true if a system call has been interrupted.
  48. *
  49. * @return bool
  50. */
  51. protected function hasSystemCallBeenInterrupted()
  52. {
  53. $lastError = error_get_last();
  54. // stream_select returns false when the `select` system call is interrupted by an incoming signal
  55. return isset($lastError['message']) && false !== stripos($lastError['message'], '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()
  79. {
  80. if (!isset($this->pipes[0])) {
  81. return;
  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_class($this->input), gettype($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 = array();
  104. $w = array($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;
  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 array($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 array($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 array($this->pipes[0]);
  146. }
  147. }
  148. }