1 | <?php |
||
26 | abstract class Minify |
||
27 | { |
||
28 | /** |
||
29 | * The data to be minified. |
||
30 | * |
||
31 | * @var string[] |
||
32 | */ |
||
33 | protected $data = array(); |
||
34 | |||
35 | /** |
||
36 | * Array of patterns to match. |
||
37 | * |
||
38 | * @var string[] |
||
39 | */ |
||
40 | protected $patterns = array(); |
||
41 | |||
42 | /** |
||
43 | * This array will hold content of strings and regular expressions that have |
||
44 | * been extracted from the JS source code, so we can reliably match "code", |
||
45 | * without having to worry about potential "code-like" characters inside. |
||
46 | * |
||
47 | * @var string[] |
||
48 | */ |
||
49 | public $extracted = array(); |
||
50 | |||
51 | /** |
||
52 | * Init the minify class - optionally, code may be passed along already. |
||
53 | */ |
||
54 | public function __construct(/* $data = null, ... */) |
||
61 | |||
62 | /** |
||
63 | * Add a file or straight-up code to be minified. |
||
64 | * |
||
65 | * @param string|string[] $data |
||
66 | * |
||
67 | * @return static |
||
68 | */ |
||
69 | public function add($data /* $data = null, ... */) |
||
101 | |||
102 | /** |
||
103 | * Minify the data & (optionally) saves it to a file. |
||
104 | * |
||
105 | * @param string[optional] $path Path to write the data to |
||
106 | * |
||
107 | * @return string The minified data |
||
108 | */ |
||
109 | public function minify($path = null) |
||
120 | |||
121 | /** |
||
122 | * Minify & gzip the data & (optionally) saves it to a file. |
||
123 | * |
||
124 | * @param string[optional] $path Path to write the data to |
||
125 | * @param int[optional] $level Compression level, from 0 to 9 |
||
126 | * |
||
127 | * @return string The minified & gzipped data |
||
128 | */ |
||
129 | public function gzip($path = null, $level = 9) |
||
141 | |||
142 | /** |
||
143 | * Minify the data & write it to a CacheItemInterface object. |
||
144 | * |
||
145 | * @param CacheItemInterface $item Cache item to write the data to |
||
146 | * |
||
147 | * @return CacheItemInterface Cache item with the minifier data |
||
148 | */ |
||
149 | public function cache(CacheItemInterface $item) |
||
156 | |||
157 | /** |
||
158 | * Minify the data. |
||
159 | * |
||
160 | * @param string[optional] $path Path to write the data to |
||
161 | * |
||
162 | * @return string The minified data |
||
163 | */ |
||
164 | abstract public function execute($path = null); |
||
165 | |||
166 | /** |
||
167 | * Load data. |
||
168 | * |
||
169 | * @param string $data Either a path to a file or the content itself |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | protected function load($data) |
||
187 | |||
188 | /** |
||
189 | * Save to file. |
||
190 | * |
||
191 | * @param string $content The minified data |
||
192 | * @param string $path The path to save the minified data to |
||
193 | * |
||
194 | * @throws IOException |
||
195 | */ |
||
196 | protected function save($content, $path) |
||
204 | |||
205 | /** |
||
206 | * Register a pattern to execute against the source content. |
||
207 | * |
||
208 | * @param string $pattern PCRE pattern |
||
209 | * @param string|callable $replacement Replacement value for matched pattern |
||
210 | */ |
||
211 | protected function registerPattern($pattern, $replacement = '') |
||
218 | |||
219 | /** |
||
220 | * We can't "just" run some regular expressions against JavaScript: it's a |
||
221 | * complex language. E.g. having an occurrence of // xyz would be a comment, |
||
222 | * unless it's used within a string. Of you could have something that looks |
||
223 | * like a 'string', but inside a comment. |
||
224 | * The only way to accurately replace these pieces is to traverse the JS one |
||
225 | * character at a time and try to find whatever starts first. |
||
226 | * |
||
227 | * @param string $content The content to replace patterns in |
||
228 | * |
||
229 | * @return string The (manipulated) content |
||
230 | */ |
||
231 | protected function replace($content) |
||
302 | |||
303 | /** |
||
304 | * This is where a pattern is matched against $content and the matches |
||
305 | * are replaced by their respective value. |
||
306 | * This function will be called plenty of times, where $content will always |
||
307 | * move up 1 character. |
||
308 | * |
||
309 | * @param string $pattern Pattern to match |
||
310 | * @param string|callable $replacement Replacement value |
||
311 | * @param string $content Content to match pattern against |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | protected function replacePattern($pattern, $replacement, $content) |
||
323 | |||
324 | /** |
||
325 | * Strings are a pattern we need to match, in order to ignore potential |
||
326 | * code-like content inside them, but we just want all of the string |
||
327 | * content to remain untouched. |
||
328 | * |
||
329 | * This method will replace all string content with simple STRING# |
||
330 | * placeholder text, so we've rid all strings from characters that may be |
||
331 | * misinterpreted. Original string content will be saved in $this->extracted |
||
332 | * and after doing all other minifying, we can restore the original content |
||
333 | * via restoreStrings(). |
||
334 | * |
||
335 | * @param string[optional] $chars |
||
336 | * @param string[optional] $placeholderPrefix |
||
337 | */ |
||
338 | protected function extractStrings($chars = '\'"', $placeholderPrefix = '') |
||
375 | |||
376 | /** |
||
377 | * This method will restore all extracted data (strings, regexes) that were |
||
378 | * replaced with placeholder text in extract*(). The original content was |
||
379 | * saved in $this->extracted. |
||
380 | * |
||
381 | * @param string $content |
||
382 | * |
||
383 | * @return string |
||
384 | */ |
||
385 | protected function restoreExtractedData($content) |
||
398 | |||
399 | /** |
||
400 | * Check if the path is a regular file and can be read. |
||
401 | * |
||
402 | * @param string $path |
||
403 | * |
||
404 | * @return bool |
||
405 | */ |
||
406 | protected function canImportFile($path) |
||
420 | |||
421 | /** |
||
422 | * Attempts to open file specified by $path for writing. |
||
423 | * |
||
424 | * @param string $path The path to the file |
||
425 | * |
||
426 | * @return resource Specifier for the target file |
||
427 | * |
||
428 | * @throws IOException |
||
429 | */ |
||
430 | protected function openFileForWriting($path) |
||
438 | |||
439 | /** |
||
440 | * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. |
||
441 | * |
||
442 | * @param resource $handler The resource to write to |
||
443 | * @param string $content The content to write |
||
444 | * @param string $path The path to the file (for exception printing only) |
||
445 | * |
||
446 | * @throws IOException |
||
447 | */ |
||
448 | protected function writeToFile($handler, $content, $path = '') |
||
454 | } |
||
455 |
If you suppress an error, we recommend checking for the error condition explicitly: