diff --git a/src/CachePlugin.php b/src/CachePlugin.php index e74657a..10b1c25 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -3,6 +3,7 @@ namespace Http\Client\Common\Plugin; use Http\Client\Common\Plugin; +use Http\Client\Common\Plugin\Exception\RewindStreamException; use Http\Message\StreamFactory; use Http\Promise\FulfilledPromise; use Psr\Cache\CacheItemInterface; @@ -379,7 +380,15 @@ private function createResponseFromCacheItem(CacheItemInterface $cacheItem) /** @var ResponseInterface $response */ $response = $data['response']; - $response = $response->withBody($this->streamFactory->createStream($data['body'])); + $stream = $this->streamFactory->createStream($data['body']); + + try { + $stream->rewind(); + } catch (\Exception $e) { + throw new RewindStreamException('Cannot rewind stream.', 0, $e); + } + + $response = $response->withBody($stream); return $response; } diff --git a/src/Exception/RewindStreamException.php b/src/Exception/RewindStreamException.php new file mode 100644 index 0000000..1b9eaee --- /dev/null +++ b/src/Exception/RewindStreamException.php @@ -0,0 +1,12 @@ + + */ +class RewindStreamException extends \RuntimeException implements Exception +{ +}