From 7021c9ddc68a70756b065e8be070aa774a5304b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sat, 11 Mar 2017 16:15:58 +0000 Subject: [PATCH 1/4] Rewind stream retrieved from the cache --- src/CachePlugin.php | 11 ++++++++++- src/RewindStreamException.php | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/RewindStreamException.php diff --git a/src/CachePlugin.php b/src/CachePlugin.php index e74657a..88efb2f 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -9,6 +9,7 @@ use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use RewindStreamException; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -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/RewindStreamException.php b/src/RewindStreamException.php new file mode 100644 index 0000000..6aeb0bd --- /dev/null +++ b/src/RewindStreamException.php @@ -0,0 +1,12 @@ + + */ +class RewindStreamException extends \RuntimeException implements Exception +{ +} From 123a821b2cb62867b1ec4ca91a7d9ebc8e939fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Tue, 28 Mar 2017 07:37:48 +0100 Subject: [PATCH 2/4] Move exception to Common namespace --- composer.json | 4 ++-- spec/{ => Plugin}/CachePluginSpec.php | 0 src/{ => Plugin}/CachePlugin.php | 2 +- src/RewindStreamException.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename spec/{ => Plugin}/CachePluginSpec.php (100%) rename src/{ => Plugin}/CachePlugin.php (99%) diff --git a/composer.json b/composer.json index a4f0fe4..3124c6b 100644 --- a/composer.json +++ b/composer.json @@ -23,12 +23,12 @@ }, "autoload": { "psr-4": { - "Http\\Client\\Common\\Plugin\\": "src/" + "Http\\Client\\Common\\": "src/" } }, "autoload-dev": { "psr-4": { - "spec\\Http\\Client\\Common\\Plugin\\": "spec/" + "spec\\Http\\Client\\Common\\": "spec/" } }, "scripts": { diff --git a/spec/CachePluginSpec.php b/spec/Plugin/CachePluginSpec.php similarity index 100% rename from spec/CachePluginSpec.php rename to spec/Plugin/CachePluginSpec.php diff --git a/src/CachePlugin.php b/src/Plugin/CachePlugin.php similarity index 99% rename from src/CachePlugin.php rename to src/Plugin/CachePlugin.php index 88efb2f..3a8e640 100644 --- a/src/CachePlugin.php +++ b/src/Plugin/CachePlugin.php @@ -3,13 +3,13 @@ namespace Http\Client\Common\Plugin; use Http\Client\Common\Plugin; +use Http\Client\Common\RewindStreamException; use Http\Message\StreamFactory; use Http\Promise\FulfilledPromise; use Psr\Cache\CacheItemInterface; use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -use RewindStreamException; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; diff --git a/src/RewindStreamException.php b/src/RewindStreamException.php index 6aeb0bd..2109031 100644 --- a/src/RewindStreamException.php +++ b/src/RewindStreamException.php @@ -1,6 +1,6 @@ Date: Tue, 28 Mar 2017 07:54:16 +0100 Subject: [PATCH 3/4] move exception --- spec/{Plugin => }/CachePluginSpec.php | 4 ++-- src/{Plugin => }/CachePlugin.php | 5 ++--- src/{ => Exception}/RewindStreamException.php | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) rename spec/{Plugin => }/CachePluginSpec.php (99%) rename src/{Plugin => }/CachePlugin.php (99%) rename src/{ => Exception}/RewindStreamException.php (80%) diff --git a/spec/Plugin/CachePluginSpec.php b/spec/CachePluginSpec.php similarity index 99% rename from spec/Plugin/CachePluginSpec.php rename to spec/CachePluginSpec.php index b8f6560..c09874c 100644 --- a/spec/Plugin/CachePluginSpec.php +++ b/spec/CachePluginSpec.php @@ -1,6 +1,6 @@ shouldHaveType('Http\Client\Common\Plugin\CachePlugin'); + $this->shouldHaveType('Http\Client\Common\CachePlugin'); } function it_is_a_plugin() diff --git a/src/Plugin/CachePlugin.php b/src/CachePlugin.php similarity index 99% rename from src/Plugin/CachePlugin.php rename to src/CachePlugin.php index 3a8e640..da9729e 100644 --- a/src/Plugin/CachePlugin.php +++ b/src/CachePlugin.php @@ -1,9 +1,8 @@ Date: Tue, 28 Mar 2017 08:02:50 +0100 Subject: [PATCH 4/4] reverted --- composer.json | 4 ++-- spec/CachePluginSpec.php | 4 ++-- src/CachePlugin.php | 5 +++-- src/Exception/RewindStreamException.php | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 3124c6b..a4f0fe4 100644 --- a/composer.json +++ b/composer.json @@ -23,12 +23,12 @@ }, "autoload": { "psr-4": { - "Http\\Client\\Common\\": "src/" + "Http\\Client\\Common\\Plugin\\": "src/" } }, "autoload-dev": { "psr-4": { - "spec\\Http\\Client\\Common\\": "spec/" + "spec\\Http\\Client\\Common\\Plugin\\": "spec/" } }, "scripts": { diff --git a/spec/CachePluginSpec.php b/spec/CachePluginSpec.php index c09874c..b8f6560 100644 --- a/spec/CachePluginSpec.php +++ b/spec/CachePluginSpec.php @@ -1,6 +1,6 @@ shouldHaveType('Http\Client\Common\CachePlugin'); + $this->shouldHaveType('Http\Client\Common\Plugin\CachePlugin'); } function it_is_a_plugin() diff --git a/src/CachePlugin.php b/src/CachePlugin.php index da9729e..10b1c25 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -1,8 +1,9 @@