Skip to content

Rewind stream when possible #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions src/RewindStreamException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Http\Client\Common\Plugin;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given how few classes we have i think its ok to have the exception in the main namespace. though the result when using this package together client-common will be that there is a lot of classes in this namespace: https://github.com/php-http/client-common/tree/master/src/Plugin

if we ever add exceptions for specific plugins in client-common we are likely to place them in an Exception sub namespace. for that reason i would prefer to also put this exception into a sub namespace Http\Client\Common\Plugin\Exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Putting the exception in Http\Client\Common\Plugin\Exception\RewindStreamException is preferred.


use Http\Client\Exception;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The namespace to src/ is Http\Client\Common\Plugin

But lets put this exception in Http\Client\Common\Plugin\Exception


/**
* @author Théo FIDRY <theo.fidry@gmail.com>
*/
class RewindStreamException extends \RuntimeException implements Exception
{
}