Skip to content

Commit 0201b81

Browse files
authored
Merge branch 'master' into master
2 parents a271c30 + 7f5a8ad commit 0201b81

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

ParamonovavSpressHtmlCompress.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,36 @@
33
use Yosymfony\Spress\Core\Plugin\PluginInterface;
44
use Yosymfony\Spress\Core\Plugin\EventSubscriber;
55
use Yosymfony\Spress\Core\Plugin\Event\EnvironmentEvent;
6-
use Yosymfony\Spress\Core\Plugin\Event\ContentEvent;
7-
use Yosymfony\Spress\Core\Plugin\Event\FinishEvent;
86
use Yosymfony\Spress\Core\Plugin\Event\RenderEvent;
7+
use Yosymfony\Spress\Core\IO\IOInterface;
98

109
class ParamonovavSpressHtmlCompress implements PluginInterface
1110
{
12-
private $io, $html_compress_exclude = ['.htaccess','robots.txt','crossdomain.xml', 'sitemap.xml', 'rss.xml'];
11+
/** @var IOInterface */
12+
private $io;
13+
1314

1415
private $html_compress_exclude_pattern = '/(.*)?\.(jpe?g|png|gif|ico|svg|psd|tiff|webm|mov|avi|mkv|mp4)$/i';
1516

17+
/** @var string[] */
18+
private $html_compress_exclude = ['.htaccess','robots.txt','crossdomain.xml', 'sitemap.xml', 'rss.xml'];
19+
20+
/** @var bool */
21+
private $htmlCompress = true;
22+
23+
/**
24+
* @param EventSubscriber $subscriber
25+
*/
26+
1627
public function initialize(EventSubscriber $subscriber)
1728
{
18-
$subscriber-> addEventListener('spress.start', 'onStart');
19-
$subscriber-> addEventListener('spress.after_render_page', 'onAfterRenderPage');
29+
$subscriber->addEventListener('spress.start', 'onStart');
30+
$subscriber->addEventListener('spress.after_render_page', 'onAfterRenderPage');
2031
}
2132

33+
/**
34+
* @return string[]
35+
*/
2236
public function getMetas()
2337
{
2438
return [
@@ -29,11 +43,16 @@ public function getMetas()
2943
];
3044
}
3145

46+
/**
47+
* @param EnvironmentEvent $event
48+
*/
3249
public function onStart(EnvironmentEvent $event)
3350
{
34-
$this-> io = $event-> getIO();
51+
$this->io = $event->getIO();
52+
53+
$configValues = $event->getConfigValues();
3554

36-
$configValues = $event-> getConfigValues();
55+
$this->htmlCompress = !empty($configValues['html_compress']);
3756

3857
if(isset($configValues['html_compress_exclude']))
3958
{
@@ -46,18 +65,21 @@ public function onStart(EnvironmentEvent $event)
4665
}
4766

4867
}
49-
68+
69+
/**
70+
* @param RenderEvent $event
71+
*/
5072
public function onAfterRenderPage(RenderEvent $event)
5173
{
52-
$id = $event-> getId();
74+
$id = $event->getId();
5375

54-
if (in_array($id, $this-> html_compress_exclude) || preg_match($this->html_compress_exclude_pattern, $id))
76+
if (!$this->htmlCompress || in_array($id, $this-> html_compress_exclude) || preg_match($this->html_compress_exclude_pattern, $id))
5577
{
5678
return;
5779
}
5880

59-
$this-> io-> write('Minify/Compress html: '.$event-> getId());
81+
$this->io->write('Minify/Compress html: '.$event->getId());
6082

61-
$event-> setContent( \WyriHaximus\HtmlCompress\Factory::construct()-> compress( $event-> getContent() ) );
83+
$event->setContent( \WyriHaximus\HtmlCompress\Factory::construct()->compress( $event->getContent() ) );
6284
}
6385
}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ Just run build command:
2929
```bash
3030
spress site:build
3131
```
32+
33+
If you want to disable compression use the following config
34+
35+
```yaml
36+
html_compress: false
37+
```

0 commit comments

Comments
 (0)