Skip to content

Commit 55f531a

Browse files
theofidryNyholm
authored andcommitted
Rewind stream when possible (#29)
* Rewind stream retrieved from the cache * Move exception to Common namespace * move exception * reverted
1 parent 7789139 commit 55f531a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/CachePlugin.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Http\Client\Common\Plugin;
44

55
use Http\Client\Common\Plugin;
6+
use Http\Client\Common\Plugin\Exception\RewindStreamException;
67
use Http\Message\StreamFactory;
78
use Http\Promise\FulfilledPromise;
89
use Psr\Cache\CacheItemInterface;
@@ -379,7 +380,15 @@ private function createResponseFromCacheItem(CacheItemInterface $cacheItem)
379380

380381
/** @var ResponseInterface $response */
381382
$response = $data['response'];
382-
$response = $response->withBody($this->streamFactory->createStream($data['body']));
383+
$stream = $this->streamFactory->createStream($data['body']);
384+
385+
try {
386+
$stream->rewind();
387+
} catch (\Exception $e) {
388+
throw new RewindStreamException('Cannot rewind stream.', 0, $e);
389+
}
390+
391+
$response = $response->withBody($stream);
383392

384393
return $response;
385394
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Http\Client\Common\Plugin\Exception;
4+
5+
use Http\Client\Exception;
6+
7+
/**
8+
* @author Théo FIDRY <[email protected]>
9+
*/
10+
class RewindStreamException extends \RuntimeException implements Exception
11+
{
12+
}

0 commit comments

Comments
 (0)