1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Transformers; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\StreamInterface; |
6
|
|
|
use JSMinPlus; |
7
|
|
|
use CSSmin; |
8
|
|
|
use Minify_HTML; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Generic resolver to minify responses. |
12
|
|
|
*/ |
13
|
|
|
class Minifier extends Resolver |
14
|
|
|
{ |
15
|
|
|
protected $transformers = [ |
16
|
|
|
'text/javascript' => [__CLASS__, 'js'], |
17
|
|
|
'text/css' => [__CLASS__, 'css'], |
18
|
|
|
'text/html' => [__CLASS__, 'html'], |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Javascript minifier. |
23
|
|
|
* |
24
|
|
|
* @param StreamInterface $input |
25
|
|
|
* @param StreamInterface $output |
26
|
|
|
* |
27
|
|
|
* @return StreamInterface |
28
|
|
|
*/ |
29
|
|
|
public static function js(StreamInterface $input, StreamInterface $output) |
30
|
|
|
{ |
31
|
|
|
$output->write(JSMinPlus::minify((string) $input)); |
32
|
|
|
|
33
|
|
|
return $output; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* CSS minifier. |
38
|
|
|
* |
39
|
|
|
* @param StreamInterface $input |
40
|
|
|
* @param StreamInterface $output |
41
|
|
|
* |
42
|
|
|
* @return StreamInterface |
43
|
|
|
*/ |
44
|
|
|
public static function css(StreamInterface $input, StreamInterface $output) |
45
|
|
|
{ |
46
|
|
|
$output->write((new CSSmin())->run((string) $input)); |
47
|
|
|
|
48
|
|
|
return $output; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* HTML minifier. |
53
|
|
|
* |
54
|
|
|
* @param StreamInterface $input |
55
|
|
|
* @param StreamInterface $output |
56
|
|
|
* |
57
|
|
|
* @return StreamInterface |
58
|
|
|
*/ |
59
|
|
|
public static function html(StreamInterface $input, StreamInterface $output) |
60
|
|
|
{ |
61
|
|
|
$output->write(Minify_HTML::minify((string) $input, ['jsCleanComments' => true])); |
62
|
|
|
|
63
|
|
|
return $output; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|