CachingStream.php 4.5 KB

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