DenyNoCacheRoutes.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace Drupal\Core\PageCache\ResponsePolicy;
  3. use Drupal\Core\PageCache\ResponsePolicyInterface;
  4. use Drupal\Core\Routing\RouteMatchInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. /**
  8. * Cache policy for routes with the 'no_cache' option set.
  9. *
  10. * This policy rule denies caching of responses generated for routes that have
  11. * the 'no_cache' option set to TRUE.
  12. */
  13. class DenyNoCacheRoutes implements ResponsePolicyInterface {
  14. /**
  15. * The current route match.
  16. *
  17. * @var \Drupal\Core\Routing\RouteMatchInterface
  18. */
  19. protected $routeMatch;
  20. /**
  21. * Constructs a deny node preview page cache policy.
  22. *
  23. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  24. * The current route match.
  25. */
  26. public function __construct(RouteMatchInterface $route_match) {
  27. $this->routeMatch = $route_match;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function check(Response $response, Request $request) {
  33. if (($route = $this->routeMatch->getRouteObject()) && $route->getOption('no_cache')) {
  34. return static::DENY;
  35. }
  36. }
  37. }