1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MatthiasMullie\Minify; |
4
|
|
|
|
5
|
|
|
use MatthiasMullie\Minify\Exceptions\IOException; |
6
|
|
|
use Psr\Cache\CacheItemInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Abstract minifier class. |
10
|
|
|
* |
11
|
|
|
* Please report bugs on https://github.com/matthiasmullie/minify/issues |
12
|
|
|
* |
13
|
|
|
* @author Matthias Mullie <[email protected]> |
14
|
|
|
* @copyright Copyright (c) 2012, Matthias Mullie. All rights reserved. |
15
|
|
|
* @license MIT License |
16
|
|
|
*/ |
17
|
|
|
abstract class Minify |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The data to be minified. |
21
|
|
|
* |
22
|
|
|
* @var string[] |
23
|
|
|
*/ |
24
|
|
|
protected $data = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Array of patterns to match. |
28
|
|
|
* |
29
|
|
|
* @var string[] |
30
|
|
|
*/ |
31
|
|
|
protected $patterns = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* This array will hold content of strings and regular expressions that have |
35
|
|
|
* been extracted from the JS source code, so we can reliably match "code", |
36
|
|
|
* without having to worry about potential "code-like" characters inside. |
37
|
|
|
* |
38
|
|
|
* @var string[] |
39
|
|
|
*/ |
40
|
|
|
public $extracted = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Init the minify class - optionally, code may be passed along already. |
44
|
|
|
*/ |
45
|
|
|
public function __construct(/* $data = null, ... */) |
46
|
|
|
{ |
47
|
|
|
// it's possible to add the source through the constructor as well ;) |
48
|
|
|
if (func_num_args()) { |
49
|
|
|
call_user_func_array(array($this, 'add'), func_get_args()); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Add a file or straight-up code to be minified. |
55
|
|
|
* |
56
|
|
|
* @param string $data |
57
|
|
|
*/ |
58
|
|
|
public function add($data /* $data = null, ... */) |
59
|
|
|
{ |
60
|
|
|
// bogus "usage" of parameter $data: scrutinizer warns this variable is |
61
|
|
|
// not used (we're using func_get_args instead to support overloading), |
62
|
|
|
// but it still needs to be defined because it makes no sense to have |
63
|
|
|
// this function without argument :) |
64
|
|
|
$args = array($data) + func_get_args(); |
65
|
|
|
|
66
|
|
|
// this method can be overloaded |
67
|
|
|
foreach ($args as $data) { |
68
|
|
|
// redefine var |
69
|
|
|
$data = (string) $data; |
70
|
|
|
|
71
|
|
|
// load data |
72
|
|
|
$value = $this->load($data); |
73
|
|
|
$key = ($data != $value) ? $data : count($this->data); |
74
|
|
|
|
75
|
|
|
// store data |
76
|
|
|
$this->data[$key] = $value; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Minify the data & (optionally) saves it to a file. |
82
|
|
|
* |
83
|
|
|
* @param string[optional] $path Path to write the data to. |
84
|
|
|
* |
85
|
|
|
* @return string The minified data. |
86
|
|
|
*/ |
87
|
|
|
public function minify($path = null) |
88
|
|
|
{ |
89
|
|
|
$content = $this->execute($path); |
90
|
|
|
|
91
|
|
|
// save to path |
92
|
|
|
if ($path !== null) { |
93
|
|
|
$this->save($content, $path); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $content; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Minify & gzip the data & (optionally) saves it to a file. |
101
|
|
|
* |
102
|
|
|
* @param string[optional] $path Path to write the data to. |
103
|
|
|
* @param int[optional] $level Compression level, from 0 to 9. |
104
|
|
|
* |
105
|
|
|
* @return string The minified & gzipped data. |
106
|
|
|
*/ |
107
|
|
|
public function gzip($path = null, $level = 9) |
108
|
|
|
{ |
109
|
|
|
$content = $this->execute($path); |
110
|
|
|
$content = gzencode($content, $level, FORCE_GZIP); |
111
|
|
|
|
112
|
|
|
// save to path |
113
|
|
|
if ($path !== null) { |
114
|
|
|
$this->save($content, $path); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $content; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Minify the data & write it to a CacheItemInterface object. |
122
|
|
|
* |
123
|
|
|
* @param CacheItemInterface $item Cache item to write the data to. |
124
|
|
|
* |
125
|
|
|
* @return CacheItemInterface Cache item with the minifier data. |
126
|
|
|
*/ |
127
|
|
|
public function cache(CacheItemInterface $item) |
128
|
|
|
{ |
129
|
|
|
$content = $this->execute(); |
130
|
|
|
$item->set($content); |
131
|
|
|
|
132
|
|
|
return $item; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Minify the data. |
137
|
|
|
* |
138
|
|
|
* @param string[optional] $path Path to write the data to. |
139
|
|
|
* |
140
|
|
|
* @return string The minified data. |
141
|
|
|
*/ |
142
|
|
|
abstract public function execute($path = null); |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Load data. |
146
|
|
|
* |
147
|
|
|
* @param string $data Either a path to a file or the content itself. |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
protected function load($data) |
152
|
|
|
{ |
153
|
|
|
// check if the data is a file |
154
|
|
|
if ($this->canImportFile($data)) { |
155
|
|
|
$data = file_get_contents($data); |
156
|
|
|
|
157
|
|
|
// strip BOM, if any |
158
|
|
|
if (substr($data, 0, 3) == "\xef\xbb\xbf") { |
159
|
|
|
$data = substr($data, 3); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $data; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Save to file. |
168
|
|
|
* |
169
|
|
|
* @param string $content The minified data. |
170
|
|
|
* @param string $path The path to save the minified data to. |
171
|
|
|
* |
172
|
|
|
* @throws IOException |
173
|
|
|
*/ |
174
|
|
|
protected function save($content, $path) |
175
|
|
|
{ |
176
|
|
|
$handler = $this->openFileForWriting($path); |
177
|
|
|
|
178
|
|
|
$this->writeToFile($handler, $content); |
179
|
|
|
|
180
|
|
|
@fclose($handler); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Register a pattern to execute against the source content. |
185
|
|
|
* |
186
|
|
|
* @param string $pattern PCRE pattern. |
187
|
|
|
* @param string|callable $replacement Replacement value for matched pattern. |
188
|
|
|
*/ |
189
|
|
|
protected function registerPattern($pattern, $replacement = '') |
190
|
|
|
{ |
191
|
|
|
// study the pattern, we'll execute it more than once |
192
|
|
|
$pattern .= 'S'; |
193
|
|
|
|
194
|
|
|
$this->patterns[] = array($pattern, $replacement); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* We can't "just" run some regular expressions against JavaScript: it's a |
199
|
|
|
* complex language. E.g. having an occurrence of // xyz would be a comment, |
200
|
|
|
* unless it's used within a string. Of you could have something that looks |
201
|
|
|
* like a 'string', but inside a comment. |
202
|
|
|
* The only way to accurately replace these pieces is to traverse the JS one |
203
|
|
|
* character at a time and try to find whatever starts first. |
204
|
|
|
* |
205
|
|
|
* @param string $content The content to replace patterns in. |
206
|
|
|
* |
207
|
|
|
* @return string The (manipulated) content. |
208
|
|
|
*/ |
209
|
|
|
protected function replace($content) |
210
|
|
|
{ |
211
|
|
|
$processed = ''; |
212
|
|
|
$positions = array_fill(0, count($this->patterns), -1); |
213
|
|
|
$matches = array(); |
214
|
|
|
|
215
|
|
|
while ($content) { |
216
|
|
|
// find first match for all patterns |
217
|
|
|
foreach ($this->patterns as $i => $pattern) { |
218
|
|
|
list($pattern, $replacement) = $pattern; |
219
|
|
|
|
220
|
|
|
// no need to re-run matches that are still in the part of the |
221
|
|
|
// content that hasn't been processed |
222
|
|
|
if ($positions[$i] >= 0) { |
223
|
|
|
continue; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$match = null; |
227
|
|
|
if (preg_match($pattern, $content, $match)) { |
228
|
|
|
$matches[$i] = $match; |
229
|
|
|
|
230
|
|
|
// we'll store the match position as well; that way, we |
231
|
|
|
// don't have to redo all preg_matches after changing only |
232
|
|
|
// the first (we'll still know where those others are) |
233
|
|
|
$positions[$i] = strpos($content, $match[0]); |
234
|
|
|
} else { |
235
|
|
|
// if the pattern couldn't be matched, there's no point in |
236
|
|
|
// executing it again in later runs on this same content; |
237
|
|
|
// ignore this one until we reach end of content |
238
|
|
|
unset($matches[$i]); |
239
|
|
|
$positions[$i] = strlen($content); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// no more matches to find: everything's been processed, break out |
244
|
|
|
if (!$matches) { |
|
|
|
|
245
|
|
|
$processed .= $content; |
246
|
|
|
break; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// see which of the patterns actually found the first thing (we'll |
250
|
|
|
// only want to execute that one, since we're unsure if what the |
251
|
|
|
// other found was not inside what the first found) |
252
|
|
|
$discardLength = min($positions); |
253
|
|
|
$firstPattern = array_search($discardLength, $positions); |
254
|
|
|
$match = $matches[$firstPattern][0]; |
255
|
|
|
|
256
|
|
|
// execute the pattern that matches earliest in the content string |
257
|
|
|
list($pattern, $replacement) = $this->patterns[$firstPattern]; |
258
|
|
|
$replacement = $this->replacePattern($pattern, $replacement, $content); |
259
|
|
|
|
260
|
|
|
// figure out which part of the string was unmatched; that's the |
261
|
|
|
// part we'll execute the patterns on again next |
262
|
|
|
$content = substr($content, $discardLength); |
263
|
|
|
$unmatched = (string) substr($content, strpos($content, $match) + strlen($match)); |
264
|
|
|
|
265
|
|
|
// move the replaced part to $processed and prepare $content to |
266
|
|
|
// again match batch of patterns against |
267
|
|
|
$processed .= substr($replacement, 0, strlen($replacement) - strlen($unmatched)); |
268
|
|
|
$content = $unmatched; |
269
|
|
|
|
270
|
|
|
// first match has been replaced & that content is to be left alone, |
271
|
|
|
// the next matches will start after this replacement, so we should |
272
|
|
|
// fix their offsets |
273
|
|
|
foreach ($positions as $i => $position) { |
274
|
|
|
$positions[$i] -= $discardLength + strlen($match); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return $processed; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* This is where a pattern is matched against $content and the matches |
283
|
|
|
* are replaced by their respective value. |
284
|
|
|
* This function will be called plenty of times, where $content will always |
285
|
|
|
* move up 1 character. |
286
|
|
|
* |
287
|
|
|
* @param string $pattern Pattern to match. |
288
|
|
|
* @param string|callable $replacement Replacement value. |
289
|
|
|
* @param string $content Content to match pattern against. |
290
|
|
|
* |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
protected function replacePattern($pattern, $replacement, $content) |
294
|
|
|
{ |
295
|
|
|
if (is_callable($replacement)) { |
296
|
|
|
return preg_replace_callback($pattern, $replacement, $content, 1, $count); |
297
|
|
|
} else { |
298
|
|
|
return preg_replace($pattern, $replacement, $content, 1, $count); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Strings are a pattern we need to match, in order to ignore potential |
304
|
|
|
* code-like content inside them, but we just want all of the string |
305
|
|
|
* content to remain untouched. |
306
|
|
|
* |
307
|
|
|
* This method will replace all string content with simple STRING# |
308
|
|
|
* placeholder text, so we've rid all strings from characters that may be |
309
|
|
|
* misinterpreted. Original string content will be saved in $this->extracted |
310
|
|
|
* and after doing all other minifying, we can restore the original content |
311
|
|
|
* via restoreStrings(). |
312
|
|
|
* |
313
|
|
|
* @param string[optional] $chars |
314
|
|
|
*/ |
315
|
|
|
protected function extractStrings($chars = '\'"') |
316
|
|
|
{ |
317
|
|
|
// PHP only supports $this inside anonymous functions since 5.4 |
318
|
|
|
$minifier = $this; |
319
|
|
|
$callback = function ($match) use ($minifier) { |
320
|
|
|
// check the second index here, because the first always contains a quote |
321
|
|
|
if (!$match[2]) { |
322
|
|
|
/* |
323
|
|
|
* Empty strings need no placeholder; they can't be confused for |
324
|
|
|
* anything else anyway. |
325
|
|
|
* But we still needed to match them, for the extraction routine |
326
|
|
|
* to skip over this particular string. |
327
|
|
|
*/ |
328
|
|
|
return $match[0]; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$count = count($minifier->extracted); |
332
|
|
|
$placeholder = $match[1].$count.$match[1]; |
333
|
|
|
$minifier->extracted[$placeholder] = $match[1].$match[2].$match[1]; |
334
|
|
|
|
335
|
|
|
return $placeholder; |
336
|
|
|
}; |
337
|
|
|
|
338
|
|
|
/* |
339
|
|
|
* The \\ messiness explained: |
340
|
|
|
* * Don't count ' or " as end-of-string if it's escaped (has backslash |
341
|
|
|
* in front of it) |
342
|
|
|
* * Unless... that backslash itself is escaped (another leading slash), |
343
|
|
|
* in which case it's no longer escaping the ' or " |
344
|
|
|
* * So there can be either no backslash, or an even number |
345
|
|
|
* * multiply all of that times 4, to account for the escaping that has |
346
|
|
|
* to be done to pass the backslash into the PHP string without it being |
347
|
|
|
* considered as escape-char (times 2) and to get it in the regex, |
348
|
|
|
* escaped (times 2) |
349
|
|
|
*/ |
350
|
|
|
$this->registerPattern('/(['.$chars.'])(.*?(?<!\\\\)(\\\\\\\\)*+)\\1/s', $callback); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* This method will restore all extracted data (strings, regexes) that were |
355
|
|
|
* replaced with placeholder text in extract*(). The original content was |
356
|
|
|
* saved in $this->extracted. |
357
|
|
|
* |
358
|
|
|
* @param string $content |
359
|
|
|
* |
360
|
|
|
* @return string |
361
|
|
|
*/ |
362
|
|
|
protected function restoreExtractedData($content) |
363
|
|
|
{ |
364
|
|
|
if (!$this->extracted) { |
|
|
|
|
365
|
|
|
// nothing was extracted, nothing to restore |
366
|
|
|
return $content; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
$content = strtr($content, $this->extracted); |
370
|
|
|
|
371
|
|
|
$this->extracted = array(); |
372
|
|
|
|
373
|
|
|
return $content; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Check if the path is a regular file and can be read. |
378
|
|
|
* |
379
|
|
|
* @param string $path |
380
|
|
|
* |
381
|
|
|
* @return bool |
382
|
|
|
*/ |
383
|
|
|
protected function canImportFile($path) |
384
|
|
|
{ |
385
|
|
|
return strlen($path) < PHP_MAXPATHLEN && is_file($path) && is_readable($path); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Attempts to open file specified by $path for writing. |
390
|
|
|
* |
391
|
|
|
* @param string $path The path to the file. |
392
|
|
|
* |
393
|
|
|
* @return resource Specifier for the target file. |
394
|
|
|
* |
395
|
|
|
* @throws IOException |
396
|
|
|
*/ |
397
|
|
|
protected function openFileForWriting($path) |
398
|
|
|
{ |
399
|
|
|
if (($handler = @fopen($path, 'w')) === false) { |
400
|
|
|
throw new IOException('The file "'.$path.'" could not be opened for writing. Check if PHP has enough permissions.'); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
return $handler; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. |
408
|
|
|
* |
409
|
|
|
* @param resource $handler The resource to write to. |
410
|
|
|
* @param string $content The content to write. |
411
|
|
|
* @param string $path The path to the file (for exception printing only). |
412
|
|
|
* |
413
|
|
|
* @throws IOException |
414
|
|
|
*/ |
415
|
|
|
protected function writeToFile($handler, $content, $path = '') |
416
|
|
|
{ |
417
|
|
|
if (($result = @fwrite($handler, $content)) === false || ($result < strlen($content))) { |
418
|
|
|
throw new IOException('The file "'.$path.'" could not be written to. Check your disk space and file permissions.'); |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: