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

Open in your IDE?
  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\State\Util\OperationRequestInitiatorTrait;
  14. use ApiPlatform\Symfony\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.  * @deprecated use \Symfony\EventListener\AddHeadersListener.php instead
  22.  */
  23. final class AddHeadersListener
  24. {
  25.     use OperationRequestInitiatorTrait;
  26.     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 null, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory null, private readonly ?int $staleWhileRevalidate null, private readonly ?int $staleIfError null)
  27.     {
  28.         $this->resourceMetadataCollectionFactory $resourceMetadataCollectionFactory;
  29.     }
  30.     public function onKernelResponse(ResponseEvent $event): void
  31.     {
  32.         $request $event->getRequest();
  33.         if (!$request->isMethodCacheable()) {
  34.             return;
  35.         }
  36.         $attributes RequestAttributesExtractor::extractAttributes($request);
  37.         if (\count($attributes) < 1) {
  38.             return;
  39.         }
  40.         $response $event->getResponse();
  41.         if (!$response->getContent() || !$response->isSuccessful()) {
  42.             return;
  43.         }
  44.         $operation $this->initializeOperation($request);
  45.         if ('api_platform.symfony.main_controller' === $operation?->getController()) {
  46.             return;
  47.         }
  48.         $resourceCacheHeaders $attributes['cache_headers'] ?? $operation?->getCacheHeaders() ?? [];
  49.         if ($this->etag && !$response->getEtag()) {
  50.             $response->setEtag(md5((string) $response->getContent()));
  51.         }
  52.         if (null !== ($maxAge $resourceCacheHeaders['max_age'] ?? $this->maxAge) && !$response->headers->hasCacheControlDirective('max-age')) {
  53.             $response->setMaxAge($maxAge);
  54.         }
  55.         $vary $resourceCacheHeaders['vary'] ?? $this->vary;
  56.         if (null !== $vary) {
  57.             $response->setVary(array_diff($vary$response->getVary()), false);
  58.         }
  59.         // if the public-property is defined and not yet set; apply it to the response
  60.         $public = ($resourceCacheHeaders['public'] ?? $this->public);
  61.         if (null !== $public && !$response->headers->hasCacheControlDirective('public')) {
  62.             $public $response->setPublic() : $response->setPrivate();
  63.         }
  64.         // Cache-Control "s-maxage" is only relevant is resource is not marked as "private"
  65.         if (false !== $public && null !== ($sharedMaxAge $resourceCacheHeaders['shared_max_age'] ?? $this->sharedMaxAge) && !$response->headers->hasCacheControlDirective('s-maxage')) {
  66.             $response->setSharedMaxAge($sharedMaxAge);
  67.         }
  68.         if (null !== ($staleWhileRevalidate $resourceCacheHeaders['stale_while_revalidate'] ?? $this->staleWhileRevalidate) && !$response->headers->hasCacheControlDirective('stale-while-revalidate')) {
  69.             $response->headers->addCacheControlDirective('stale-while-revalidate', (string) $staleWhileRevalidate);
  70.         }
  71.         if (null !== ($staleIfError $resourceCacheHeaders['stale_if_error'] ?? $this->staleIfError) && !$response->headers->hasCacheControlDirective('stale-if-error')) {
  72.             $response->headers->addCacheControlDirective('stale-if-error', (string) $staleIfError);
  73.         }
  74.     }
  75. }