You are here

class ResponseSubscriber 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

Respond to event processes.

Hierarchy

  • class \Drupal\advagg\EventSubscriber\ResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ResponseSubscriber

1 string reference to 'ResponseSubscriber'
advagg.services.yml in ./advagg.services.yml
advagg.services.yml
1 service uses ResponseSubscriber
advagg.suscriber in ./advagg.services.yml
Drupal\advagg\EventSubscriber\ResponseSubscriber

File

src/EventSubscriber/ResponseSubscriber.php, line 14

Namespace

Drupal\advagg\EventSubscriber
View source
class ResponseSubscriber implements EventSubscriberInterface {

  /**
   * A config object for the advagg configuration.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * Constructs the Subscriber object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   A config factory for retrieving required config objects.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->config = $config_factory
      ->get('advagg.settings');
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      KernelEvents::RESPONSE => [
        [
          'processResponse',
          -9999,
        ],
        [
          'forceAbsolutePaths',
          0,
        ],
      ],
    ];
  }

  /**
   * Passes HtmlResponse responses on to other functions if enabled.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event to process.
   */
  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()));
    }
  }

  /**
   * Force absolute paths.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $response
   *   The response event object.
   */
  public function forceAbsolutePaths(FilterResponseEvent $response) {

    // Skip if not enabled.
    if (!$this->config
      ->get('path.convert.absolute')) {
      return;
    }
    $response = $response
      ->getResponse();

    // Only process Html Responses.
    if (!$response instanceof HtmlResponse) {
      return;
    }
    $content = $response
      ->getContent();
    $pattern = '/(<script src="|url\\("|rel="stylesheet" href=")(\\/[a-zA-Z0-0].*")/';
    $response
      ->setContent(preg_replace_callback($pattern, [
      $this,
      'forceAbsolutePathsCallback',
    ], $content));
  }

  /**
   * Callback to replace individual stylesheet links.
   *
   * @param array $matches
   *   Array from matches from preg_replace_callback.
   *
   * @return string
   *   Updated html string.
   */
  public function forceAbsolutePathsCallback(array $matches) {
    global $base_root;
    return "{$matches[1]}{$base_root}{$matches[2]}";
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResponseSubscriber::$config protected property A config object for the advagg configuration.
ResponseSubscriber::forceAbsolutePaths public function Force absolute paths.
ResponseSubscriber::forceAbsolutePathsCallback public function Callback to replace individual stylesheet links.
ResponseSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ResponseSubscriber::processResponse public function Passes HtmlResponse responses on to other functions if enabled.
ResponseSubscriber::__construct public function Constructs the Subscriber object.