027b2b1761334cd8ab712dc3194e6017b496442b
[yaffs-website] / vendor / symfony / http-kernel / HttpCache / ResponseCacheStrategy.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * This code is partially based on the Rack-Cache library by Ryan Tomayko,
9  * which is released under the MIT license.
10  * (based on commit 02d2b48d75bcb63cf1c0c7149c077ad256542801)
11  *
12  * For the full copyright and license information, please view the LICENSE
13  * file that was distributed with this source code.
14  */
15
16 namespace Symfony\Component\HttpKernel\HttpCache;
17
18 use Symfony\Component\HttpFoundation\Response;
19
20 /**
21  * ResponseCacheStrategy knows how to compute the Response cache HTTP header
22  * based on the different response cache headers.
23  *
24  * This implementation changes the master response TTL to the smallest TTL received
25  * or force validation if one of the surrogates has validation cache strategy.
26  *
27  * @author Fabien Potencier <fabien@symfony.com>
28  */
29 class ResponseCacheStrategy implements ResponseCacheStrategyInterface
30 {
31     private $cacheable = true;
32     private $embeddedResponses = 0;
33     private $ttls = array();
34     private $maxAges = array();
35     private $isNotCacheableResponseEmbedded = false;
36
37     /**
38      * {@inheritdoc}
39      */
40     public function add(Response $response)
41     {
42         if (!$response->isFresh() || !$response->isCacheable()) {
43             $this->cacheable = false;
44         } else {
45             $maxAge = $response->getMaxAge();
46             $this->ttls[] = $response->getTtl();
47             $this->maxAges[] = $maxAge;
48
49             if (null === $maxAge) {
50                 $this->isNotCacheableResponseEmbedded = true;
51             }
52         }
53
54         ++$this->embeddedResponses;
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     public function update(Response $response)
61     {
62         // if we have no embedded Response, do nothing
63         if (0 === $this->embeddedResponses) {
64             return;
65         }
66
67         // Remove validation related headers in order to avoid browsers using
68         // their own cache, because some of the response content comes from
69         // at least one embedded response (which likely has a different caching strategy).
70         if ($response->isValidateable()) {
71             $response->setEtag(null);
72             $response->setLastModified(null);
73         }
74
75         if (!$response->isFresh()) {
76             $this->cacheable = false;
77         }
78
79         if (!$this->cacheable) {
80             $response->headers->set('Cache-Control', 'no-cache, must-revalidate');
81
82             return;
83         }
84
85         $this->ttls[] = $response->getTtl();
86         $this->maxAges[] = $response->getMaxAge();
87
88         if ($this->isNotCacheableResponseEmbedded) {
89             $response->headers->removeCacheControlDirective('s-maxage');
90         } elseif (null !== $maxAge = min($this->maxAges)) {
91             $response->setSharedMaxAge($maxAge);
92             $response->headers->set('Age', $maxAge - min($this->ttls));
93         }
94         $response->setMaxAge(0);
95     }
96 }