You are here

public function BoostResponseSubscriber::onRequest in Boost 8

Process a GetResponseEvent instance.

File

src/EventSubscriber/BoostResponseSubscriber.php, line 69
Contains Drupal\boost\EventSubscriber\BoostResponseSubscriber

Class

BoostResponseSubscriber
Class BoostResponseSubscriber.

Namespace

Drupal\boost\EventSubscriber

Code

public function onRequest(GetResponseEvent $event) {
  if ($this->account
    ->isAuthenticated()) {
    return;
  }

  // Invalidate on POST to rebuild page response.
  $method = $event
    ->getRequest()
    ->getMethod();
  if ($method == 'POST') {
    $this->boostCache
      ->delete();
  }

  // Only cache GET requests.
  if ($method != 'GET') {
    return;
  }

  // Check for existing cached response.
  if ($content = $this->boostCache
    ->retrieve()) {
    $response = new Response();
    $response
      ->setContent($content);
    $response
      ->setStatusCode(Response::HTTP_OK);
    $response->headers
      ->set('X-Boost-Cache', 'partial');
    $event
      ->setResponse($response);
  }
}