Drupal investigation

AbstractPipes.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /** @var array */
  19. public $pipes = array();
  20. /** @var string */
  21. private $inputBuffer = '';
  22. /** @var resource|null */
  23. private $input;
  24. /** @var bool */
  25. private $blocked = true;
  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 = 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 (null !== $this->input) {
  69. stream_set_blocking($this->input, 0);
  70. }
  71. $this->blocked = false;
  72. }
  73. /**
  74. * Writes input to stdin.
  75. */
  76. protected function write()
  77. {
  78. if (!isset($this->pipes[0])) {
  79. return;
  80. }
  81. $input = $this->input;
  82. $r = $e = array();
  83. $w = array($this->pipes[0]);
  84. // let's have a look if something changed in streams
  85. if (false === $n = @stream_select($r, $w, $e, 0, 0)) {
  86. return;
  87. }
  88. foreach ($w as $stdin) {
  89. if (isset($this->inputBuffer[0])) {
  90. $written = fwrite($stdin, $this->inputBuffer);
  91. $this->inputBuffer = substr($this->inputBuffer, $written);
  92. if (isset($this->inputBuffer[0])) {
  93. return array($this->pipes[0]);
  94. }
  95. }
  96. if ($input) {
  97. for (;;) {
  98. $data = fread($input, self::CHUNK_SIZE);
  99. if (!isset($data[0])) {
  100. break;
  101. }
  102. $written = fwrite($stdin, $data);
  103. $data = substr($data, $written);
  104. if (isset($data[0])) {
  105. $this->inputBuffer = $data;
  106. return array($this->pipes[0]);
  107. }
  108. }
  109. if (feof($input)) {
  110. // no more data to read on input resource
  111. // use an empty buffer in the next reads
  112. $this->input = null;
  113. }
  114. }
  115. }
  116. // no input to read on resource, buffer is empty
  117. if (null === $this->input && !isset($this->inputBuffer[0])) {
  118. fclose($this->pipes[0]);
  119. unset($this->pipes[0]);
  120. } elseif (!$w) {
  121. return array($this->pipes[0]);
  122. }
  123. }
  124. }