From 270de59b0dfe9670e6fd75ff034385289b37d072 Mon Sep 17 00:00:00 2001 From: Michel Hunziker Date: Thu, 20 Aug 2015 15:41:31 +0200 Subject: [PATCH] Move cache prevention logic to the Cache-Control object --- src/CacheUtil.php | 19 +++++++++++-------- src/Header/ResponseCacheControl.php | 11 +++++++++++ test/Header/ResponseCacheControlTest.php | 11 +++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/CacheUtil.php b/src/CacheUtil.php index f025c38..46df371 100644 --- a/src/CacheUtil.php +++ b/src/CacheUtil.php @@ -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); } /** @@ -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); } /** @@ -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); } diff --git a/src/Header/ResponseCacheControl.php b/src/Header/ResponseCacheControl.php index b8cbbad..98ed680 100644 --- a/src/Header/ResponseCacheControl.php +++ b/src/Header/ResponseCacheControl.php @@ -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. * diff --git a/test/Header/ResponseCacheControlTest.php b/test/Header/ResponseCacheControlTest.php index 8915b86..e525cae 100644 --- a/test/Header/ResponseCacheControlTest.php +++ b/test/Header/ResponseCacheControlTest.php @@ -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); + } }