vendor/symfony/src/Symfony/Component/HttpKernel/HttpCache/EsiResponseCacheStrategy.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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  * EsiResponseCacheStrategy knows how to compute the Response cache HTTP header
       
    22  * based on the different ESI 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 ESI has validation cache strategy.
       
    26  *
       
    27  * @author Fabien Potencier <fabien@symfony.com>
       
    28  */
       
    29 class EsiResponseCacheStrategy implements EsiResponseCacheStrategyInterface
       
    30 {
       
    31     private $cacheable = true;
       
    32     private $ttls = array();
       
    33     private $maxAges = array();
       
    34 
       
    35     /**
       
    36      * Adds a Response.
       
    37      *
       
    38      * @param Response $response
       
    39      */
       
    40     public function add(Response $response)
       
    41     {
       
    42         if ($response->isValidateable()) {
       
    43             $this->cacheable = false;
       
    44         } else {
       
    45             $this->ttls[] = $response->getTtl();
       
    46             $this->maxAges[] = $response->getMaxAge();
       
    47         }
       
    48     }
       
    49 
       
    50     /**
       
    51      * Updates the Response HTTP headers based on the embedded Responses.
       
    52      *
       
    53      * @param Response $response
       
    54      */
       
    55     public function update(Response $response)
       
    56     {
       
    57         // if we only have one Response, do nothing
       
    58         if (1 === count($this->ttls)) {
       
    59             return;
       
    60         }
       
    61 
       
    62         if (!$this->cacheable) {
       
    63             $response->headers->set('Cache-Control', 'no-cache, must-revalidate');
       
    64 
       
    65             return;
       
    66         }
       
    67 
       
    68         if (null !== $maxAge = min($this->maxAges)) {
       
    69             $response->setSharedMaxAge($maxAge);
       
    70             $response->headers->set('Age', $maxAge - min($this->ttls));
       
    71         }
       
    72         $response->setMaxAge(0);
       
    73     }
       
    74 }