vendor/sonata-project/block-bundle/src/Cache/HttpCacheHandler.php line 57

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Sonata Project package.
  5. *
  6. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Sonata\BlockBundle\Cache;
  12. use Sonata\BlockBundle\Block\BlockContextInterface;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  15. /**
  16. * @deprecated since sonata-project/block-bundle 4.11 and will be removed in 5.0.
  17. */
  18. final class HttpCacheHandler implements HttpCacheHandlerInterface
  19. {
  20. private ?int $currentTtl = null;
  21. public function alterResponse(Response $response): void
  22. {
  23. if (!$response->isCacheable()) {
  24. // the controller flags the response as private so we keep it private!
  25. return;
  26. }
  27. // no block has been rendered
  28. if (null === $this->currentTtl) {
  29. return;
  30. }
  31. // a block has a lower ttl that the current response, so we update the ttl to match
  32. // the one provided in the block
  33. if ($this->currentTtl < $response->getTtl()) {
  34. $response->setTtl($this->currentTtl);
  35. }
  36. }
  37. public function updateMetadata(Response $response, ?BlockContextInterface $blockContext = null): void
  38. {
  39. if (null === $this->currentTtl) {
  40. $this->currentTtl = $response->getTtl();
  41. }
  42. if ($response->isCacheable() && $response->getTtl() < $this->currentTtl) {
  43. $this->currentTtl = $response->getTtl();
  44. }
  45. }
  46. public function onKernelResponse(ResponseEvent $event): void
  47. {
  48. $this->alterResponse($event->getResponse());
  49. }
  50. }