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 ;
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}
0 commit comments