123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * sfCacheFilter deals with page caching and action caching.
- *
- * @package symfony
- * @subpackage filter
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfCacheFilter.class.php 13564 2008-11-30 21:34:25Z fabien $
- */
- class sfCacheFilter extends sfFilter
- {
- protected
- $cacheManager = null,
- $request = null,
- $response = null,
- $routing = null,
- $cache = array();
- /**
- * Initializes this Filter.
- *
- * @param sfContext $context The current application context
- * @param array $parameters An associative array of initialization parameters
- *
- * @return bool true, if initialization completes successfully, otherwise false
- *
- * @throws <b>sfInitializationException</b> If an error occurs while initializing this Filter
- */
- public function initialize($context, $parameters = array())
- {
- parent::initialize($context, $parameters);
- $this->cacheManager = $context->getViewCacheManager();
- $this->request = $context->getRequest();
- $this->response = $context->getResponse();
- $this->routing = $context->getRouting();
- }
- /**
- * Executes this filter.
- *
- * @param sfFilterChain $filterChain A sfFilterChain instance
- */
- public function execute($filterChain)
- {
- // execute this filter only once, if cache is set and no GET or POST parameters
- if (!sfConfig::get('sf_cache'))
- {
- $filterChain->execute();
- return;
- }
- if ($this->executeBeforeExecution())
- {
- $filterChain->execute();
- }
- $this->executeBeforeRendering();
- }
- public function executeBeforeExecution()
- {
- // register our cache configuration
- $this->cacheManager->registerConfiguration($this->context->getModuleName());
- $uri = $this->routing->getCurrentInternalUri();
- if (is_null($uri))
- {
- return true;
- }
- // page cache
- $cacheable = $this->cacheManager->isCacheable($uri);
- if ($cacheable && $this->cacheManager->withLayout($uri))
- {
- $inCache = $this->cacheManager->getPageCache($uri);
- $this->cache[$uri] = $inCache;
- if ($inCache)
- {
- // page is in cache, so no need to run execution filter
- return false;
- }
- }
- return true;
- }
- /**
- * Executes this filter.
- */
- public function executeBeforeRendering()
- {
- // cache only 200 HTTP status
- if (200 != $this->response->getStatusCode())
- {
- return;
- }
- $uri = $this->routing->getCurrentInternalUri();
- // save page in cache
- if (isset($this->cache[$uri]) && false === $this->cache[$uri])
- {
- $this->setCacheExpiration($uri);
- $this->setCacheValidation($uri);
- // set Vary headers
- foreach ($this->cacheManager->getVary($uri, 'page') as $vary)
- {
- $this->response->addVaryHttpHeader($vary);
- }
- $this->cacheManager->setPageCache($uri);
- }
- // cache validation
- $this->checkCacheValidation();
- }
- /**
- * Sets cache expiration headers.
- *
- * @param string An internal URI
- */
- protected function setCacheExpiration($uri)
- {
- // don't add cache expiration (Expires) if
- // * the client lifetime is not set
- // * the response already has a cache validation (Last-Modified header)
- // * the Expires header has already been set
- if (!$lifetime = $this->cacheManager->getClientLifeTime($uri, 'page'))
- {
- return;
- }
- if ($this->response->hasHttpHeader('Last-Modified'))
- {
- return;
- }
- if (!$this->response->hasHttpHeader('Expires'))
- {
- $this->response->setHttpHeader('Expires', $this->response->getDate(time() + $lifetime), false);
- $this->response->addCacheControlHttpHeader('max-age', $lifetime);
- }
- }
- /**
- * Sets cache validation headers.
- *
- * @param string An internal URI
- */
- protected function setCacheValidation($uri)
- {
- // don't add cache validation (Last-Modified) if
- // * the client lifetime is set (cache.yml)
- // * the response already has a Last-Modified header
- if ($this->cacheManager->getClientLifeTime($uri, 'page'))
- {
- return;
- }
- if (!$this->response->hasHttpHeader('Last-Modified'))
- {
- $this->response->setHttpHeader('Last-Modified', $this->response->getDate(time()), false);
- }
- if (sfConfig::get('sf_etag'))
- {
- $etag = '"'.md5($this->response->getContent()).'"';
- $this->response->setHttpHeader('ETag', $etag);
- }
- }
- /**
- * Checks cache validation headers.
- */
- protected function checkCacheValidation()
- {
- // Etag support
- if (sfConfig::get('sf_etag'))
- {
- $etag = '"'.md5($this->response->getContent()).'"';
- if ($this->request->getHttpHeader('IF_NONE_MATCH') == $etag)
- {
- $this->response->setStatusCode(304);
- $this->response->setHeaderOnly(true);
- if (sfConfig::get('sf_logging_enabled'))
- {
- $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array('ETag matches If-None-Match (send 304)')));
- }
- }
- }
- // conditional GET support
- // never in debug mode
- if ($this->response->hasHttpHeader('Last-Modified') && !sfConfig::get('sf_debug'))
- {
- $lastModified = $this->response->getHttpHeader('Last-Modified');
- $lastModified = $lastModified[0];
- if ($this->request->getHttpHeader('IF_MODIFIED_SINCE') == $lastModified)
- {
- $this->response->setStatusCode(304);
- $this->response->setHeaderOnly(true);
- if (sfConfig::get('sf_logging_enabled'))
- {
- $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array('Last-Modified matches If-Modified-Since (send 304)')));
- }
- }
- }
- }
- }
|