Nessuna descrizione

CachingStream.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // Because 0 is the first byte, we seek to size - 1.
  48. $byte = $size - 1 - $offset;
  49. } else {
  50. throw new \InvalidArgumentException('Invalid whence');
  51. }
  52. $diff = $byte - $this->stream->getSize();
  53. if ($diff > 0) {
  54. // If the seek byte is greater the number of read bytes, then read
  55. // the difference of bytes to cache the bytes and inherently seek.
  56. $this->read($diff);
  57. } else {
  58. // We can just do a normal seek since we've already seen this byte.
  59. $this->stream->seek($byte);
  60. }
  61. }
  62. public function read($length)
  63. {
  64. // Perform a regular read on any previously read data from the buffer
  65. $data = $this->stream->read($length);
  66. $remaining = $length - strlen($data);
  67. // More data was requested so read from the remote stream
  68. if ($remaining) {
  69. // If data was written to the buffer in a position that would have
  70. // been filled from the remote stream, then we must skip bytes on
  71. // the remote stream to emulate overwriting bytes from that
  72. // position. This mimics the behavior of other PHP stream wrappers.
  73. $remoteData = $this->remoteStream->read(
  74. $remaining + $this->skipReadBytes
  75. );
  76. if ($this->skipReadBytes) {
  77. $len = strlen($remoteData);
  78. $remoteData = substr($remoteData, $this->skipReadBytes);
  79. $this->skipReadBytes = max(0, $this->skipReadBytes - $len);
  80. }
  81. $data .= $remoteData;
  82. $this->stream->write($remoteData);
  83. }
  84. return $data;
  85. }
  86. public function write($string)
  87. {
  88. // When appending to the end of the currently read stream, you'll want
  89. // to skip bytes from being read from the remote stream to emulate
  90. // other stream wrappers. Basically replacing bytes of data of a fixed
  91. // length.
  92. $overflow = (strlen($string) + $this->tell()) - $this->remoteStream->tell();
  93. if ($overflow > 0) {
  94. $this->skipReadBytes += $overflow;
  95. }
  96. return $this->stream->write($string);
  97. }
  98. public function eof()
  99. {
  100. return $this->stream->eof() && $this->remoteStream->eof();
  101. }
  102. /**
  103. * Close both the remote stream and buffer stream
  104. */
  105. public function close()
  106. {
  107. $this->remoteStream->close() && $this->stream->close();
  108. }
  109. private function cacheEntireStream()
  110. {
  111. $target = new FnStream(['write' => 'strlen']);
  112. copy_to_stream($this, $target);
  113. return $this->tell();
  114. }
  115. }