-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.loader.php
195 lines (168 loc) · 5.35 KB
/
class.loader.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/*
* A small PHP class that minifies website resources and reduces requests
*
*
* -- MIT license --
* Copyright (c) 2013 Luke Ward
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* --
*
* @author Luke Ward <[email protected]>
* @license http://opensource.org/licenses/mit-license.php MIT License
* @link https://github.com/flabbyrabbit/minifier-php
*/
require_once("class.minijs.php");
class PHPMinifier {
/*
* Base directories can be assigned for both javascript and css
* when all files reside within a common directory. A min folder
* will be generated at these locations to store generated files
*/
var $js_base = '/javascript';
var $css_base = '/css';
/*
* Default file arrays for both JS and CSS contain common files
* across the application. These will be generated seperately to any
* files that are specific to portions of the application
*/
var $default_js = Array('/main.js');
var $default_css = Array('/reset.css', '/main.css');
/*
* Generates and stores minified versions of selected javascript and css files
* Prints link and script tags for generated files
*/
public function load() {
//Build default CSS file
$path = "{$this->css_base}/min/main.css";
if ($this->generate($path, $this->default_css, 'css')) {
$includes = "<link rel='stylesheet' href='{$path}' type='text/css'/>\n";
}
//Build custom CSS file, if required
if (is_array($this->custom_css) && count($this->custom_css)) {
//generate filename to reflect contents
$id = substr(md5(implode($this->custom_css)),0,10);
$path = "{$this->css_base}/min/extra_{$id}.css";
if ($this->generate($path, $this->custom_css, 'css')) {
$includes .= "<link rel='stylesheet' href='{$path}' type='text/css'/>\n";
}
}
//Build default JS file
$path = "{$this->js_base}/min/main.js";
if ($this->generate($path, $this->default_js, 'js')) {
$includes .= "<script type='text/javascript' src='{$path}'></script>\n";
}
//Build custom JS, if required
if (is_array($this->custom_js) && count($this->custom_js)) {
//generate filename to reflect contents
$id = substr(md5(implode($this->custom_js)),0,10);
$path = "{$this->js_base}/min/extra_{$id}.css";
if ($this->generate($path, $this->custom_js, 'js')) {
$includes .= "<script type='text/javascript' src='{$path}'></script>\n";
}
}
echo $includes;
}
/*
*
*
*/
public function add_file($filename, $type) {
if ($type == 'js') {
if (!is_array($this->custom_js) || !count($this->custom_js)) {
$this->custom_js = array();
}
$this->custom_js = array_merge($this->custom_js, (array)$filename);
} else if ($type == 'css') {
if (!is_array($this->custom_css) || !count($this->custom_css)) {
$this->custom_css = array();
}
$this->custom_css = array_merge($this->custom_css, (array)$filename);
}
}
/*
*
*
*/
private function generate($filename, $file_array, $type) {
if ($type == 'js') {
$base = $this->js_base;
} else if ($type == 'css') {
$base = $this->css_base;
} else {
return false;
}
/*
* check if generated file already exists
* if so store last modified time for comparison
*/
if (file_exists($filename)) {
$modified = filemtime($filename);
foreach ($file_array as $file) {
$filepath = $base.$file;
if ((file_exists($filepath)) && (filemtime($filepath) > $modified)) {
$generate = true;
break;
}
}
} else {
$generate = true;
}
if ($generate) {
// load and concatenate file contents
foreach ($file_array as $file) {
$filepath = $base.$file;
if (file_exists($filepath)) {
$contents .= file_get_contents($filepath) . "\n";
}
}
// select minification rountine
if ($type == 'js') {
$contents = $this->minify_js($contents);
} else if ($type == 'css') {
$contents = $this->minify_css($contents);
}
// store file
file_put_contents($filepath, $contents);
}
return true;
}
/*
*
*
*/
private function minify_js($contents) {
$jsmin = new JSMin($contents);
$contents = $jsmin->min();
return $contents;
}
/*
*
*
*/
private function minify_css($contents) {
$contents = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $contents);
$contents = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $contents);
return $contents;
}
}
?>