1 | <?php |
||
16 | abstract class Minify |
||
17 | { |
||
18 | /** |
||
19 | * The data to be minified. |
||
20 | * |
||
21 | * @var string[] |
||
22 | */ |
||
23 | protected $data = array(); |
||
24 | |||
25 | /** |
||
26 | * Array of patterns to match. |
||
27 | * |
||
28 | * @var string[] |
||
29 | */ |
||
30 | protected $patterns = array(); |
||
31 | |||
32 | /** |
||
33 | * This array will hold content of strings and regular expressions that have |
||
34 | * been extracted from the JS source code, so we can reliably match "code", |
||
35 | * without having to worry about potential "code-like" characters inside. |
||
36 | * |
||
37 | * @var string[] |
||
38 | */ |
||
39 | public $extracted = array(); |
||
40 | |||
41 | /** |
||
42 | * Init the minify class - optionally, code may be passed along already. |
||
43 | */ |
||
44 | public function __construct(/* $data = null, ... */) |
||
51 | |||
52 | /** |
||
53 | * Add a file or straight-up code to be minified. |
||
54 | * |
||
55 | * @param string $data |
||
56 | */ |
||
57 | public function add($data /* $data = null, ... */) |
||
78 | |||
79 | /** |
||
80 | * Minify the data & (optionally) saves it to a file. |
||
81 | * |
||
82 | * @param string[optional] $path Path to write the data to. |
||
83 | * |
||
84 | * @return string The minified data. |
||
85 | */ |
||
86 | public function minify($path = null) |
||
97 | |||
98 | /** |
||
99 | * Minify & gzip the data & (optionally) saves it to a file. |
||
100 | * |
||
101 | * @param string[optional] $path Path to write the data to. |
||
102 | * @param int[optional] $level Compression level, from 0 to 9. |
||
103 | * |
||
104 | * @return string The minified & gzipped data. |
||
105 | */ |
||
106 | public function gzip($path = null, $level = 9) |
||
118 | |||
119 | /** |
||
120 | * Minify the data & write it to a CacheItemInterface object. |
||
121 | * |
||
122 | * @param CacheItemInterface $item Cache item to write the data to. |
||
123 | * |
||
124 | * @return CacheItemInterface Cache item with the minifier data. |
||
125 | */ |
||
126 | public function cache(CacheItemInterface $item) |
||
133 | |||
134 | /** |
||
135 | * Minify the data. |
||
136 | * |
||
137 | * @param string[optional] $path Path to write the data to. |
||
138 | * |
||
139 | * @return string The minified data. |
||
140 | */ |
||
141 | abstract public function execute($path = null); |
||
142 | |||
143 | /** |
||
144 | * Load data. |
||
145 | * |
||
146 | * @param string $data Either a path to a file or the content itself. |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | protected function load($data) |
||
164 | |||
165 | /** |
||
166 | * Save to file. |
||
167 | * |
||
168 | * @param string $content The minified data. |
||
169 | * @param string $path The path to save the minified data to. |
||
170 | * |
||
171 | * @throws Exception |
||
172 | */ |
||
173 | protected function save($content, $path) |
||
181 | |||
182 | /** |
||
183 | * Register a pattern to execute against the source content. |
||
184 | * |
||
185 | * @param string $pattern PCRE pattern. |
||
186 | * @param string|callable $replacement Replacement value for matched pattern. |
||
187 | * |
||
188 | * @throws Exception |
||
189 | */ |
||
190 | protected function registerPattern($pattern, $replacement = '') |
||
197 | |||
198 | /** |
||
199 | * We can't "just" run some regular expressions against JavaScript: it's a |
||
200 | * complex language. E.g. having an occurrence of // xyz would be a comment, |
||
201 | * unless it's used within a string. Of you could have something that looks |
||
202 | * like a 'string', but inside a comment. |
||
203 | * The only way to accurately replace these pieces is to traverse the JS one |
||
204 | * character at a time and try to find whatever starts first. |
||
205 | * |
||
206 | * @param string $content The content to replace patterns in. |
||
207 | * |
||
208 | * @return string The (manipulated) content. |
||
209 | */ |
||
210 | protected function replace($content) |
||
281 | |||
282 | /** |
||
283 | * This is where a pattern is matched against $content and the matches |
||
284 | * are replaced by their respective value. |
||
285 | * This function will be called plenty of times, where $content will always |
||
286 | * move up 1 character. |
||
287 | * |
||
288 | * @param string $pattern Pattern to match. |
||
289 | * @param string|callable $replacement Replacement value. |
||
290 | * @param string $content Content to match pattern against. |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | protected function replacePattern($pattern, $replacement, $content) |
||
302 | |||
303 | /** |
||
304 | * Strings are a pattern we need to match, in order to ignore potential |
||
305 | * code-like content inside them, but we just want all of the string |
||
306 | * content to remain untouched. |
||
307 | * |
||
308 | * This method will replace all string content with simple STRING# |
||
309 | * placeholder text, so we've rid all strings from characters that may be |
||
310 | * misinterpreted. Original string content will be saved in $this->extracted |
||
311 | * and after doing all other minifying, we can restore the original content |
||
312 | * via restoreStrings(). |
||
313 | * |
||
314 | * @param string[optional] $chars |
||
315 | */ |
||
316 | protected function extractStrings($chars = '\'"') |
||
354 | |||
355 | /** |
||
356 | * This method will restore all extracted data (strings, regexes) that were |
||
357 | * replaced with placeholder text in extract*(). The original content was |
||
358 | * saved in $this->extracted. |
||
359 | * |
||
360 | * @param string $content |
||
361 | * |
||
362 | * @return string |
||
363 | */ |
||
364 | protected function restoreExtractedData($content) |
||
377 | |||
378 | /** |
||
379 | * |
||
380 | * Check if the path is a regular file and can be read. |
||
381 | * |
||
382 | * @param string $path |
||
383 | * @return bool |
||
384 | */ |
||
385 | protected function canImportFile($path) { |
||
388 | |||
389 | /** |
||
390 | * |
||
391 | * Attempts to open file specified by $path for writing. |
||
392 | * |
||
393 | * @param string $path The path to the file. |
||
394 | * @return resource Specifier for the target file. |
||
395 | * |
||
396 | * @throws Exception |
||
397 | */ |
||
398 | protected function openFileForWriting($path) { |
||
405 | |||
406 | /** |
||
407 | * |
||
408 | * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. |
||
409 | * |
||
410 | * @param resource $handler The resource to write to. |
||
411 | * @param string $content The content to write. |
||
412 | * @param string $path The path to the file (for exception printing only). |
||
413 | * |
||
414 | * @throws Exception |
||
415 | */ |
||
416 | protected function writeToFile($handler, $content, $path = '') { |
||
421 | } |
||
422 |
If you suppress an error, we recommend checking for the error condition explicitly: