AbstractPipes.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /**
  12. * @author Romain Neutron <imprec@gmail.com>
  13. *
  14. * @internal
  15. */
  16. abstract class AbstractPipes implements PipesInterface
  17. {
  18. public $pipes = array();
  19. private $inputBuffer = '';
  20. private $input;
  21. private $blocked = true;
  22. private $lastError;
  23. /**
  24. * @param resource|null $input
  25. */
  26. public function __construct($input)
  27. {
  28. if (\is_resource($input)) {
  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 = $this->lastError;
  54. $this->lastError = null;
  55. // stream_select returns false when the `select` system call is interrupted by an incoming signal
  56. return null !== $lastError && false !== stripos($lastError, '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 (null !== $this->input) {
  70. stream_set_blocking($this->input, 0);
  71. }
  72. $this->blocked = false;
  73. }
  74. /**
  75. * Writes input to stdin.
  76. */
  77. protected function write()
  78. {
  79. if (!isset($this->pipes[0])) {
  80. return;
  81. }
  82. $input = $this->input;
  83. $r = $e = array();
  84. $w = array($this->pipes[0]);
  85. // let's have a look if something changed in streams
  86. if (false === @stream_select($r, $w, $e, 0, 0)) {
  87. return;
  88. }
  89. foreach ($w as $stdin) {
  90. if (isset($this->inputBuffer[0])) {
  91. $written = fwrite($stdin, $this->inputBuffer);
  92. $this->inputBuffer = substr($this->inputBuffer, $written);
  93. if (isset($this->inputBuffer[0])) {
  94. return array($this->pipes[0]);
  95. }
  96. }
  97. if ($input) {
  98. for (;;) {
  99. $data = fread($input, self::CHUNK_SIZE);
  100. if (!isset($data[0])) {
  101. break;
  102. }
  103. $written = fwrite($stdin, $data);
  104. $data = substr($data, $written);
  105. if (isset($data[0])) {
  106. $this->inputBuffer = $data;
  107. return array($this->pipes[0]);
  108. }
  109. }
  110. if (feof($input)) {
  111. // no more data to read on input resource
  112. // use an empty buffer in the next reads
  113. $this->input = null;
  114. }
  115. }
  116. }
  117. // no input to read on resource, buffer is empty
  118. if (null === $this->input && !isset($this->inputBuffer[0])) {
  119. fclose($this->pipes[0]);
  120. unset($this->pipes[0]);
  121. } elseif (!$w) {
  122. return array($this->pipes[0]);
  123. }
  124. }
  125. /**
  126. * @internal
  127. */
  128. public function handleError($type, $msg)
  129. {
  130. $this->lastError = $msg;
  131. }
  132. }