You are here

public function ResponseSubscriber::processResponse in Advanced CSS/JS Aggregation 8.3

Same name and namespace in other branches
  1. 8.4 src/EventSubscriber/ResponseSubscriber.php \Drupal\advagg\EventSubscriber\ResponseSubscriber::processResponse()

Passes HtmlResponse responses on to other functions if enabled.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event: The event to process.

File

src/EventSubscriber/ResponseSubscriber.php, line 51

Class

ResponseSubscriber
Respond to event processes.

Namespace

Drupal\advagg\EventSubscriber

Code

public function processResponse(FilterResponseEvent $event) {

  // Only subscribe to the event if DNS prefetching is enabled.
  if ($this->config
    ->get('dns_prefetch')) {
    $response = $event
      ->getResponse();

    // Ensure that it is an html response.
    if (stripos($response->headers
      ->get('Content-Type'), 'text/html') === FALSE) {
      return;
    }
    global $_advagg_prefetch;
    if (empty($_advagg_prefetch)) {
      return;
    }
    $_advagg_prefetch = array_unique($_advagg_prefetch);
    $domains = '<head>';
    foreach ($_advagg_prefetch as $domain) {
      $domains .= "<link rel='dns-prefetch' href='{$domain}'>";
    }
    $response
      ->setContent(str_replace('<head>', $domains, $response
      ->getContent()));
  }
}