Development

/branches/1.1/lib/filter/sfCacheFilter.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/filter/sfCacheFilter.class.php

Revision 17858, 5.0 kB (checked in by FabianLange, 5 years ago)

[1.1, 1.2, 1.3] fixed some codeing standards (fixes #6376 - patch from gimler)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfCacheFilter deals with page caching and action caching.
13  *
14  * @package    symfony
15  * @subpackage filter
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfCacheFilter extends sfFilter
20 {
21   protected
22     $cacheManager = null,
23     $request      = null,
24     $response     = null,
25     $routing      = null,
26     $cache        = array();
27
28   /**
29    * Initializes this Filter.
30    *
31    * @param sfContext $context    The current application context
32    * @param array     $parameters An associative array of initialization parameters
33    *
34    * @return bool true, if initialization completes successfully, otherwise false
35    *
36    * @throws <b>sfInitializationException</b> If an error occurs while initializing this Filter
37    */
38   public function initialize($context, $parameters = array())
39   {
40     parent::initialize($context, $parameters);
41
42     $this->cacheManager = $context->getViewCacheManager();
43     $this->request      = $context->getRequest();
44     $this->response     = $context->getResponse();
45     $this->routing      = $context->getRouting();
46   }
47
48   /**
49    * Executes this filter.
50    *
51    * @param sfFilterChain $filterChain A sfFilterChain instance
52    */
53   public function execute($filterChain)
54   {
55     // execute this filter only once, if cache is set and no GET or POST parameters
56     if (!sfConfig::get('sf_cache'))
57     {
58       $filterChain->execute();
59
60       return;
61     }
62
63     if ($this->executeBeforeExecution())
64     {
65       $filterChain->execute();
66     }
67
68     $this->executeBeforeRendering();
69   }
70
71   public function executeBeforeExecution()
72   {
73     $uri = $this->routing->getCurrentInternalUri();
74
75     if (is_null($uri))
76     {
77       return true;
78     }
79
80     // page cache
81     $cacheable = $this->cacheManager->isCacheable($uri);
82     if ($cacheable && $this->cacheManager->withLayout($uri))
83     {
84       $inCache = $this->cacheManager->getPageCache($uri);
85       $this->cache[$uri] = $inCache;
86
87       if ($inCache)
88       {
89         // page is in cache, so no need to run execution filter
90         return false;
91       }
92     }
93
94     return true;
95   }
96
97   /**
98    * Executes this filter.
99    */
100   public function executeBeforeRendering()
101   {
102     // cache only 200 HTTP status
103     if (200 != $this->response->getStatusCode())
104     {
105       return;
106     }
107
108     $uri = $this->routing->getCurrentInternalUri();
109
110     // save page in cache
111     if (isset($this->cache[$uri]) && false === $this->cache[$uri])
112     {
113       // set some headers that deals with cache
114       if ($lifetime = $this->cacheManager->getClientLifeTime($uri, 'page'))
115       {
116         $this->response->setHttpHeader('Last-Modified', $this->response->getDate(time()), false);
117         $this->response->setHttpHeader('Expires', $this->response->getDate(time() + $lifetime), false);
118         $this->response->addCacheControlHttpHeader('max-age', $lifetime);
119       }
120
121       // set Vary headers
122       foreach ($this->cacheManager->getVary($uri, 'page') as $vary)
123       {
124         $this->response->addVaryHttpHeader($vary);
125       }
126
127       $this->cacheManager->setPageCache($uri);
128     }
129
130     // remove PHP automatic Cache-Control and Expires headers if not overwritten by application or cache
131     if ($this->response->hasHttpHeader('Last-Modified') || sfConfig::get('sf_etag'))
132     {
133       // FIXME: these headers are set by PHP sessions (see session_cache_limiter())
134       $this->response->setHttpHeader('Cache-Control', null, false);
135       $this->response->setHttpHeader('Expires', null, false);
136       $this->response->setHttpHeader('Pragma', null, false);
137     }
138
139     // Etag support
140     if (sfConfig::get('sf_etag'))
141     {
142       $etag = '"'.md5($this->response->getContent()).'"';
143       $this->response->setHttpHeader('ETag', $etag);
144
145       if ($this->request->getHttpHeader('IF_NONE_MATCH') == $etag)
146       {
147         $this->response->setStatusCode(304);
148         $this->response->setHeaderOnly(true);
149
150         if (sfConfig::get('sf_logging_enabled'))
151         {
152           $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array('ETag matches If-None-Match (send 304)')));
153         }
154       }
155     }
156
157     // conditional GET support
158     // never in debug mode
159     if ($this->response->hasHttpHeader('Last-Modified') && !sfConfig::get('sf_debug'))
160     {
161       $last_modified = $this->response->getHttpHeader('Last-Modified');
162       $last_modified = $last_modified[0];
163       if ($this->request->getHttpHeader('IF_MODIFIED_SINCE') == $last_modified)
164       {
165         $this->response->setStatusCode(304);
166         $this->response->setHeaderOnly(true);
167
168         if (sfConfig::get('sf_logging_enabled'))
169         {
170           $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array('Last-Modified matches If-Modified-Since (send 304)')));
171         }
172       }
173     }
174   }
175 }
176
Note: See TracBrowser for help on using the browser.