-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathminify.class.php
125 lines (114 loc) · 2.79 KB
/
minify.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
class minify {
/*
* @str = all the css in 1 string
*
* Credits:: thanks to http://kitmacallister.com/2011/minify-css-with-php/
*/
function minifyCSS($str){
$find = array('!/\*.*?\*/!s',
'/\n\s*\n/',
'/[\n\r \t]/',
'/ +/',
'/ ?([,:;{}]) ?/',
'/;}/'
);
$repl = array('',
"\n",
' ',
' ',
'$1',
'}'
);
return preg_replace($find, $repl, $str);
}
/*
* @str = all the js in 1 string
* I wrote this one out, its not perfect, but does compress the file a little.
*/
function minifyJS($str){
return preg_replace(
array(
'!/\*.*?\*/!s',
"/\n\s+/",
"/\n(\s*\n)+/",
"!\n//.*?\n!s",
"/\n\}(.+?)\n/",
"/;\n/"
), array(
'',
"\n",
"\n",
"\n",
"}\\1\n",
';'
), $str);
}
/*!
* compress, Compress the CSS and JS files into 1 file.
*
* @type = css || js
* @files = array of the files to compress
* @file = the path/name of the file
*/
function compress($type, $files, $file) {
$LastUpdate = $this->getLU($files);
if(is_file($_SERVER['DOCUMENT_ROOT'].$file)){
$MainLast = filemtime($_SERVER['DOCUMENT_ROOT'].$file);
} else { $updateF = 1; $MainLast = 0; }
if(isset($updateF) || $LastUpdate > $MainLast){
// files have been updated so update the minified file
switch($type){
case 'css':
$Contents = $this->getFCont($files);
$NewCSS = $this->minifyCSS($Contents['con']);
$PutIn = "/*\n This is a minified version of Skem9s CSS ".$Contents['top']."\n*/\n".$NewCSS;
break;
case 'js':
$Contents = $this->getFCont($files);
$NewJS = $this->minifyJS($Contents['con']);
$PutIn = "/*\n This is a combined version of Skem9s JS ".$Contents['top']."\n*/\n".$NewJS;
break;
}
$gogo = $this->saveFile($file, $PutIn);
}
return $file.'?vers='.$LastUpdate;
}
/*
* getLU Last Updated File
*
* @files = array of all of the files
*/
function getLU($files){
$LastUpdate = 0;
foreach($files as $v){
$ed = filemtime($_SERVER['DOCUMENT_ROOT'].$v);
if($ed > $LastUpdate){ $LastUpdate = $ed; }
}
return $LastUpdate;
}
function saveFile($file, $save){
$fp = fopen($_SERVER['DOCUMENT_ROOT'].$file, 'w');
flock($fp, LOCK_EX);
fwrite($fp, $save);
flock($fp, LOCK_UN);
fclose($fp);
return true;
}
/*
* get File Contents
*
* @files = array of all the files
*/
function getFCont($files){
$cont = '';
$FileTop = "\n\n Last Updated:: ".date ("F d Y H:i:s.", time())."\n\n Files include::\n";
$root = $_SERVER['DOCUMENT_ROOT'];
foreach($files as $v){
$FileTop .= " ".$v."\n";
$cont .= is_file($root.$v) ? file_get_contents($root.$v)."\n" : '';
}
return array('con' => $cont, 'top' => $FileTop);
}
}
?>