Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit a92bc1e

Browse files
committed
add code
1 parent 273ff55 commit a92bc1e

File tree

6 files changed

+176
-0
lines changed

6 files changed

+176
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

DependencyInjection/Configuration.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace MatTheCat\HtmlCompressorBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
use Symfony\Component\Process\ExecutableFinder;
8+
9+
/**
10+
* This is the class that validates and merges configuration from your app/config files
11+
*
12+
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
13+
*/
14+
class Configuration implements ConfigurationInterface
15+
{
16+
/**
17+
* {@inheritDoc}
18+
*/
19+
public function getConfigTreeBuilder()
20+
{
21+
$treeBuilder = new TreeBuilder();
22+
$finder = new ExecutableFinder();
23+
24+
$treeBuilder->root('html_compressor')
25+
->children()
26+
->booleanNode('enabled')->defaultTrue()->end()
27+
->scalarNode('java')
28+
->defaultValue(
29+
function() use($finder) {
30+
return $finder->find('java', '/usr/bin/java');
31+
})
32+
->end()
33+
->scalarNode('jar')
34+
->isRequired()
35+
->cannotBeEmpty()
36+
->end()
37+
->arrayNode('options')
38+
->normalizeKeys(false)
39+
->prototype('scalar')
40+
->end()
41+
->end();
42+
43+
return $treeBuilder;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace MatTheCat\HtmlCompressorBundle\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\Config\FileLocator;
7+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8+
use Symfony\Component\DependencyInjection\Loader;
9+
10+
/**
11+
* This is the class that loads and manages your bundle configuration
12+
*
13+
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
14+
*/
15+
class MatTheCatHtmlCompressorExtension extends Extension
16+
{
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
public function load(array $configs, ContainerBuilder $container)
21+
{
22+
$configuration = new Configuration();
23+
$config = $this->processConfiguration($configuration, $configs);
24+
25+
$container->setParameter('html_compressor.java.bin', $config['java']);
26+
$container->setParameter('html_compressor.htmlcompressor.bin', $config['jar']);
27+
$container->setParameter('html_compressor.htmlcompressor.options', $config['options']);
28+
29+
if($config['enabled']) {
30+
$loader = new Loader\YamlFileLoader(
31+
$container,
32+
new FileLocator(__DIR__.'/../Resources/config')
33+
);
34+
$loader->load('services.yml');
35+
}
36+
}
37+
38+
public function getAlias()
39+
{
40+
return 'html_compressor';
41+
}
42+
}

EventListener/ResponseListener.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace MatTheCat\HtmlCompressorBundle\EventListener;
4+
5+
use Symfony\Component\HttpKernel\HttpKernelInterface;
6+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
7+
use Symfony\Component\Process\ProcessBuilder;
8+
9+
class ResponseListener
10+
{
11+
protected $javaPath;
12+
protected $htmlCompressorPath;
13+
protected $htmlCompressorOptions;
14+
15+
public function __construct($javaPath, $htmlCompressorPath, array $htmlCompressorOptions)
16+
{
17+
$this->javaPath = $javaPath;
18+
$this->htmlCompressorPath = $htmlCompressorPath;
19+
$this->htmlCompressorOptions = $htmlCompressorOptions;
20+
}
21+
22+
public function onKernelResponse(FilterResponseEvent $event) {
23+
$response = $event->getResponse();
24+
$contentType = $response->headers->get('Content-type');
25+
if(
26+
!$response->isCacheable() ||
27+
in_array(
28+
substr($contentType, strrpos($contentType, '/')+1),
29+
array('xml', 'html')
30+
)
31+
) {
32+
return;
33+
}
34+
$pb = new ProcessBuilder(
35+
array(
36+
$this->javaPath,
37+
'-jar',
38+
$this->htmlCompressorPath
39+
)
40+
);
41+
foreach($this->htmlCompressorOptions as $option => $value) {
42+
if(!is_null($option)) {
43+
$pb->add($option);
44+
45+
if(!is_null($value)) {
46+
$pb->add($value);
47+
}
48+
}
49+
}
50+
$pb->add($input = tempnam(sys_get_temp_dir(), 'html_compressor'));
51+
file_put_contents($input, $response->getContent());
52+
$proc = $pb->getProcess();
53+
$code = $proc->run();
54+
unlink($input);
55+
56+
if (!$code) {
57+
$response->setContent($proc->getOutput());
58+
}
59+
}
60+
}

MatTheCatHtmlCompressorBundle.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace MatTheCat\HtmlCompressorBundle;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
use MatTheCat\HtmlCompressorBundle\DependencyInjection\MatTheCatHtmlCompressorExtension;
8+
9+
class MatTheCatHtmlCompressorBundle extends Bundle
10+
{
11+
public function __construct()
12+
{
13+
$this->extension = new MatTheCatHtmlCompressorExtension;
14+
}
15+
}

Resources/config/services.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
parameters:
2+
html_compressor.response_handler.class: MatTheCat\HtmlCompressorBundle\EventListener\ResponseListener
3+
html_compressor.response_handler.event.priority: -255
4+
5+
services:
6+
html_compressor.response_handler:
7+
class: '%html_compressor.response_handler.class%'
8+
arguments:
9+
- '%html_compressor.java.bin%'
10+
- '%html_compressor.htmlcompressor.bin%'
11+
- '%html_compressor.htmlcompressor.options%'
12+
tags:
13+
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse, priority: %html_compressor.response_handler.event.priority% }

0 commit comments

Comments
 (0)