Exemple #1
0
 /**
  * Prepares the response for caching by the decorator. Sets up headers and renders the response
  * body due to some caching methods not serialising [Kohana_View] correctly. The `_prepare_response()`
  * method performs the following.
  *
  * 1.  Set correct headers to the Response object, formatting `Cache-Control` correctly
  * 2.  Creates an `Expires:` header if required
  * 3.  Renders the [Kohana_Reponse]::`$body`
  *
  * @param   Kohana_Response  response 
  * @return  Kohana_Response
  */
 protected function _prepare_response(Kohana_Response $response)
 {
     // Get the cache control headers
     $cache_control = Request_Cache::parse_cache_control($response->headers);
     // Set the lifetime of the cache from the header
     $this->_lifetime = (int) $cache_control['max-age'];
     // Get time now
     $time = time();
     // If the expires header is not set
     if (!isset($response->headers['Expires'])) {
         // Calculate expires header (DateTime would probably be better here - SdF)
         $expires = gmdate('D, d M Y H:i:s T', $time + $cache_control['max-age']);
     }
     // Tell caches to check their validation
     $cache_control['must-revalidate'] = NULL;
     // Replace the headers with those that are not set
     $response->headers += array('Cache-Control' => Request_Cache::create_cache_control($cache_control), 'Expires' => $expires, 'Last-Modified' => gmdate('D, d M Y H:i:s T', $time), 'Content-Length' => strlen((string) $response->body));
     // Render the body (some caches have trouble serializing)
     $response->body = (string) $response->body;
     // return
     return $response;
 }