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
|
4 |
|
public function __construct(LoggerInterface $logger, TransformationEngine $transformationEngine, $config) |
49
|
|
|
{ |
50
|
4 |
|
$this->config = $config; |
51
|
4 |
|
$this->logger = $logger; |
52
|
4 |
|
$this->transformationEngine = $transformationEngine; |
53
|
4 |
|
$this->errorCollection = new ErrorCollection(); |
54
|
4 |
|
} |
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
|
4 |
|
public function onKernelResponse(ResponseEvent $event): void |
71
|
|
|
{ |
72
|
4 |
|
if (!array_key_exists('transform_enabled', $this->config) || |
73
|
4 |
|
false === $this->config['transform_enabled']) { |
74
|
1 |
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
if (!$event->isMasterRequest()) { |
78
|
1 |
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
if (!$this->isAmpHtml($event->getResponse())) { |
82
|
1 |
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
$optimizedHtml = $this->transformationEngine->optimizeHtml( |
86
|
1 |
|
$event->getResponse()->getContent(), |
87
|
1 |
|
$this->errorCollection |
88
|
|
|
); |
89
|
|
|
|
90
|
1 |
|
$this->handleErrors(); |
91
|
|
|
|
92
|
1 |
|
$event->getResponse()->setContent($optimizedHtml); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Response $response |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
2 |
|
private function isAmpHtml(Response $response): bool |
100
|
|
|
{ |
101
|
2 |
|
$contentType = $response->headers->get('Content-type'); |
102
|
2 |
|
if (strpos($contentType, 'text/html') === false) { |
103
|
1 |
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
$content = $response->getContent(); |
107
|
2 |
|
$dom = HtmlDomParser::str_get_html($content); |
108
|
2 |
|
$htmlElementAttrs = $dom->find('html', 0)->getAllAttributes(); |
109
|
2 |
|
if (empty(array_intersect(['⚡', 'amp'], array_keys($htmlElementAttrs)))) { |
110
|
1 |
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
private function handleErrors(): void |
117
|
|
|
{ |
118
|
1 |
|
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
|
|
|
} |
128
|
|
|
} |