33use Yosymfony \Spress \Core \Plugin \PluginInterface ;
44use Yosymfony \Spress \Core \Plugin \EventSubscriber ;
55use Yosymfony \Spress \Core \Plugin \Event \EnvironmentEvent ;
6- use Yosymfony \Spress \Core \Plugin \Event \ContentEvent ;
7- use Yosymfony \Spress \Core \Plugin \Event \FinishEvent ;
86use Yosymfony \Spress \Core \Plugin \Event \RenderEvent ;
7+ use Yosymfony \Spress \Core \IO \IOInterface ;
98
109class 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}
0 commit comments