You are here

class AmpHtmlResponseSubscriber in Accelerated Mobile Pages (AMP) 8

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/AmpHtmlResponseSubscriber.php \Drupal\amp\EventSubscriber\AmpHtmlResponseSubscriber

Response subscriber to handle amp HTML responses.

Hierarchy

  • class \Drupal\amp\EventSubscriber\AmpHtmlResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AmpHtmlResponseSubscriber

1 string reference to 'AmpHtmlResponseSubscriber'
amp.services.yml in ./amp.services.yml
amp.services.yml
1 service uses AmpHtmlResponseSubscriber
html_response.amp_subscriber in ./amp.services.yml
Drupal\amp\EventSubscriber\AmpHtmlResponseSubscriber

File

src/EventSubscriber/AmpHtmlResponseSubscriber.php, line 20
Contains \Drupal\amp\EventSubscriber\AmpHtmlResponseSubscriber.

Namespace

Drupal\amp\EventSubscriber
View source
class AmpHtmlResponseSubscriber implements EventSubscriberInterface {

  /**
   * The AMP HTML response markup processor service.
   *
   * @var \Drupal\amp\Render\AmpHtmlResponseMarkupProcessor
   */
  protected $ampHtmlResponseMarkupProcessor;

  /**
   * The route amp context to determine whether a route is an amp one.
   *
   * @var \Drupal\amp\Routing\AmpContext
   */
  protected $ampContext;

  /**
   * Constructs an AmpHtmlResponseSubscriber object.
   *
   * @param \Drupal\amp\Render\AmpHtmlResponseMarkupProcessor $amp_html_response_markup_processor
   *   The HTML response attachments processor service.
   */
  public function __construct(AmpHtmlResponseMarkupProcessor $amp_html_response_markup_processor, AmpContext $amp_context) {
    $this->ampHtmlResponseMarkupProcessor = $amp_html_response_markup_processor;
    $this->ampContext = $amp_context;
  }

  /**
   * Processes markup for HtmlResponse responses.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event to process.
   */
  public function onRespond(FilterResponseEvent $event) {
    $response = $event
      ->getResponse();
    if (!$response instanceof HtmlResponse) {
      return;
    }
    if ($this->ampContext
      ->isAmpRoute()) {
      $event
        ->setResponse($this->ampHtmlResponseMarkupProcessor
        ->processMarkupToAmp($response));
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // We want to run this as late as possible, after the HTML has been modified by all the Response listeners
    $events[KernelEvents::RESPONSE][] = [
      'onRespond',
      -1024,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AmpHtmlResponseSubscriber::$ampContext protected property The route amp context to determine whether a route is an amp one.
AmpHtmlResponseSubscriber::$ampHtmlResponseMarkupProcessor protected property The AMP HTML response markup processor service.
AmpHtmlResponseSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AmpHtmlResponseSubscriber::onRespond public function Processes markup for HtmlResponse responses.
AmpHtmlResponseSubscriber::__construct public function Constructs an AmpHtmlResponseSubscriber object.