-
Notifications
You must be signed in to change notification settings - Fork 3
/
ParamonovavSpressHtmlCompress.php
85 lines (66 loc) · 2.35 KB
/
ParamonovavSpressHtmlCompress.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
use Yosymfony\Spress\Core\Plugin\PluginInterface;
use Yosymfony\Spress\Core\Plugin\EventSubscriber;
use Yosymfony\Spress\Core\Plugin\Event\EnvironmentEvent;
use Yosymfony\Spress\Core\Plugin\Event\RenderEvent;
use Yosymfony\Spress\Core\IO\IOInterface;
class ParamonovavSpressHtmlCompress implements PluginInterface
{
/** @var IOInterface */
private $io;
private $html_compress_exclude_pattern = '/(.*)?\.(jpe?g|png|gif|ico|svg|psd|tiff|webm|mov|avi|mkv|mp4)$/i';
/** @var string[] */
private $html_compress_exclude = ['.htaccess','robots.txt','crossdomain.xml', 'sitemap.xml', 'rss.xml'];
/** @var bool */
private $htmlCompress = true;
/**
* @param EventSubscriber $subscriber
*/
public function initialize(EventSubscriber $subscriber)
{
$subscriber->addEventListener('spress.start', 'onStart');
$subscriber->addEventListener('spress.after_render_page', 'onAfterRenderPage');
}
/**
* @return string[]
*/
public function getMetas()
{
return [
'name' => 'paramonovav/spress-html-compress',
'description' => 'Compress/minify your HTML',
'author' => 'paramonovav',
'license' => 'MIT',
];
}
/**
* @param EnvironmentEvent $event
*/
public function onStart(EnvironmentEvent $event)
{
$this->io = $event->getIO();
$configValues = $event->getConfigValues();
$this->htmlCompress = !empty($configValues['html_compress']);
if(isset($configValues['html_compress_exclude']))
{
$this->html_compress_exclude = $configValues['html_compress_exclude'];
}
if(isset($configValues['html_compress_exclude_pattern']))
{
$this->html_compress_exclude_pattern = $configValues['html_compress_exclude_pattern'];
}
}
/**
* @param RenderEvent $event
*/
public function onAfterRenderPage(RenderEvent $event)
{
$id = $event->getId();
if (!$this->htmlCompress || in_array($id, $this-> html_compress_exclude) || preg_match($this->html_compress_exclude_pattern, $id))
{
return;
}
$this->io->write('Minify/Compress html: '.$event->getId());
$event->setContent( \WyriHaximus\HtmlCompress\Factory::construct()->compress( $event->getContent() ) );
}
}