Skip to content

Commit 7f5a8ad

Browse files
authored
Merge pull request #4 from shdev/master
compression can be disabled per configuration
2 parents 6326c3a + df89a4c commit 7f5a8ad

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

ParamonovavSpressHtmlCompress.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,32 @@
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;
1313

14+
/** @var string[] */
15+
private $html_compress_exclude = ['.htaccess','robots.txt','crossdomain.xml', 'sitemap.xml', 'rss.xml'];
16+
17+
/** @var bool */
18+
private $htmlCompress = true;
19+
20+
/**
21+
* @param EventSubscriber $subscriber
22+
*/
1423
public function initialize(EventSubscriber $subscriber)
1524
{
16-
$subscriber-> addEventListener('spress.start', 'onStart');
17-
$subscriber-> addEventListener('spress.after_render_page', 'onAfterRenderPage');
25+
$subscriber->addEventListener('spress.start', 'onStart');
26+
$subscriber->addEventListener('spress.after_render_page', 'onAfterRenderPage');
1827
}
1928

29+
/**
30+
* @return string[]
31+
*/
2032
public function getMetas()
2133
{
2234
return [
@@ -27,29 +39,37 @@ public function getMetas()
2739
];
2840
}
2941

42+
/**
43+
* @param EnvironmentEvent $event
44+
*/
3045
public function onStart(EnvironmentEvent $event)
3146
{
32-
$this-> io = $event-> getIO();
47+
$this->io = $event->getIO();
48+
49+
$configValues = $event->getConfigValues();
3350

34-
$configValues = $event-> getConfigValues();
51+
$this->htmlCompress = !empty($configValues['html_compress']);
3552

3653
if(isset($configValues['html_compress_exclude']))
3754
{
3855
$this->html_compress_exclude = $configValues['html_compress_exclude'];
3956
}
4057
}
41-
58+
59+
/**
60+
* @param RenderEvent $event
61+
*/
4262
public function onAfterRenderPage(RenderEvent $event)
4363
{
44-
$id = $event-> getId();
4564

46-
if (in_array($id, $this-> html_compress_exclude) || preg_match('/(.*)?\.(jpe?g|png|gif|ico|svg|psd|tiff|webm|mov|avi|mkv|mp4)$/i', $id))
47-
{
65+
$id = $event->getId();
66+
67+
if (!$this->htmlCompress || in_array($id, $this->html_compress_exclude, true) || preg_match('/(.*)?\.(jpe?g|png|gif|ico|svg|psd|tiff|webm|mov|avi|mkv|mp4)$/i', $id)) {
4868
return;
4969
}
5070

51-
$this-> io-> write('Minify/Compress html: '.$event-> getId());
71+
$this->io->write('Minify/Compress html: '.$event->getId());
5272

53-
$event-> setContent( \WyriHaximus\HtmlCompress\Factory::construct()-> compress( $event-> getContent() ) );
73+
$event->setContent( \WyriHaximus\HtmlCompress\Factory::construct()->compress( $event->getContent() ) );
5474
}
5575
}

README.md

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

0 commit comments

Comments
 (0)