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

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\Core\HttpCache\EventListener;
  12. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  13. use ApiPlatform\Core\Util\RequestAttributesExtractor;
  14. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  15. /**
  16.  * Configures cache HTTP headers for the current response.
  17.  *
  18.  * @author Kévin Dunglas <dunglas@gmail.com>
  19.  *
  20.  * @experimental
  21.  */
  22. final class AddHeadersListener
  23. {
  24.     private $etag;
  25.     private $maxAge;
  26.     private $sharedMaxAge;
  27.     private $vary;
  28.     private $public;
  29.     private $resourceMetadataFactory;
  30.     private $staleWhileRevalidate;
  31.     private $staleIfError;
  32.     public function __construct(bool $etag falseint $maxAge nullint $sharedMaxAge null, array $vary nullbool $public nullResourceMetadataFactoryInterface $resourceMetadataFactory nullint $staleWhileRevalidate nullint $staleIfError null)
  33.     {
  34.         $this->etag $etag;
  35.         $this->maxAge $maxAge;
  36.         $this->sharedMaxAge $sharedMaxAge;
  37.         $this->vary $vary;
  38.         $this->public $public;
  39.         $this->resourceMetadataFactory $resourceMetadataFactory;
  40.         $this->staleWhileRevalidate $staleWhileRevalidate;
  41.         $this->staleIfError $staleIfError;
  42.     }
  43.     public function onKernelResponse(ResponseEvent $event): void
  44.     {
  45.         $request $event->getRequest();
  46.         if (!$request->isMethodCacheable()) {
  47.             return;
  48.         }
  49.         $attributes RequestAttributesExtractor::extractAttributes($request);
  50.         if (\count($attributes) < 1) {
  51.             return;
  52.         }
  53.         $response $event->getResponse();
  54.         if (!$response->getContent() || !$response->isSuccessful()) {
  55.             return;
  56.         }
  57.         $resourceCacheHeaders = [];
  58.         if ($this->resourceMetadataFactory) {
  59.             $resourceMetadata $this->resourceMetadataFactory->create($attributes['resource_class']);
  60.             $resourceCacheHeaders $resourceMetadata->getOperationAttribute($attributes'cache_headers', [], true);
  61.         }
  62.         if ($this->etag && !$response->getEtag()) {
  63.             $response->setEtag(md5((string) $response->getContent()));
  64.         }
  65.         if (null !== ($maxAge $resourceCacheHeaders['max_age'] ?? $this->maxAge) && !$response->headers->hasCacheControlDirective('max-age')) {
  66.             $response->setMaxAge($maxAge);
  67.         }
  68.         $vary $resourceCacheHeaders['vary'] ?? $this->vary;
  69.         if (null !== $vary) {
  70.             $response->setVary(array_diff($vary$response->getVary()), false);
  71.         }
  72.         // if the public-property is defined and not yet set; apply it to the response
  73.         $public = ($resourceCacheHeaders['public'] ?? $this->public);
  74.         if (null !== $public && !$response->headers->hasCacheControlDirective('public')) {
  75.             $public $response->setPublic() : $response->setPrivate();
  76.         }
  77.         // Cache-Control "s-maxage" is only relevant is resource is not marked as "private"
  78.         if (false !== $public && null !== ($sharedMaxAge $resourceCacheHeaders['shared_max_age'] ?? $this->sharedMaxAge) && !$response->headers->hasCacheControlDirective('s-maxage')) {
  79.             $response->setSharedMaxAge($sharedMaxAge);
  80.         }
  81.         if (null !== ($staleWhileRevalidate $resourceCacheHeaders['stale_while_revalidate'] ?? $this->staleWhileRevalidate) && !$response->headers->hasCacheControlDirective('stale-while-revalidate')) {
  82.             $response->headers->addCacheControlDirective('stale-while-revalidate', (string) $staleWhileRevalidate);
  83.         }
  84.         if (null !== ($staleIfError $resourceCacheHeaders['stale_if_error'] ?? $this->staleIfError) && !$response->headers->hasCacheControlDirective('stale-if-error')) {
  85.             $response->headers->addCacheControlDirective('stale-if-error', (string) $staleIfError);
  86.         }
  87.     }
  88. }