-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileBundler.php
More file actions
executable file
·241 lines (205 loc) · 8.54 KB
/
FileBundler.php
File metadata and controls
executable file
·241 lines (205 loc) · 8.54 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* FileBundler.php - version 1.0
*
* Copyright (c) 2008 Aleksandar Kolundzija (subchild.com)
* 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 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.
*
*
* FileBundler is used for concatenating multiple JavaScript or CSS files into
* a single, optimized file (bundle). Processing is done in real-time. If a
* requested collection of files is already bundled that bundle is used,
* otherwise a new one is created and and written to disk for future use.
* Changes to source code therefore require deleting old, outdated bundles.
*/
class FileBundler
{
define("ENABLE_HEAD_INCLUDE_BUNDLES", true); // this should go into a config file and be used to dis/en-able bundling
private $type; // "js" | "css"
private $files = array();
private $approot = ""; // root directory of web application
private $sourceDir = ""
private $bundleDir = "";
private $debugMode;
private $compress;
private $optimizer; // "jsmin" | "packer"
/* PRIVATE METHODS ------------------------------------------------ */
private function log($message){
if ($this->debugMode){ error_log("*** FileBundler ***: $message"); }
}
private function getBundleWebDir(){
return $this->bundleDir;
}
private function getBundleSysDir(){
return $approot.$this->getBundleWebDir();
}
/**
* Method generateBundleFilename() sorts filenames, concatenates them
* into a string and hashes it. Resulting string is used as a unique
* filename for this set of files.
*/
private function generateBundleFilename(){
$files = $this->files;
sort($files);
$generatedFilename = md5(implode(".",$files)) . "." . $this->type;
return $generatedFilename;
}
private function compressJavaScriptBundle($code){
$compressedCode;
switch ($this->optimizer){
case "jsmin" :
require_once 'jsmin-1.1.1.php';
$compressedCode = JSMin::minify($code);
break;
case "packer" :
require_once 'class.JavaScriptPacker.php';
$packer = new JavaScriptPacker($code, 'Normal', true, false);
$compressedCode = $packer->pack();
break;
default :
$this->log("Unknown JavaScript optimizer was specified");
break;
}
return $compressedCode;
}
private function compressCSSBundle($css){
$css = preg_replace('/[\r\n\t\s]+/s', ' ', $css); // new lines, multiple spaces/tabs/newlines
$css = preg_replace('#/\*.*?\*/#', '', $css); // comments
$css = preg_replace('/[\s]*([\{\},;:])[\s]*/', '\1', $css); // spaces before and after marks
$css = preg_replace('/^\s+/', '', $css); // spaces on the beginning
return $css;
}
/* PUBLIC METHODS ------------------------------------------------- */
/**
* Constructs a FileBundler, used for bundling JavaScript and CSS includes
* into a single file. File optimization (minifying) is supported.
*
* @param array $props
*/
public function __construct($props){
$this->type = isset($props['type']) ? $props['type'] : "js"; // default file type is js (JavaScript)
$this->debugMode = isset($props['debugMode']) ? $props['debugMode'] : false; // set debugMode to true for useful trace messages
$this->compress = isset($props['compress']) ? $props['compress'] : true; // when compress is set to false no bundling takes place. individual files are included
$this->optimizer = isset($props['optimizer']) ? $props['optimizer'] : "jsmin"; // "jsmin" or "packer"
$this->approot = isset($props['approot']) ? $props['approot'] : ""; // application root
$this->sourceDir = isset($props['sourceDir']) ? $props['sourceDir'] : "/{$this->type}"; // source directory, ex: "/scripts"
$this->bundleDir = isset($props['bundleDir']) ? $props['bundleDir'] : "/{$this->type}/bundles"; // bundle directory
$this->showList = isset($props['showList']) ? $props['showList'] : true; // include file list into comment in bundle
if (isset($props['files'])){
$this->addFiles($props['files']);
}
}
/**
* Adds a single file to bundler. This is really a shortcut for the addFiles()
* method intended to be used when only a single file is being added.
*
* @param string $file
*/
public function addFile($file){
$this->addFiles(array($file));
}
/**
* Adds files (js or css) to be bundled.
*
* @param array $files
*/
public function addFiles($files){
$this->log("Adding files: " . implode(",",$files));
$filesWithRootRelativePaths = array();
for ($i=0; $i<count($files); $i++){
if (substr($files[$i],0,1)!="/"){
$filesWithRootRelativePaths[] = $this->sourceDirs[$this->type].$files[$i];
}
else {
$filesWithRootRelativePaths[] = $files[$i];
}
if (!is_file($approot.$filesWithRootRelativePaths[count($filesWithRootRelativePaths)-1])){
$this->log($filesWithRootRelativePaths[count($filesWithRootRelativePaths)-1] . " doesn't exist. removing from bundle.");
array_pop($filesWithRootRelativePaths);
}
}
$this->files = array_merge($this->files, $filesWithRootRelativePaths);
$this->files = array_unique($this->files);
}
/**
* Writes the HTML tag (script or link) for including the bundled file.
* If the requested bundle already exists, it is reused. Otherwise, a new
* bundle is created (written to disk). Generated bundle can be optimized
* (compressed).
*
* @param boolean $compress
*/
public function writeBundle($overwrite = false){
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$newBundle = false;
if (ENABLE_HEAD_INCLUDE_BUNDLES){
$bundleName = $this->generateBundleFilename();
$bundleSysPath = $this->getBundleSysDir().$bundleName;
if (!is_file($bundleSysPath) || $overwrite){ // if bundle doesn't exist
$newBundle = true;
$this->log(">>> CREATING A NEW BUNDLE <<<");
$fileList = ($this->showList) ? "/*\n" . implode("\n",$this->files) . "\n*/\n" : "";
$code = "";
foreach ($this->files as $file){
$code .= file_get_contents($approot.$file);
}
if ($this->compress){
switch ($this->type){
case "js" :
$this->log("Compressing JS bundle...");
$code = $this->compressJavaScriptBundle($code);
break;
case "css" :
$this->log("Compressing CSS bundle...");
$code = $this->compressCSSBundle($code);
break;
default :
$this->log("writeBundle(): Unknown bundle type");
break;
}
}
file_put_contents($bundleSysPath, $fileList.$thirdPartyCode.$code);
}
switch ($this->type){
case "js" : print "<script src=\"{$this->getBundleWebDir()}{$bundleName}\" type=\"text/javascript\"></script>\n"; break;
case "css" : print "<link href=\"{$this->getBundleWebDir()}{$bundleName}\" type=\"text/css\" rel=\"stylesheet\"/>\n"; break;
default : $this->log("writeBundle(): Unknown bundle type"); break;
}
}
else {
foreach ($this->files as $file){
switch ($this->type){
case "js" : print "<script src=\"$file\" type=\"text/javascript\"></script>\n"; break;
case "css" : print "<link href=\"$file\" type=\"text/css\" rel=\"stylesheet\"/>\n"; break;
default : $this->log("writeBundle(): Unknown bundle type (not bundling)"); break;
}
}
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
$this->log("writeBundle(): new bundle created in: $totaltime");
}
}
?>