-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression.php
More file actions
160 lines (124 loc) · 5.05 KB
/
compression.php
File metadata and controls
160 lines (124 loc) · 5.05 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
class compression {
private $js_dirs, $css_dirs, $merged_dir, $home_dir;
public function __construct($js_dirs, $css_dirs, $merged_dir, $home_dir) {
$this->home_dir = $home_dir;
$this->js_dirs = $js_dirs;
$this->css_dirs = $css_dirs;
$this->mdir = $merged_dir;
require_once dirname(__FILE__) . '/JSmin.php';
}
static public function compress($js_dirs = array(), $css_dirs = array(), $merged_dir = null, $home_dir = null) {
$c = new compression($js_dirs, $css_dirs, $merged_dir, $home_dir);
$c->all();
}
public function all() {
$this->precheking();
$sections = array('js' => array(),'css' => array());
foreach($this->js_dirs as $js_dirs){
foreach(scandir($this->home_dir.$js_dirs) as $file) {
if(preg_match('/\.js$/s',$file)) {
$sections['js'][$this->mdir.'all.js'][] = $js_dirs.$file;
}
}
}
foreach($this->css_dirs as $css_dirs){
foreach(scandir($this->home_dir.$css_dirs) as $file) {
if(preg_match('/\.css$/',$file)) {
if(!preg_match('/ie(\d)*/',strtolower($file)) && !preg_match('/print/',strtolower($file))) {
$sections['css'][$this->mdir.'all.css'][] = $css_dirs.$file;
}
}
}
}
if(isset($_COOKIE['debug'])){
echo '<pre>';print_r($sections);echo '</pre>';
$this->error('DEBUG');
}
foreach($sections as $section => $data) {
foreach($data as $target => $files) {
if(($modified = @filemtime($this->home_dir.$target)) !== false) {
foreach($files as $file) {
if(is_file($file) && (int)@filemtime($this->home_dir.$file) > $modified) {
if($section === 'css') {
$this->css($target,$files);
} else {
$this->js($target,$files);
}
break;
}
}
} else {
if($section === 'css') {
$this->css($target,$files);
} else {
$this->js($target,$files);
}
}
}
}
}
private function js($target,$files) {
$merged = $this->merge($files);
try {
$this->write($target,JSmin::minify($merged));
}
catch (JSMinException $e) {
$this->write($target,$merged);
}
}
private function css($target,$files) {
$merged = $this->merge($files);
$merged = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!','',$merged);
$merged = str_replace(array("\r","\n","\t",' ',' '),'',$merged);
$this->write($target,$merged);
}
private function merge($files) {
$merged = '';
foreach($files as $file) {
if(($content = @file_get_contents($this->home_dir.$file)) !== false) {
$merged .= $content."\n";
} else {
$this->error(sprintf('Cannot read file: %s', $this->home_dir.$file));
}
}
return $merged;
}
private function write($target,$content) {
if(@file_put_contents($this->home_dir.$target,$content) !== false) {
@chmod($target,0666);
} else {
$this->error(sprintf('Cannot write file: %s', $this->home_dir.$target));
}
}
private function precheking(){
if(!is_null($this->home_dir) && !file_exists($this->home_dir)){
$this->error(sprintf('The <strong>%s</strong> does not exist.', $this->home_dir));
}
if(!is_null($this->merged_dir) && !file_exists($this->home_dir.$this->merged_dir)){
$this->error(sprintf('The <strong>%s</strong> does not exist.', $this->home_dir.$this->merged_dir));
}
if(!empty($this->js_dirs) && is_array($this->js_dirs)){
foreach($this->js_dirs as $js_dir){
if(!file_exists($this->home_dir.$js_dir)){
$this->error(sprintf('The <strong>%s</strong> does not exist.', $this->home_dir.$js_dir));
}
}
}else{
$this->error('Var <strong>$js_dirs</strong> not array');
}
if(!empty($this->css_dirs) && is_array($this->css_dirs)){
foreach($this->css_dirs as $css_dir){
if(!file_exists($this->home_dir.$css_dir)){
$this->error(sprintf('The <strong>%s</strong> does not exist.', $this->home_dir.$css_dir));
}
}
}else{
$this->error('Var <strong>$css_dirs</strong> not array');
}
}
private function error($msg){
die(__CLASS__ . " error: <br />" . $msg);
}
}
?>