CachingStream.php 4.4 KB

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