Thanks to visit codestin.com
Credit goes to github.com

Skip to content

doc and codestyle tweaks #72

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 1 commit into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 7 additions & 7 deletions spec/CachePluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
$request->getUri()->willReturn('/');
$response->getStatusCode()->willReturn(200);
$response->getBody()->willReturn($stream);
$response->getHeader('Cache-Control')->willReturn(array());
$response->getHeader('Expires')->willReturn(array());
$response->getHeader('Cache-Control')->willReturn([]);
$response->getHeader('Expires')->willReturn([]);

$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
Expand All @@ -60,8 +60,8 @@ function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheIte
$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn('/');
$response->getStatusCode()->willReturn(400);
$response->getHeader('Cache-Control')->willReturn(array());
$response->getHeader('Expires')->willReturn(array());
$response->getHeader('Cache-Control')->willReturn([]);
$response->getHeader('Expires')->willReturn([]);

$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
Expand Down Expand Up @@ -97,9 +97,9 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
$request->getUri()->willReturn('/');
$response->getStatusCode()->willReturn(200);
$response->getBody()->willReturn($stream);
$response->getHeader('Cache-Control')->willReturn(array('max-age=40'));
$response->getHeader('Age')->willReturn(array('15'));
$response->getHeader('Expires')->willReturn(array());
$response->getHeader('Cache-Control')->willReturn(['max-age=40']);
$response->getHeader('Age')->willReturn(['15']);
$response->getHeader('Expires')->willReturn([]);

$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
Expand Down
4 changes: 2 additions & 2 deletions spec/StopwatchPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function it_records_event(Stopwatch $stopwatch, RequestInterface $request, Respo
$request->getRequestTarget()->willReturn('/');

$stopwatch->start('GET /', 'php_http.request')->shouldBeCalled();
$stopwatch->stop('GET /', 'php_http.request')->shouldBeCalled();
$stopwatch->stop('GET /')->shouldBeCalled();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

stop does not have a cateogry parameter, it only has one parameter.


$next = function (RequestInterface $request) use ($response) {
return new FulfilledPromise($response->getWrappedObject());
Expand All @@ -48,7 +48,7 @@ function it_records_event_on_error(Stopwatch $stopwatch, RequestInterface $reque
$request->getRequestTarget()->willReturn('/');

$stopwatch->start('GET /', 'php_http.request')->shouldBeCalled();
$stopwatch->stop('GET /', 'php_http.request')->shouldBeCalled();
$stopwatch->stop('GET /')->shouldBeCalled();

$next = function (RequestInterface $request) {
return new RejectedPromise(new NetworkException('', $request));
Expand Down
15 changes: 8 additions & 7 deletions src/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Allow for caching a response.
* Allow for caching a response with a PSR-6 compatible caching engine.
*
* It can follow the RFC-7234 caching specification or use a fixed cache lifetime.
*
* @author Tobias Nyholm <[email protected]>
*
Expand Down Expand Up @@ -40,8 +42,8 @@ class CachePlugin implements Plugin
* @param StreamFactory $streamFactory
* @param array $config {
*
* @var bool $respect_cache_headers whether to look at the cache directives or ignore them
* @var int $default_ttl If we do not respect cache headers or can't calculate a good ttl, use this value.
* @var bool $respect_cache_headers Whether to look at the cache directives or ignore them
* @var int $default_ttl If we do not respect cache headers or the headers specify cache control, use this value
* }
*/
public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = [])
Expand All @@ -61,18 +63,17 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
{
$method = strtoupper($request->getMethod());

// if the request not is cachable, move to $next
// if the request is not cacheable, move to $next
if ('GET' !== $method && 'HEAD' !== $method) {
return $next($request);
}

// If we can cache the request
$key = $this->createCacheKey($request);
$cacheItem = $this->pool->getItem($key);

if ($cacheItem->isHit()) {
// return cached response
$data = $cacheItem->get();
/** @var ResponseInterface $response */
$response = $data['response'];
$response = $response->withBody($this->streamFactory->createStream($data['body']));

Expand All @@ -82,7 +83,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) {
if ($this->isCacheable($response)) {
$bodyStream = $response->getBody();
$body = $bodyStream->__toString();
$body = (string) $bodyStream;
if ($bodyStream->isSeekable()) {
$bodyStream->rewind();
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/PluginClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class PluginClient implements HttpClient, HttpAsyncClient
* @param Plugin[] $plugins
* @param array $options {
*
* @var int $max_restarts
* @var int $max_restarts Number of times plugins may initiate a restart of the plugin chain. Defaults to 10.
* }
*
* @throws \RuntimeException if client is not an instance of HttpClient or HttpAsyncClient
Expand Down
2 changes: 1 addition & 1 deletion src/RedirectPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Follow redirections.
* Follow redirect responses from the server.
*
* @author Joel Wurtz <[email protected]>
*
Expand Down
4 changes: 2 additions & 2 deletions src/StopwatchPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
$this->stopwatch->start($eventName, self::CATEGORY);

return $next($request)->then(function (ResponseInterface $response) use ($eventName) {
$this->stopwatch->stop($eventName, self::CATEGORY);
$this->stopwatch->stop($eventName);

return $response;
}, function (Exception $exception) use ($eventName) {
$this->stopwatch->stop($eventName, self::CATEGORY);
$this->stopwatch->stop($eventName);

throw $exception;
});
Expand Down