|
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
|
|
|
* @var ErrorCollection |
|
39
|
|
|
*/ |
|
40
|
|
|
private $errorCollection; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* AmpOptimizerSubscriber constructor. |
|
44
|
|
|
* @param LoggerInterface $logger |
|
45
|
|
|
* @param TransformationEngine $transformationEngine |
|
46
|
|
|
* @param $config |
|
47
|
|
|
*/ |
|
48
|
6 |
|
public function __construct(LoggerInterface $logger, TransformationEngine $transformationEngine, $config) |
|
49
|
|
|
{ |
|
50
|
6 |
|
$this->config = $config; |
|
51
|
6 |
|
$this->logger = $logger; |
|
52
|
6 |
|
$this->transformationEngine = $transformationEngine; |
|
53
|
6 |
|
$this->errorCollection = new ErrorCollection(); |
|
54
|
6 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return array|array[] |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public static function getSubscribedEvents() |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
62
|
1 |
|
KernelEvents::RESPONSE => ['onKernelResponse', -10], |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param ResponseEvent $event |
|
68
|
|
|
* @return void |
|
69
|
|
|
*/ |
|
70
|
6 |
|
public function onKernelResponse(ResponseEvent $event): void |
|
71
|
|
|
{ |
|
72
|
6 |
|
if (!array_key_exists('transform_enabled', $this->config) || |
|
73
|
6 |
|
false === $this->config['transform_enabled']) { |
|
74
|
2 |
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
4 |
|
if (!$event->isMasterRequest()) { |
|
78
|
1 |
|
return; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
if (!$this->isAmpHtml($event->getResponse())) { |
|
82
|
1 |
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
$optimizedHtml = $this->transformationEngine->optimizeHtml( |
|
86
|
2 |
|
$event->getResponse()->getContent(), |
|
87
|
2 |
|
$this->errorCollection |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
2 |
|
$this->handleErrors(); |
|
91
|
|
|
|
|
92
|
2 |
|
$event->getResponse()->setContent($optimizedHtml); |
|
93
|
2 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param Response $response |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
3 |
|
private function isAmpHtml(Response $response): bool |
|
100
|
|
|
{ |
|
101
|
3 |
|
$contentType = $response->headers->get('Content-type'); |
|
102
|
3 |
|
if (strpos($contentType, 'text/html') === false) { |
|
103
|
1 |
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
3 |
|
$content = $response->getContent(); |
|
107
|
3 |
|
$dom = HtmlDomParser::str_get_html($content); |
|
108
|
3 |
|
$htmlElementAttrs = $dom->find('html', 0)->getAllAttributes(); |
|
109
|
3 |
|
if (empty(array_intersect(['⚡', 'amp'], array_keys($htmlElementAttrs)))) { |
|
110
|
1 |
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
private function handleErrors(): void |
|
117
|
|
|
{ |
|
118
|
2 |
|
if ($this->errorCollection->count() > 0) { |
|
119
|
1 |
|
foreach ($this->errorCollection as $error) { |
|
120
|
1 |
|
$this->logger->error(sprintf( |
|
121
|
1 |
|
"AMP-Optimizer Error code: %s\nError Message: %s\n", |
|
122
|
1 |
|
$error->getCode(), |
|
123
|
1 |
|
$error->getMessage() |
|
124
|
|
|
)); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
2 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param bool $enabled |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function setEnabled(bool $enabled): void |
|
133
|
|
|
{ |
|
134
|
1 |
|
$this->config['transform_enabled'] = $enabled; |
|
135
|
|
|
} |
|
136
|
|
|
} |