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

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\FilterResponseEvent;
  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.     public function __construct(bool $etag falseint $maxAge nullint $sharedMaxAge null, array $vary nullbool $public nullResourceMetadataFactoryInterface $resourceMetadataFactory null)
  31.     {
  32.         $this->etag $etag;
  33.         $this->maxAge $maxAge;
  34.         $this->sharedMaxAge $sharedMaxAge;
  35.         $this->vary $vary;
  36.         $this->public $public;
  37.         $this->resourceMetadataFactory $resourceMetadataFactory;
  38.     }
  39.     public function onKernelResponse(FilterResponseEvent $event): void
  40.     {
  41.         $request $event->getRequest();
  42.         if (!$request->isMethodCacheable() || !RequestAttributesExtractor::extractAttributes($request)) {
  43.             return;
  44.         }
  45.         $response $event->getResponse();
  46.         if (!$response->getContent() || !$response->isSuccessful()) {
  47.             return;
  48.         }
  49.         $resourceCacheHeaders = [];
  50.         if ($this->resourceMetadataFactory && $attributes RequestAttributesExtractor::extractAttributes($request)) {
  51.             $resourceMetadata $this->resourceMetadataFactory->create($attributes['resource_class']);
  52.             $resourceCacheHeaders $resourceMetadata->getOperationAttribute($attributes'cache_headers', [], true);
  53.         }
  54.         if ($this->etag && !$response->getEtag()) {
  55.             $response->setEtag(md5((string) $response->getContent()));
  56.         }
  57.         if (null !== ($maxAge $resourceCacheHeaders['max_age'] ?? $this->maxAge) && !$response->headers->hasCacheControlDirective('max-age')) {
  58.             $response->setMaxAge($maxAge);
  59.         }
  60.         if (null !== $this->vary) {
  61.             $response->setVary(array_diff($this->vary$response->getVary()), false);
  62.         }
  63.         if (null !== ($sharedMaxAge $resourceCacheHeaders['shared_max_age'] ?? $this->sharedMaxAge) && !$response->headers->hasCacheControlDirective('s-maxage')) {
  64.             $response->setSharedMaxAge($sharedMaxAge);
  65.         }
  66.         if (null !== $this->public && !$response->headers->hasCacheControlDirective('public')) {
  67.             $this->public $response->setPublic() : $response->setPrivate();
  68.         }
  69.     }
  70. }