vendor/api-platform/core/src/HttpCache/EventListener/AddHeadersListener.php line 35

  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.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. declare(strict_types=1);
  11. namespace ApiPlatform\HttpCache\EventListener;
  12. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  13. use ApiPlatform\Util\OperationRequestInitiatorTrait;
  14. use ApiPlatform\Util\RequestAttributesExtractor;
  15. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  16. /**
  17.  * Configures cache HTTP headers for the current response.
  18.  *
  19.  * @author Kévin Dunglas <dunglas@gmail.com>
  20.  */
  21. final class AddHeadersListener
  22. {
  23.     use OperationRequestInitiatorTrait;
  24.     public function __construct(private readonly bool $etag false, private readonly ?int $maxAge null, private readonly ?int $sharedMaxAge null, private readonly ?array $vary null, private readonly ?bool $public nullResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory null, private readonly ?int $staleWhileRevalidate null, private readonly ?int $staleIfError null)
  25.     {
  26.         $this->resourceMetadataCollectionFactory $resourceMetadataCollectionFactory;
  27.     }
  28.     public function onKernelResponse(ResponseEvent $event): void
  29.     {
  30.         $request $event->getRequest();
  31.         if (!$request->isMethodCacheable()) {
  32.             return;
  33.         }
  34.         $attributes RequestAttributesExtractor::extractAttributes($request);
  35.         if (\count($attributes) < 1) {
  36.             return;
  37.         }
  38.         $response $event->getResponse();
  39.         if (!$response->getContent() || !$response->isSuccessful()) {
  40.             return;
  41.         }
  42.         $operation $this->initializeOperation($request);
  43.         $resourceCacheHeaders $attributes['cache_headers'] ?? $operation?->getCacheHeaders() ?? [];
  44.         if ($this->etag && !$response->getEtag()) {
  45.             $response->setEtag(md5((string) $response->getContent()));
  46.         }
  47.         if (null !== ($maxAge $resourceCacheHeaders['max_age'] ?? $this->maxAge) && !$response->headers->hasCacheControlDirective('max-age')) {
  48.             $response->setMaxAge($maxAge);
  49.         }
  50.         $vary $resourceCacheHeaders['vary'] ?? $this->vary;
  51.         if (null !== $vary) {
  52.             $response->setVary(array_diff($vary$response->getVary()), false);
  53.         }
  54.         // if the public-property is defined and not yet set; apply it to the response
  55.         $public = ($resourceCacheHeaders['public'] ?? $this->public);
  56.         if (null !== $public && !$response->headers->hasCacheControlDirective('public')) {
  57.             $public $response->setPublic() : $response->setPrivate();
  58.         }
  59.         // Cache-Control "s-maxage" is only relevant is resource is not marked as "private"
  60.         if (false !== $public && null !== ($sharedMaxAge $resourceCacheHeaders['shared_max_age'] ?? $this->sharedMaxAge) && !$response->headers->hasCacheControlDirective('s-maxage')) {
  61.             $response->setSharedMaxAge($sharedMaxAge);
  62.         }
  63.         if (null !== ($staleWhileRevalidate $resourceCacheHeaders['stale_while_revalidate'] ?? $this->staleWhileRevalidate) && !$response->headers->hasCacheControlDirective('stale-while-revalidate')) {
  64.             $response->headers->addCacheControlDirective('stale-while-revalidate', (string) $staleWhileRevalidate);
  65.         }
  66.         if (null !== ($staleIfError $resourceCacheHeaders['stale_if_error'] ?? $this->staleIfError) && !$response->headers->hasCacheControlDirective('stale-if-error')) {
  67.             $response->headers->addCacheControlDirective('stale-if-error', (string) $staleIfError);
  68.         }
  69.     }
  70. }