1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hola\AmpToolboxBundle\EventSubscriber; |
4
|
|
|
|
5
|
|
|
use AmpProject\Optimizer\ErrorCollection; |
6
|
|
|
use AmpProject\Optimizer\TransformationEngine; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Sunra\PhpSimple\HtmlDomParser; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent; |
12
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Listen response event and optimize html |
16
|
|
|
* Class AmpOptimizerSubscriber |
17
|
|
|
* @package Hola\AmpToolboxBundle\EventSubscriber |
18
|
|
|
*/ |
19
|
|
|
class AmpOptimizerSubscriber implements EventSubscriberInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var LoggerInterface |
24
|
|
|
*/ |
25
|
|
|
private $logger; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var TransformationEngine |
29
|
|
|
*/ |
30
|
|
|
private $transformationEngine; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* AmpOptimizerSubscriber constructor. |
39
|
|
|
* @param LoggerInterface $logger |
40
|
|
|
* @param TransformationEngine $transformationEngine |
41
|
|
|
* @param $config |
42
|
|
|
*/ |
43
|
3 |
|
public function __construct(LoggerInterface $logger, TransformationEngine $transformationEngine, $config) |
44
|
|
|
{ |
45
|
3 |
|
$this->config = $config; |
46
|
3 |
|
$this->logger = $logger; |
47
|
3 |
|
$this->transformationEngine = $transformationEngine; |
48
|
3 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array|array[] |
52
|
|
|
*/ |
53
|
1 |
|
public static function getSubscribedEvents() |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
1 |
|
KernelEvents::RESPONSE => ['onKernelResponse', -10], |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param ResponseEvent $event |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
3 |
|
public function onKernelResponse(ResponseEvent $event): void |
65
|
|
|
{ |
66
|
3 |
|
if (!$event->isMasterRequest()) { |
67
|
1 |
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
if (!$this->isAmpHtml($event->getResponse())) { |
71
|
1 |
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
$errorCollection = new ErrorCollection(); |
75
|
|
|
|
76
|
1 |
|
$optimizedHtml = $this->transformationEngine->optimizeHtml( |
77
|
1 |
|
$event->getResponse()->getContent(), |
78
|
1 |
|
$errorCollection |
79
|
|
|
); |
80
|
|
|
|
81
|
1 |
|
$event->getResponse()->setContent($optimizedHtml); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Response $response |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
2 |
|
private function isAmpHtml(Response $response): bool |
89
|
|
|
{ |
90
|
2 |
|
$contentType = $response->headers->get('Content-type'); |
91
|
2 |
|
if (strpos($contentType, 'text/html') === false) { |
92
|
1 |
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
$content = $response->getContent(); |
96
|
2 |
|
$dom = HtmlDomParser::str_get_html($content); |
97
|
2 |
|
$htmlElementAttrs = $dom->find('html', 0)->getAllAttributes(); |
98
|
2 |
|
if (empty(array_intersect(['⚡', 'amp'], array_keys($htmlElementAttrs)))) { |
99
|
1 |
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return true; |
103
|
|
|
} |
104
|
|
|
} |