CachingStream.php 4.2 KB

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