You are here

public function AmpHtmlResponseMarkupProcessor::processMarkupToAmp in Accelerated Mobile Pages (AMP) 8.2

Same name and namespace in other branches
  1. 8 src/Render/AmpHtmlResponseMarkupProcessor.php \Drupal\amp\Render\AmpHtmlResponseMarkupProcessor::processMarkupToAmp()

Processes the content of a response into amp html.

Parameters

\Drupal\Core\Render\HtmlResponse $response: The response to process.

Return value

\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.

File

src/Render/AmpHtmlResponseMarkupProcessor.php, line 89

Class

AmpHtmlResponseMarkupProcessor
Processes markup of HTML responses.

Namespace

Drupal\amp\Render

Code

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;
}