1 | <?php |
||
17 | abstract class Minify |
||
18 | { |
||
19 | /** |
||
20 | * The data to be minified. |
||
21 | * |
||
22 | * @var string[] |
||
23 | */ |
||
24 | public $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, ... */) |
||
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, ... */) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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 | * @throws Exception |
||
190 | */ |
||
191 | protected function registerPattern($pattern, $replacement = '') |
||
198 | |||
199 | /** |
||
200 | * We can't "just" run some regular expressions against JavaScript: it's a |
||
201 | * complex language. E.g. having an occurrence of // xyz would be a comment, |
||
202 | * unless it's used within a string. Of you could have something that looks |
||
203 | * like a 'string', but inside a comment. |
||
204 | * The only way to accurately replace these pieces is to traverse the JS one |
||
205 | * character at a time and try to find whatever starts first. |
||
206 | * |
||
207 | * @param string $content The content to replace patterns in. |
||
208 | * |
||
209 | * @return string The (manipulated) content. |
||
210 | */ |
||
211 | protected function replace($content) |
||
282 | |||
283 | /** |
||
284 | * This is where a pattern is matched against $content and the matches |
||
285 | * are replaced by their respective value. |
||
286 | * This function will be called plenty of times, where $content will always |
||
287 | * move up 1 character. |
||
288 | * |
||
289 | * @param string $pattern Pattern to match. |
||
290 | * @param string|callable $replacement Replacement value. |
||
291 | * @param string $content Content to match pattern against. |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | protected function replacePattern($pattern, $replacement, $content) |
||
303 | |||
304 | /** |
||
305 | * Strings are a pattern we need to match, in order to ignore potential |
||
306 | * code-like content inside them, but we just want all of the string |
||
307 | * content to remain untouched. |
||
308 | * |
||
309 | * This method will replace all string content with simple STRING# |
||
310 | * placeholder text, so we've rid all strings from characters that may be |
||
311 | * misinterpreted. Original string content will be saved in $this->extracted |
||
312 | * and after doing all other minifying, we can restore the original content |
||
313 | * via restoreStrings(). |
||
314 | * |
||
315 | * @param string[optional] $chars |
||
316 | */ |
||
317 | protected function extractStrings($chars = '\'"') |
||
355 | |||
356 | /** |
||
357 | * This method will restore all extracted data (strings, regexes) that were |
||
358 | * replaced with placeholder text in extract*(). The original content was |
||
359 | * saved in $this->extracted. |
||
360 | * |
||
361 | * @param string $content |
||
362 | * |
||
363 | * @return string |
||
364 | */ |
||
365 | protected function restoreExtractedData($content) |
||
378 | |||
379 | /** |
||
380 | * |
||
381 | * Check if the path is a regular file and can be read. |
||
382 | * |
||
383 | * @param string $path |
||
384 | * @return bool |
||
385 | */ |
||
386 | protected function canImportFile($path) { |
||
389 | |||
390 | /** |
||
391 | * |
||
392 | * Attempts to open file specified by $path for writing. |
||
393 | * |
||
394 | * @param string $path The path to the file. |
||
395 | * @return resource Specifier for the target file. |
||
396 | * |
||
397 | * @throws IOException |
||
398 | */ |
||
399 | protected function openFileForWriting($path) { |
||
406 | |||
407 | /** |
||
408 | * |
||
409 | * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. |
||
410 | * |
||
411 | * @param resource $handler The resource to write to. |
||
412 | * @param string $content The content to write. |
||
413 | * @param string $path The path to the file (for exception printing only). |
||
414 | * |
||
415 | * @throws IOException |
||
416 | */ |
||
417 | protected function writeToFile($handler, $content, $path = '') { |
||
422 | } |
||
423 |
If you suppress an error, we recommend checking for the error condition explicitly: