Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

Commit

Permalink
Move cache prevention logic to the Cache-Control object
Browse files Browse the repository at this point in the history
  • Loading branch information
micheh committed Aug 20, 2015
1 parent ec842db commit 270de59
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/CacheUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ class CacheUtil
*/
public function withCache(ResponseInterface $response, $public = false, $maxAge = 600)
{
$type = $public ? 'public' : 'private';
$age = max(0, (int) $maxAge);
$control = new ResponseCacheControl();
$control = $public ? $control->withPublic() : $control->withPrivate();
$control = $control->withMaxAge($maxAge);

return $this->withCacheControl($response, $type . ', max-age=' . $age);
return $this->withCacheControl($response, $control);
}

/**
Expand All @@ -58,7 +59,10 @@ public function withCache(ResponseInterface $response, $public = false, $maxAge
*/
public function withCachePrevention(ResponseInterface $response)
{
return $this->withCacheControl($response, 'no-cache, no-store, must-revalidate');
$control = new ResponseCacheControl();
$control = $control->withCachePrevention();

return $this->withCacheControl($response, $control);
}

/**
Expand Down Expand Up @@ -154,17 +158,16 @@ public function withLastModified(ResponseInterface $response, $time)
}

/**
* Method to add a Cache-Control header to the provided PSR-7 message. The cache control
* parameter can either be a string or a Cache-Control object.
* Method to add a Cache-Control header to the provided PSR-7 message.
*
* @link https://tools.ietf.org/html/rfc7234#section-5.2
*
* @param MessageInterface $message PSR-7 message to add the Cache-Control header to
* @param string|CacheControl $cacheControl Cache-Control string or object
* @param CacheControl $cacheControl Cache-Control object to add to the message
* @return MessageInterface The PSR-7 message with the added Cache-Control header
* @throws InvalidArgumentException If the Cache-Control header is invalid
*/
public function withCacheControl(MessageInterface $message, $cacheControl)
public function withCacheControl(MessageInterface $message, CacheControl $cacheControl)
{
return $message->withHeader('Cache-Control', (string) $cacheControl);
}
Expand Down
11 changes: 11 additions & 0 deletions src/Header/ResponseCacheControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ public function hasProxyRevalidate()
return $this->hasDirective('proxy-revalidate');
}

/**
* Convenience method to set flags which should prevent the client from caching.
* Adds `no-cache, no-store, must-revalidate`.
*
* @return static
*/
public function withCachePrevention()
{
return $this->withNoCache()->withNoStore()->withMustRevalidate();
}

/**
* Sets the flag for the public and private directives.
*
Expand Down
11 changes: 11 additions & 0 deletions test/Header/ResponseCacheControlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,15 @@ public function testHasProxyRevalidate()
$control = $this->getControlWithHasFlag('proxy-revalidate');
$this->assertReturn($control->hasProxyRevalidate());
}

/**
* @covers Micheh\Cache\Header\ResponseCacheControl::withCachePrevention
*/
public function testWithCachePrevention()
{
$control = $this->cacheControl->withCachePrevention();
$directives = ['no-cache' => true, 'no-store' => true, 'must-revalidate' => true];

$this->assertAttributeSame($directives, 'directives', $control);
}
}

0 comments on commit 270de59

Please sign in to comment.