mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Implementing the modified() method in CakeResponse to have an easier way of setting the modification time
This commit is contained in:
parent
bab7e772d2
commit
130b827e6f
2 changed files with 61 additions and 5 deletions
|
@ -676,10 +676,10 @@ class CakeResponse {
|
|||
}
|
||||
$this->header(array(
|
||||
'Date' => gmdate("D, j M Y G:i:s ", time()) . 'GMT',
|
||||
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
||||
'Cache-Control' => 'public, max-age=' . ($time - time()),
|
||||
'Pragma' => 'cache'
|
||||
));
|
||||
$this->modified($since);
|
||||
$this->expires($time);
|
||||
}
|
||||
|
||||
|
@ -692,7 +692,7 @@ class CakeResponse {
|
|||
*
|
||||
* `$response->expires('now')` Will Expire the response cache now
|
||||
* `$response->expires(new DateTime('+1 day'))` Will set the expiration in next 24 hours
|
||||
* `$response->expires)` Will return the current expiration header value
|
||||
* `$response->expires()` Will return the current expiration header value
|
||||
*
|
||||
* @param string|DateTime $time
|
||||
* @return string
|
||||
|
@ -708,6 +708,30 @@ class CakeResponse {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Last-Modified header for the response by taking an modification time
|
||||
* If called with no parameters it will return the current Last-Modified value
|
||||
*
|
||||
* ## Examples:
|
||||
*
|
||||
* `$response->modified('now')` Will set the Last-Modified to the current time
|
||||
* `$response->modified(new DateTime('+1 day'))` Will set the modification date in the past 24 hours
|
||||
* `$response->modified()` Will return the current Last-Modified header value
|
||||
*
|
||||
* @param string|DateTime $time
|
||||
* @return string
|
||||
*/
|
||||
public function modified($time = null) {
|
||||
if ($time !== null) {
|
||||
$date = $this->_getUTCDate($time);
|
||||
$this->_headers['Last-Modified'] = $date->format('D, j M Y H:i:s') . ' GMT';
|
||||
}
|
||||
if (isset($this->_headers['Last-Modified'])) {
|
||||
return $this->_headers['Last-Modified'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a DateTime object initialized at the $time param and using UTC
|
||||
* as timezone
|
||||
|
|
|
@ -271,7 +271,7 @@ class CakeResponseTest extends CakeTestCase {
|
|||
$response->expires('+1 day');
|
||||
$expected = array(
|
||||
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
||||
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
||||
'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
|
||||
'Expires' => $time->format('D, j M Y H:i:s') . ' GMT',
|
||||
'Cache-Control' => 'public, max-age=' . ($time->format('U') - time()),
|
||||
'Pragma' => 'cache'
|
||||
|
@ -284,7 +284,7 @@ class CakeResponseTest extends CakeTestCase {
|
|||
$time = '+5 day';
|
||||
$expected = array(
|
||||
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
||||
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
||||
'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
|
||||
'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
|
||||
'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()),
|
||||
'Pragma' => 'cache'
|
||||
|
@ -297,7 +297,7 @@ class CakeResponseTest extends CakeTestCase {
|
|||
$time = time();
|
||||
$expected = array(
|
||||
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
||||
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
||||
'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
|
||||
'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT",
|
||||
'Cache-Control' => 'public, max-age=0',
|
||||
'Pragma' => 'cache'
|
||||
|
@ -603,4 +603,36 @@ class CakeResponseTest extends CakeTestCase {
|
|||
->method('_sendHeader')->with('Expires', $time->format('D, j M Y H:i:s') . ' GMT');
|
||||
$response->send();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests setting the modification date
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testModified() {
|
||||
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
||||
$now = new DateTime('now', new DateTimeZone('America/Los_Angeles'));
|
||||
$response->modified($now);
|
||||
$now->setTimeZone(new DateTimeZone('UTC'));
|
||||
$this->assertEquals($now->format('D, j M Y H:i:s') . ' GMT', $response->modified());
|
||||
$response->expects($this->at(1))
|
||||
->method('_sendHeader')->with('Last-Modified', $now->format('D, j M Y H:i:s') . ' GMT');
|
||||
$response->send();
|
||||
|
||||
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
||||
$now = time();
|
||||
$response->modified($now);
|
||||
$this->assertEquals(gmdate('D, j M Y H:i:s', $now) . ' GMT', $response->modified());
|
||||
$response->expects($this->at(1))
|
||||
->method('_sendHeader')->with('Last-Modified', gmdate('D, j M Y H:i:s', $now) . ' GMT');
|
||||
$response->send();
|
||||
|
||||
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
||||
$time = new DateTime('+1 day', new DateTimeZone('UTC'));
|
||||
$response->modified('+1 day');
|
||||
$this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
|
||||
$response->expects($this->at(1))
|
||||
->method('_sendHeader')->with('Last-Modified', $time->format('D, j M Y H:i:s') . ' GMT');
|
||||
$response->send();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue