cleaned up composer
This commit is contained in:
@@ -17,7 +17,7 @@ use Symfony\Component\HttpFoundation\Response;
|
||||
* ResponseCacheStrategy knows how to compute the Response cache HTTP header
|
||||
* based on the different response cache headers.
|
||||
*
|
||||
* This implementation changes the master response TTL to the smallest TTL received
|
||||
* This implementation changes the main response TTL to the smallest TTL received
|
||||
* or force validation if one of the surrogates has validation cache strategy.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
@@ -50,7 +50,7 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
|
||||
private $ageDirectives = [
|
||||
'max-age' => null,
|
||||
's-maxage' => null,
|
||||
'expires' => null,
|
||||
'expires' => false,
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -81,12 +81,30 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
|
||||
return;
|
||||
}
|
||||
|
||||
$this->storeRelativeAgeDirective('max-age', $response->headers->getCacheControlDirective('max-age'), $age);
|
||||
$this->storeRelativeAgeDirective('s-maxage', $response->headers->getCacheControlDirective('s-maxage') ?: $response->headers->getCacheControlDirective('max-age'), $age);
|
||||
|
||||
$maxAge = $response->headers->hasCacheControlDirective('max-age') ? (int) $response->headers->getCacheControlDirective('max-age') : null;
|
||||
$sharedMaxAge = $response->headers->hasCacheControlDirective('s-maxage') ? (int) $response->headers->getCacheControlDirective('s-maxage') : $maxAge;
|
||||
$expires = $response->getExpires();
|
||||
$expires = null !== $expires ? (int) $expires->format('U') - (int) $response->getDate()->format('U') : null;
|
||||
$this->storeRelativeAgeDirective('expires', $expires >= 0 ? $expires : null, 0);
|
||||
|
||||
// See https://datatracker.ietf.org/doc/html/rfc7234#section-4.2.2
|
||||
// If a response is "public" but does not have maximum lifetime, heuristics might be applied.
|
||||
// Do not store NULL values so the final response can have more limiting value from other responses.
|
||||
$isHeuristicallyCacheable = $response->headers->hasCacheControlDirective('public')
|
||||
&& null === $maxAge
|
||||
&& null === $sharedMaxAge
|
||||
&& null === $expires;
|
||||
|
||||
if (!$isHeuristicallyCacheable || null !== $maxAge || null !== $expires) {
|
||||
$this->storeRelativeAgeDirective('max-age', $maxAge, $expires, $age);
|
||||
}
|
||||
|
||||
if (!$isHeuristicallyCacheable || null !== $sharedMaxAge || null !== $expires) {
|
||||
$this->storeRelativeAgeDirective('s-maxage', $sharedMaxAge, $expires, $age);
|
||||
}
|
||||
|
||||
if (null !== $expires) {
|
||||
$this->ageDirectives['expires'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,7 +117,7 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove validation related headers of the master response,
|
||||
// Remove validation related headers of the final response,
|
||||
// because some of the response content comes from at least
|
||||
// one embedded response (which likely has a different caching strategy).
|
||||
$response->setEtag(null);
|
||||
@@ -142,9 +160,9 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
|
||||
}
|
||||
}
|
||||
|
||||
if (is_numeric($this->ageDirectives['expires'])) {
|
||||
if ($this->ageDirectives['expires'] && null !== $maxAge) {
|
||||
$date = clone $response->getDate();
|
||||
$date->modify('+'.($this->ageDirectives['expires'] + $this->age).' seconds');
|
||||
$date = $date->modify('+'.$maxAge.' seconds');
|
||||
$response->setExpires($date);
|
||||
}
|
||||
}
|
||||
@@ -199,13 +217,14 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
|
||||
* If the value is lower than the currently stored value, we update the value, to keep a rolling
|
||||
* minimal value of each instruction. If the value is NULL, the directive will not be set on the final response.
|
||||
*/
|
||||
private function storeRelativeAgeDirective(string $directive, ?int $value, int $age)
|
||||
private function storeRelativeAgeDirective(string $directive, ?int $value, ?int $expires, int $age): void
|
||||
{
|
||||
if (null === $value) {
|
||||
if (null === $value && null === $expires) {
|
||||
$this->ageDirectives[$directive] = false;
|
||||
}
|
||||
|
||||
if (false !== $this->ageDirectives[$directive]) {
|
||||
$value = min($value ?? PHP_INT_MAX, $expires ?? PHP_INT_MAX);
|
||||
$value -= $age;
|
||||
$this->ageDirectives[$directive] = null !== $this->ageDirectives[$directive] ? min($this->ageDirectives[$directive], $value) : $value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user