From b4a9d6f70eef2bb09c757afe662f02d55691894f Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sat, 17 Aug 2013 08:44:31 -0700 Subject: [PATCH] Fixed issue with 304 caching. Fixed PHP 5.3 compatibility. --- flight/Engine.php | 14 ++++++++------ flight/net/Response.php | 28 +++++++++++++++------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/flight/Engine.php b/flight/Engine.php index 19c6064..5d8c194 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -75,6 +75,7 @@ class Engine { */ public function init() { static $initialized = false; + $self = $this; if ($initialized) { $this->vars = array(); @@ -86,8 +87,8 @@ class Engine { $this->loader->register('request', '\flight\net\Request'); $this->loader->register('response', '\flight\net\Response'); $this->loader->register('router', '\flight\net\Router'); - $this->loader->register('view', '\flight\template\View', array(), function($view) { - $view->path = $this->get('flight.views.path'); + $this->loader->register('view', '\flight\template\View', array(), function($view) use ($self) { + $view->path = $self->get('flight.views.path'); }); // Register framework methods @@ -320,12 +321,13 @@ class Engine { * * @param int $code HTTP status code * @param string $message Response message + * @param bool $cache Cache response */ - public function _halt($code = 200, $message = '') { + public function _halt($code = 200, $message = '', $cache = false) { $this->response(false) ->status($code) ->write($message) - ->cache(false) + ->cache($cache) ->send(); } @@ -438,7 +440,7 @@ class Engine { $this->response()->header('ETag', $id); if ($id === getenv('HTTP_IF_NONE_MATCH')) { - $this->halt(304); + $this->halt(304, '', true); } } @@ -451,7 +453,7 @@ class Engine { $this->response()->header('Last-Modified', date(DATE_RFC1123, $time)); if ($time === strtotime(getenv('HTTP_IF_MODIFIED_SINCE'))) { - $this->halt(304); + $this->halt(304, '', true); } } } diff --git a/flight/net/Response.php b/flight/net/Response.php index 4ce1d00..77a232b 100644 --- a/flight/net/Response.php +++ b/flight/net/Response.php @@ -151,19 +151,21 @@ class Response { * @return object Self reference */ public function cache($expires) { - if ($expires === false) { - $this->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT'; - $this->headers['Cache-Control'] = array( - 'no-store, no-cache, must-revalidate', - 'post-check=0, pre-check=0', - 'max-age=0' - ); - $this->headers['Pragma'] = 'no-cache'; - } - else { - $expires = is_int($expires) ? $expires : strtotime($expires); - $this->headers['Expires'] = gmdate('D, d M Y H:i:s', $expires) . ' GMT'; - $this->headers['Cache-Control'] = 'max-age='.($expires - time()); + if ($expires !== true) { + if ($expires === false) { + $this->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT'; + $this->headers['Cache-Control'] = array( + 'no-store, no-cache, must-revalidate', + 'post-check=0, pre-check=0', + 'max-age=0' + ); + $this->headers['Pragma'] = 'no-cache'; + } + else { + $expires = is_int($expires) ? $expires : strtotime($expires); + $this->headers['Expires'] = gmdate('D, d M Y H:i:s', $expires) . ' GMT'; + $this->headers['Cache-Control'] = 'max-age='.($expires - time()); + } } return $this;