You are here

class AmpHtmlResponseMarkupProcessor in Accelerated Mobile Pages (AMP) 8

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

Processes markup of HTML responses.

Hierarchy

Expanded class hierarchy of AmpHtmlResponseMarkupProcessor

1 file declares its use of AmpHtmlResponseMarkupProcessor
AmpHtmlResponseSubscriber.php in src/EventSubscriber/AmpHtmlResponseSubscriber.php
Contains \Drupal\amp\EventSubscriber\AmpHtmlResponseSubscriber.
1 string reference to 'AmpHtmlResponseMarkupProcessor'
amp.services.yml in ./amp.services.yml
amp.services.yml
1 service uses AmpHtmlResponseMarkupProcessor
html_response.amp_markup_processor in ./amp.services.yml
Drupal\amp\Render\AmpHtmlResponseMarkupProcessor

File

src/Render/AmpHtmlResponseMarkupProcessor.php, line 20
Contains \Drupal\amp\Render\AmpHtmlResponseMarkupProcessor.

Namespace

Drupal\amp\Render
View source
class AmpHtmlResponseMarkupProcessor {

  /**
   * The original content.
   *
   * @var string
   */
  protected $content;

  /**
   * The AMP-processed content.
   *
   * @var string
   */
  protected $ampContent;

  /**
   * The AMP library service.
   *
   * @var AMPService
   */
  protected $ampLibraryService;

  /**
   * The AMP library converter.
   *
   * @var \Lullabot\AMP\AMP
   */
  protected $ampConverter;

  /**
   * @var LoggerInterface
   */
  protected $logger;

  /**
   * @var ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $ampConfig;

  /**
   * Constructs an AmpHtmlResponseMarkupProcessor object.
   *
   * @param AMPService $amp_library_service
   *   An amp library service.
   *
   */
  public function __construct(AMPService $amp_library_service, LoggerInterface $logger, ConfigFactoryInterface $configFactoryInterface) {
    $this->ampService = $amp_library_service;
    $this->logger = $logger;
    $this->configFactory = $configFactoryInterface;
    $this->ampConfig = $this->configFactory
      ->get('amp.settings');
  }

  /**
   * Processes the content of a response into amp html.
   *
   * @param \Drupal\Core\Render\HtmlResponse $response
   *   The response to process.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The processed response, with the content updated to amp markup.
   *
   * @throws \InvalidArgumentException
   *   Thrown when the $response parameter is not the type of response object
   *   the processor expects.
   */
  public function processMarkupToAmp(HtmlResponse $response) {
    if (!$response instanceof HtmlResponse) {
      throw new \InvalidArgumentException('\\Drupal\\Core\\Render\\HtmlResponse instance expected.');
    }

    // Get a reference to the content.
    $this->content = $response
      ->getContent();

    // First check the config if full html warnings are on, if not then exit with unaltered response
    if (!$this->ampConfig
      ->get('amp_library_process_full_html')) {
      return $response;
    }
    $options = [
      'scope' => Scope::HTML_SCOPE,
    ];
    if ($this->ampConfig
      ->get('amp_library_process_statistics')) {
      $options += [
        'add_stats_html_comment' => true,
      ];
    }
    $this->ampConverter = $this->ampService
      ->createAMPConverter();
    $this->ampConverter
      ->loadHtml($this->content, $options);
    $this->ampContent = $this->ampConverter
      ->convertToAmpHtml();
    $request_uri = \Drupal::request()
      ->getRequestUri();
    $heading = "<h3>AMP PHP Library messages for {$request_uri}</h3>" . PHP_EOL;
    $heading .= 'To disable these notices goto <a href="/admin/config/content/amp">AMP configuration</a> and uncheck ' . '<em>Debugging: Add a notice in the Drupal log ...</em> in the AMP PHP Library Configuration fieldset' . PHP_EOL;
    if ($this->ampConfig
      ->get('amp_library_process_full_html_warnings')) {

      // Add any warnings that were generated
      $this->logger
        ->notice("{$heading} <pre>" . $this->ampConverter
        ->warningsHumanHtml() . '</pre>');
    }

    // Return the processed content.
    $response
      ->setContent($this->ampContent);
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AmpHtmlResponseMarkupProcessor::$ampConfig protected property
AmpHtmlResponseMarkupProcessor::$ampContent protected property The AMP-processed content.
AmpHtmlResponseMarkupProcessor::$ampConverter protected property The AMP library converter.
AmpHtmlResponseMarkupProcessor::$ampLibraryService protected property The AMP library service.
AmpHtmlResponseMarkupProcessor::$configFactory protected property
AmpHtmlResponseMarkupProcessor::$content protected property The original content.
AmpHtmlResponseMarkupProcessor::$logger protected property
AmpHtmlResponseMarkupProcessor::processMarkupToAmp public function Processes the content of a response into amp html.
AmpHtmlResponseMarkupProcessor::__construct public function Constructs an AmpHtmlResponseMarkupProcessor object.