|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Simple\Asset; |
|
4
|
|
|
|
|
5
|
|
|
use MatthiasMullie\Minify; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* wrapper for MatthiasMullie\Minify |
|
9
|
|
|
*/ |
|
10
|
|
|
class Serve |
|
11
|
|
|
{ |
|
12
|
|
|
public $path; |
|
13
|
|
|
public $opts = array( |
|
14
|
|
|
'etag' => true, |
|
15
|
|
|
'timeout' => 604800, |
|
16
|
|
|
'maxAge' => 315360000, |
|
17
|
|
|
'gzip' => false |
|
18
|
|
|
); |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
public function __construct($path = null, $opts = array()) |
|
22
|
|
|
{ |
|
23
|
|
|
if (!empty($path)) { |
|
24
|
|
|
$dir = dirname($path); |
|
25
|
|
|
if (!file_exists($dir)) { |
|
26
|
|
|
@mkdir($dir, 0755, true); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$this->path = $path; |
|
31
|
|
|
|
|
32
|
|
|
$this->opts = array_merge($this->opts, $opts); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* css loader |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $files |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function css(array $files) |
|
42
|
|
|
{ |
|
43
|
|
|
$minify = new Minify\CSS(); |
|
44
|
|
|
$minify->setMaxImportSize(2048); |
|
45
|
|
|
|
|
46
|
|
|
$minify->setImportExtensions( |
|
47
|
|
|
array( |
|
48
|
|
|
'gif' => 'data:image/gif', |
|
49
|
|
|
'png' => 'data:image/png', |
|
50
|
|
|
'jpe' => 'data:image/jpeg', |
|
51
|
|
|
'jpg' => 'data:image/jpeg', |
|
52
|
|
|
'jpeg' => 'data:image/jpeg', |
|
53
|
|
|
'svg' => 'data:image/svg+xml', |
|
54
|
|
|
'woff' => 'data:application/x-font-woff', |
|
55
|
|
|
'ttf' => 'data:application/x-font-ttf', |
|
56
|
|
|
'ttc' => 'data:application/x-font-ttf', |
|
57
|
|
|
'otf' => 'data:application/x-font-otf', |
|
58
|
|
|
'eot' => 'data:application/vnd.ms-fontobject', |
|
59
|
|
|
'woff2' => 'data:application/font-woff2', |
|
60
|
|
|
'tif' => 'image/tiff', |
|
61
|
|
|
'tiff' => 'image/tiff', |
|
62
|
|
|
'xbm' => 'image/x-xbitmap', |
|
63
|
|
|
) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$minify->add($files); |
|
67
|
|
|
|
|
68
|
|
|
return $this->offer( |
|
69
|
|
|
$minify, |
|
70
|
|
|
$files, |
|
71
|
|
|
array( |
|
72
|
|
|
'Content-type' => 'text/css' |
|
73
|
|
|
) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* js loader |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $files |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function js(array $files) |
|
84
|
|
|
{ |
|
85
|
|
|
$minify = new Minify\JS(); |
|
86
|
|
|
|
|
87
|
|
|
return $this->offer( |
|
88
|
|
|
$minify, |
|
89
|
|
|
$files, |
|
90
|
|
|
array( |
|
91
|
|
|
'Content-type' => 'text/javascript' |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* offer |
|
98
|
|
|
* |
|
99
|
|
|
* @param Minify\Minify $minify |
|
100
|
|
|
* @param array $files |
|
101
|
|
|
* @param array $header |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
private function offer($minify, $files, $header = array()) |
|
105
|
|
|
{ |
|
106
|
|
|
header("Expires: ".gmdate('D, d M Y H:i:s', time() + $this->opts['timeout'])." GMT", true); |
|
107
|
|
|
header("Last-Modified: ".gmdate('D, d M Y H:i:s', time())." GMT", true); |
|
108
|
|
|
header("Cache-Control: max-age=".$this->opts['maxAge'], true); |
|
109
|
|
|
header("Pragma: cache", true); |
|
110
|
|
|
|
|
111
|
|
|
$minify->add($files); |
|
112
|
|
|
|
|
113
|
|
|
$buffer = $this->opts['gzip'] |
|
114
|
|
|
? $minify->gzip( |
|
115
|
|
|
$this->path, |
|
116
|
|
|
(int) $this->opts['gzip'] |
|
117
|
|
|
) |
|
118
|
|
|
: $minify->minify( |
|
119
|
|
|
$this->path |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
foreach ($header as $key => $value) { |
|
123
|
|
|
header("$key: $value", true); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if ($this->opts['etag']) { |
|
127
|
|
|
header("Etag: " . md5($buffer), true); |
|
128
|
|
|
} |
|
129
|
|
|
session_cache_limiter('public'); |
|
130
|
|
|
|
|
131
|
|
|
return $buffer; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: