123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace Symfony\Component\HttpKernel\HttpCache;
- use Symfony\Component\HttpFoundation\Response;
- class ResponseCacheStrategy implements ResponseCacheStrategyInterface
- {
-
- private const OVERRIDE_DIRECTIVES = ['private', 'no-cache', 'no-store', 'no-transform', 'must-revalidate', 'proxy-revalidate'];
-
- private const INHERIT_DIRECTIVES = ['public', 'immutable'];
- private $embeddedResponses = 0;
- private $isNotCacheableResponseEmbedded = false;
- private $age = 0;
- private $flagDirectives = [
- 'no-cache' => null,
- 'no-store' => null,
- 'no-transform' => null,
- 'must-revalidate' => null,
- 'proxy-revalidate' => null,
- 'public' => null,
- 'private' => null,
- 'immutable' => null,
- ];
- private $ageDirectives = [
- 'max-age' => null,
- 's-maxage' => null,
- 'expires' => null,
- ];
-
- public function add(Response $response)
- {
- ++$this->embeddedResponses;
- foreach (self::OVERRIDE_DIRECTIVES as $directive) {
- if ($response->headers->hasCacheControlDirective($directive)) {
- $this->flagDirectives[$directive] = true;
- }
- }
- foreach (self::INHERIT_DIRECTIVES as $directive) {
- if (false !== $this->flagDirectives[$directive]) {
- $this->flagDirectives[$directive] = $response->headers->hasCacheControlDirective($directive);
- }
- }
- $age = $response->getAge();
- $this->age = max($this->age, $age);
- if ($this->willMakeFinalResponseUncacheable($response)) {
- $this->isNotCacheableResponseEmbedded = true;
- return;
- }
- $isHeuristicallyCacheable = $response->headers->hasCacheControlDirective('public');
- $maxAge = $response->headers->hasCacheControlDirective('max-age') ? (int) $response->headers->getCacheControlDirective('max-age') : null;
- $this->storeRelativeAgeDirective('max-age', $maxAge, $age, $isHeuristicallyCacheable);
- $sharedMaxAge = $response->headers->hasCacheControlDirective('s-maxage') ? (int) $response->headers->getCacheControlDirective('s-maxage') : $maxAge;
- $this->storeRelativeAgeDirective('s-maxage', $sharedMaxAge, $age, $isHeuristicallyCacheable);
- $expires = $response->getExpires();
- $expires = null !== $expires ? (int) $expires->format('U') - (int) $response->getDate()->format('U') : null;
- $this->storeRelativeAgeDirective('expires', $expires >= 0 ? $expires : null, 0, $isHeuristicallyCacheable);
- }
-
- public function update(Response $response)
- {
-
- if (0 === $this->embeddedResponses) {
- return;
- }
-
-
-
- $response->setEtag(null);
- $response->setLastModified(null);
- $this->add($response);
- $response->headers->set('Age', $this->age);
- if ($this->isNotCacheableResponseEmbedded) {
- if ($this->flagDirectives['no-store']) {
- $response->headers->set('Cache-Control', 'no-cache, no-store, must-revalidate');
- } else {
- $response->headers->set('Cache-Control', 'no-cache, must-revalidate');
- }
- return;
- }
- $flags = array_filter($this->flagDirectives);
- if (isset($flags['must-revalidate'])) {
- $flags['no-cache'] = true;
- }
- $response->headers->set('Cache-Control', implode(', ', array_keys($flags)));
- $maxAge = null;
- if (is_numeric($this->ageDirectives['max-age'])) {
- $maxAge = $this->ageDirectives['max-age'] + $this->age;
- $response->headers->addCacheControlDirective('max-age', $maxAge);
- }
- if (is_numeric($this->ageDirectives['s-maxage'])) {
- $sMaxage = $this->ageDirectives['s-maxage'] + $this->age;
- if ($maxAge !== $sMaxage) {
- $response->headers->addCacheControlDirective('s-maxage', $sMaxage);
- }
- }
- if (is_numeric($this->ageDirectives['expires'])) {
- $date = clone $response->getDate();
- $date = $date->modify('+'.($this->ageDirectives['expires'] + $this->age).' seconds');
- $response->setExpires($date);
- }
- }
-
- private function willMakeFinalResponseUncacheable(Response $response): bool
- {
-
-
- if ($response->headers->hasCacheControlDirective('no-cache')
- || $response->headers->getCacheControlDirective('no-store')
- ) {
- return true;
- }
-
-
- if (\in_array($response->getStatusCode(), [200, 203, 300, 301, 410])
- && null === $response->getLastModified()
- && null === $response->getEtag()
- ) {
- return false;
- }
-
-
-
- $cacheControl = ['max-age', 's-maxage', 'must-revalidate', 'proxy-revalidate', 'public', 'private'];
- foreach ($cacheControl as $key) {
- if ($response->headers->hasCacheControlDirective($key)) {
- return false;
- }
- }
- if ($response->headers->has('Expires')) {
- return false;
- }
- return true;
- }
-
- private function storeRelativeAgeDirective(string $directive, ?int $value, int $age, bool $isHeuristicallyCacheable)
- {
- if (null === $value) {
- if ($isHeuristicallyCacheable) {
-
- return;
- }
- $this->ageDirectives[$directive] = false;
- }
- if (false !== $this->ageDirectives[$directive]) {
- $value -= $age;
- $this->ageDirectives[$directive] = null !== $this->ageDirectives[$directive] ? min($this->ageDirectives[$directive], $value) : $value;
- }
- }
- }
|