CachingStream.php 4.1 KB

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