AmpHtmlResponseMarkupProcessor.php in Accelerated Mobile Pages (AMP) 8
File
src/Render/AmpHtmlResponseMarkupProcessor.php
View source
<?php
namespace Drupal\amp\Render;
use Drupal\amp\Service\AMPService;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Render\HtmlResponse;
use Lullabot\AMP\Validate\Scope;
use Psr\Log\LoggerInterface;
class AmpHtmlResponseMarkupProcessor {
protected $content;
protected $ampContent;
protected $ampLibraryService;
protected $ampConverter;
protected $logger;
protected $configFactory;
protected $ampConfig;
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');
}
public function processMarkupToAmp(HtmlResponse $response) {
if (!$response instanceof HtmlResponse) {
throw new \InvalidArgumentException('\\Drupal\\Core\\Render\\HtmlResponse instance expected.');
}
$this->content = $response
->getContent();
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')) {
$this->logger
->notice("{$heading} <pre>" . $this->ampConverter
->warningsHumanHtml() . '</pre>');
}
$response
->setContent($this->ampContent);
return $response;
}
}