CachingStream.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use Psr\Http\Message\StreamInterface;
  4. /**
  5. * Stream decorator that can cache previously read bytes from a sequentially
  6. * read stream.
  7. *
  8. * @final
  9. */
  10. class CachingStream implements StreamInterface
  11. {
  12. use StreamDecoratorTrait;
  13. /** @var StreamInterface Stream being wrapped */
  14. private $remoteStream;
  15. /** @var int Number of bytes to skip reading due to a write on the buffer */
  16. private $skipReadBytes = 0;
  17. /**
  18. * We will treat the buffer object as the body of the stream
  19. *
  20. * @param StreamInterface $stream Stream to cache. The cursor is assumed to be at the beginning of the stream.
  21. * @param StreamInterface $target Optionally specify where data is cached
  22. */
  23. public function __construct(
  24. StreamInterface $stream,
  25. StreamInterface $target = null
  26. ) {
  27. $this->remoteStream = $stream;
  28. $this->stream = $target ?: new Stream(Utils::tryFopen('php://temp', 'r+'));
  29. }
  30. public function getSize()
  31. {
  32. $remoteSize = $this->remoteStream->getSize();
  33. if (null === $remoteSize) {
  34. return null;
  35. }
  36. return max($this->stream->getSize(), $remoteSize);
  37. }
  38. public function rewind()
  39. {
  40. $this->seek(0);
  41. }
  42. public function seek($offset, $whence = SEEK_SET)
  43. {
  44. if ($whence == SEEK_SET) {
  45. $byte = $offset;
  46. } elseif ($whence == SEEK_CUR) {
  47. $byte = $offset + $this->tell();
  48. } elseif ($whence == SEEK_END) {
  49. $size = $this->remoteStream->getSize();
  50. if ($size === null) {
  51. $size = $this->cacheEntireStream();
  52. }
  53. $byte = $size + $offset;
  54. } else {
  55. throw new \InvalidArgumentException('Invalid whence');
  56. }
  57. $diff = $byte - $this->stream->getSize();
  58. if ($diff > 0) {
  59. // Read the remoteStream until we have read in at least the amount
  60. // of bytes requested, or we reach the end of the file.
  61. while ($diff > 0 && !$this->remoteStream->eof()) {
  62. $this->read($diff);
  63. $diff = $byte - $this->stream->getSize();
  64. }
  65. } else {
  66. // We can just do a normal seek since we've already seen this byte.
  67. $this->stream->seek($byte);
  68. }
  69. }
  70. public function read($length)
  71. {
  72. // Perform a regular read on any previously read data from the buffer
  73. $data = $this->stream->read($length);
  74. $remaining = $length - strlen($data);
  75. // More data was requested so read from the remote stream
  76. if ($remaining) {
  77. // If data was written to the buffer in a position that would have
  78. // been filled from the remote stream, then we must skip bytes on
  79. // the remote stream to emulate overwriting bytes from that
  80. // position. This mimics the behavior of other PHP stream wrappers.
  81. $remoteData = $this->remoteStream->read(
  82. $remaining + $this->skipReadBytes
  83. );
  84. if ($this->skipReadBytes) {
  85. $len = strlen($remoteData);
  86. $remoteData = substr($remoteData, $this->skipReadBytes);
  87. $this->skipReadBytes = max(0, $this->skipReadBytes - $len);
  88. }
  89. $data .= $remoteData;
  90. $this->stream->write($remoteData);
  91. }
  92. return $data;
  93. }
  94. public function write($string)
  95. {
  96. // When appending to the end of the currently read stream, you'll want
  97. // to skip bytes from being read from the remote stream to emulate
  98. // other stream wrappers. Basically replacing bytes of data of a fixed
  99. // length.
  100. $overflow = (strlen($string) + $this->tell()) - $this->remoteStream->tell();
  101. if ($overflow > 0) {
  102. $this->skipReadBytes += $overflow;
  103. }
  104. return $this->stream->write($string);
  105. }
  106. public function eof()
  107. {
  108. return $this->stream->eof() && $this->remoteStream->eof();
  109. }
  110. /**
  111. * Close both the remote stream and buffer stream
  112. */
  113. public function close()
  114. {
  115. $this->remoteStream->close() && $this->stream->close();
  116. }
  117. private function cacheEntireStream()
  118. {
  119. $target = new FnStream(['write' => 'strlen']);
  120. Utils::copyToStream($this, $target);
  121. return $this->tell();
  122. }
  123. }