Skip to content
Permalink
Browse files

Implement withCache()

And all of the required methods underneath it.
  • Loading branch information...
markstory committed Dec 5, 2016
1 parent f2f195f commit 4f75c34f11f67c86c2972187a7cc35b6e4bd9a16
Showing with 111 additions and 4 deletions.
  1. +67 −1 src/Network/Response.php
  2. +44 −3 tests/TestCase/Network/ResponseTest.php
@@ -1209,6 +1209,7 @@ public function withDisabledCache()
* @param string $since a valid time since the response text has not been modified
* @param string $time a valid time for cache expiry
* @return void
* @deprecated 3.4.0 Use withCache() instead.
*/
public function cache($since, $time = '+1 day')
{
@@ -1224,6 +1225,25 @@ public function cache($since, $time = '+1 day')
$this->maxAge($time - time());
}
/**
* Create a new instance with the headers to enable client caching.
*
* @param string $since a valid time since the response text has not been modified
* @param string $time a valid time for cache expiry
* @return static
*/
public function withCache($since, $time = '+1 day')
{
if (!is_int($time)) {
$time = strtotime($time);
}
return $this->withHeader('Date', gmdate("D, j M Y G:i:s ", time()) . 'GMT')
->withModified($since)
->withExpires($time)
->withSharable(true)
->withMaxAge($time - time());
}
/**
* Sets whether a response is eligible to be cached by intermediate proxies
* This method controls the `public` or `private` directive in the Cache-Control
@@ -1361,6 +1381,7 @@ public function withMaxAge($seconds)
* @param bool|null $enable if null, the method will return the current
* must-revalidate value. If boolean sets or unsets the directive.
* @return bool
* @deprecated 3.4.0 Use withMustRevalidate() instead.
*/
public function mustRevalidate($enable = null)
{
@@ -1376,7 +1397,6 @@ public function mustRevalidate($enable = null)
return array_key_exists('must-revalidate', $this->_cacheDirectives);
}
/**
* Create an instance with Cache-Control must-revalidate directive set.
*
@@ -1430,6 +1450,7 @@ protected function _setCacheControl()
*
* @param string|\DateTime|null $time Valid time string or \DateTime instance.
* @return string|null
* @deprecated 3.4.0 Use withExpires() instead.
*/
public function expires($time = null)
{
@@ -1445,6 +1466,28 @@ public function expires($time = null)
return null;
}
/**
* Create a new instance with the Expires header set.
*
* ### Examples:
*
* ```
* // Will Expire the response cache now
* $response->withExpires('now')
*
* // Will set the expiration in next 24 hours
* $response->withExpires(new DateTime('+1 day'))
* ```
*
* @param string|\DateTime $time Valid time string or \DateTime instance.
* @return static
*/
public function withExpires($time)
{
$date = $this->_getUTCDate($time);
return $this->withHeader('Expires', $date->format('D, j M Y H:i:s') . ' GMT');
}
/**
* Sets the Last-Modified header for the response by taking a modification time
* If called with no parameters it will return the current Last-Modified value
@@ -1457,6 +1500,7 @@ public function expires($time = null)
*
* @param string|\DateTime|null $time Valid time string or \DateTime instance.
* @return string|null
* @deprecated 3.4.0 Use withModified() instead.
*/
public function modified($time = null)
{
@@ -1472,6 +1516,28 @@ public function modified($time = null)
return null;
}
/**
* Create a new instance with the Last-Modified header set.
*
* ### Examples:
*
* ```
* // Will Expire the response cache now
* $response->withModified('now')
*
* // Will set the expiration in next 24 hours
* $response->withModified(new DateTime('+1 day'))
* ```
*
* @param string|\DateTime $time Valid time string or \DateTime instance.
* @return static
*/
public function withModified($time)
{
$date = $this->_getUTCDate($time);
return $this->withHeader('Last-Modified', $date->format('D, j M Y H:i:s') . ' GMT');
}
/**
* Sets the response as Not Modified by removing any body contents
* setting the status code to "304 Not Modified" and removing all
@@ -562,7 +562,17 @@ public function testCache()
*/
public function testWithCache()
{
$this->markTestIncomplete();
$response = new Response();
$since = $time = time();
$new = $response->withCache($since, $time);
$this->assertFalse($response->hasHeader('Date'));
$this->assertFalse($response->hasHeader('Last-Modified'));
$this->assertEquals(gmdate("D, j M Y G:i:s ", $since) . 'GMT', $new->getHeaderLine('Date'));
$this->assertEquals(gmdate("D, j M Y H:i:s ", $since) . 'GMT', $new->getHeaderLine('Last-Modified'));
$this->assertEquals(gmdate("D, j M Y H:i:s", $time) . " GMT", $new->getHeaderLine('Expires'));
$this->assertEquals('public, max-age=0', $new->getHeaderLine('Cache-Control'));
}
/**
@@ -793,7 +803,23 @@ public function testExpires()
*/
public function testWithExpires()
{
$this->markTestIncomplete();
$format = 'D, j M Y H:i:s';
$response = new Response();
$now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
$new = $response->withExpires($now);
$this->assertFalse($response->hasHeader('Expires'));
$now->setTimeZone(new \DateTimeZone('UTC'));
$this->assertEquals($now->format($format) . ' GMT', $new->getHeaderLine('Expires'));
$now = time();
$new = $response->withExpires($now);
$this->assertEquals(gmdate($format) . ' GMT', $new->getHeaderLine('Expires'));
$time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
$new = $response->withExpires('+1 day');
$this->assertEquals($time->format($format) . ' GMT', $new->getHeaderLine('Expires'));
}
/**
@@ -831,7 +857,22 @@ public function testModified()
*/
public function testWithModified()
{
$this->markTestIncomplete();
$format = 'D, j M Y H:i:s';
$response = new Response();
$now = new \DateTime('now', new \DateTimeZone('America/Los_Angeles'));
$new = $response->withModified($now);
$this->assertFalse($response->hasHeader('Last-Modified'));
$now->setTimeZone(new \DateTimeZone('UTC'));
$this->assertEquals($now->format($format) . ' GMT', $new->getHeaderLine('Last-Modified'));
$now = time();
$new = $response->withModified($now);
$this->assertEquals(gmdate($format) . ' GMT', $new->getHeaderLine('Last-Modified'));
$time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
$new = $response->withModified('+1 day');
$this->assertEquals($time->format($format) . ' GMT', $new->getHeaderLine('Last-Modified'));
}
/**

0 comments on commit 4f75c34

Please sign in to comment.
You can’t perform that action at this time.