Skip to content

Commit 130b827

Browse files
committed
Implementing the modified() method in CakeResponse to have an easier way of setting the modification time
1 parent bab7e77 commit 130b827

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

lib/Cake/Network/CakeResponse.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,10 @@ public function cache($since, $time = '+1 day') {
676676
}
677677
$this->header(array(
678678
'Date' => gmdate("D, j M Y G:i:s ", time()) . 'GMT',
679-
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
680679
'Cache-Control' => 'public, max-age=' . ($time - time()),
681680
'Pragma' => 'cache'
682681
));
682+
$this->modified($since);
683683
$this->expires($time);
684684
}
685685

@@ -692,7 +692,7 @@ public function cache($since, $time = '+1 day') {
692692
*
693693
* `$response->expires('now')` Will Expire the response cache now
694694
* `$response->expires(new DateTime('+1 day'))` Will set the expiration in next 24 hours
695-
* `$response->expires)` Will return the current expiration header value
695+
* `$response->expires()` Will return the current expiration header value
696696
*
697697
* @param string|DateTime $time
698698
* @return string
@@ -708,6 +708,30 @@ public function expires($time = null) {
708708
return null;
709709
}
710710

711+
/**
712+
* Sets the Last-Modified header for the response by taking an modification time
713+
* If called with no parameters it will return the current Last-Modified value
714+
*
715+
* ## Examples:
716+
*
717+
* `$response->modified('now')` Will set the Last-Modified to the current time
718+
* `$response->modified(new DateTime('+1 day'))` Will set the modification date in the past 24 hours
719+
* `$response->modified()` Will return the current Last-Modified header value
720+
*
721+
* @param string|DateTime $time
722+
* @return string
723+
*/
724+
public function modified($time = null) {
725+
if ($time !== null) {
726+
$date = $this->_getUTCDate($time);
727+
$this->_headers['Last-Modified'] = $date->format('D, j M Y H:i:s') . ' GMT';
728+
}
729+
if (isset($this->_headers['Last-Modified'])) {
730+
return $this->_headers['Last-Modified'];
731+
}
732+
return null;
733+
}
734+
711735
/**
712736
* Returns a DateTime object initialized at the $time param and using UTC
713737
* as timezone

lib/Cake/Test/Case/Network/CakeResponseTest.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public function testCache() {
271271
$response->expires('+1 day');
272272
$expected = array(
273273
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
274-
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
274+
'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
275275
'Expires' => $time->format('D, j M Y H:i:s') . ' GMT',
276276
'Cache-Control' => 'public, max-age=' . ($time->format('U') - time()),
277277
'Pragma' => 'cache'
@@ -284,7 +284,7 @@ public function testCache() {
284284
$time = '+5 day';
285285
$expected = array(
286286
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
287-
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
287+
'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
288288
'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
289289
'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()),
290290
'Pragma' => 'cache'
@@ -297,7 +297,7 @@ public function testCache() {
297297
$time = time();
298298
$expected = array(
299299
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
300-
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
300+
'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT',
301301
'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT",
302302
'Cache-Control' => 'public, max-age=0',
303303
'Pragma' => 'cache'
@@ -603,4 +603,36 @@ public function testExpires() {
603603
->method('_sendHeader')->with('Expires', $time->format('D, j M Y H:i:s') . ' GMT');
604604
$response->send();
605605
}
606+
607+
/**
608+
* Tests setting the modification date
609+
*
610+
* @return void
611+
*/
612+
public function testModified() {
613+
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
614+
$now = new DateTime('now', new DateTimeZone('America/Los_Angeles'));
615+
$response->modified($now);
616+
$now->setTimeZone(new DateTimeZone('UTC'));
617+
$this->assertEquals($now->format('D, j M Y H:i:s') . ' GMT', $response->modified());
618+
$response->expects($this->at(1))
619+
->method('_sendHeader')->with('Last-Modified', $now->format('D, j M Y H:i:s') . ' GMT');
620+
$response->send();
621+
622+
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
623+
$now = time();
624+
$response->modified($now);
625+
$this->assertEquals(gmdate('D, j M Y H:i:s', $now) . ' GMT', $response->modified());
626+
$response->expects($this->at(1))
627+
->method('_sendHeader')->with('Last-Modified', gmdate('D, j M Y H:i:s', $now) . ' GMT');
628+
$response->send();
629+
630+
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
631+
$time = new DateTime('+1 day', new DateTimeZone('UTC'));
632+
$response->modified('+1 day');
633+
$this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified());
634+
$response->expects($this->at(1))
635+
->method('_sendHeader')->with('Last-Modified', $time->format('D, j M Y H:i:s') . ' GMT');
636+
$response->send();
637+
}
606638
}

0 commit comments

Comments
 (0)