1 | <?php |
||
17 | abstract class Minify |
||
18 | { |
||
19 | /** |
||
20 | * Leave comment in compressed file |
||
21 | * if it starts with /*! or contains @license or @preserve tags. |
||
22 | * |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $leavePreservedComments = false; |
||
26 | /** |
||
27 | * The data to be minified. |
||
28 | * |
||
29 | * @var string[] |
||
30 | */ |
||
31 | protected $data = array(); |
||
32 | |||
33 | /** |
||
34 | * Array of patterns to match. |
||
35 | * |
||
36 | * @var string[] |
||
37 | */ |
||
38 | protected $patterns = array(); |
||
39 | |||
40 | /** |
||
41 | * This array will hold content of strings and regular expressions that have |
||
42 | * been extracted from the JS source code, so we can reliably match "code", |
||
43 | * without having to worry about potential "code-like" characters inside. |
||
44 | * |
||
45 | * @var string[] |
||
46 | */ |
||
47 | public $extracted = array(); |
||
48 | |||
49 | /** |
||
50 | * Init the minify class - optionally, code may be passed along already. |
||
51 | */ |
||
52 | public function __construct(/* $data = null, ... */) |
||
59 | |||
60 | /** |
||
61 | * Add a file or straight-up code to be minified. |
||
62 | * |
||
63 | * @param string|string[] $data |
||
64 | * |
||
65 | * @return static |
||
66 | */ |
||
67 | public function add($data /* $data = null, ... */) |
||
99 | |||
100 | /** |
||
101 | * Minify the data & (optionally) saves it to a file. |
||
102 | * |
||
103 | * @param string[optional] $path Path to write the data to |
||
104 | * |
||
105 | * @return string The minified data |
||
106 | */ |
||
107 | public function minify($path = null) |
||
118 | |||
119 | /** |
||
120 | * Minify & gzip the data & (optionally) saves it to a file. |
||
121 | * |
||
122 | * @param string[optional] $path Path to write the data to |
||
123 | * @param int[optional] $level Compression level, from 0 to 9 |
||
124 | * |
||
125 | * @return string The minified & gzipped data |
||
126 | */ |
||
127 | public function gzip($path = null, $level = 9) |
||
139 | |||
140 | /** |
||
141 | * Minify the data & write it to a CacheItemInterface object. |
||
142 | * |
||
143 | * @param CacheItemInterface $item Cache item to write the data to |
||
144 | * |
||
145 | * @return CacheItemInterface Cache item with the minifier data |
||
146 | */ |
||
147 | public function cache(CacheItemInterface $item) |
||
154 | |||
155 | /** |
||
156 | * Minify the data. |
||
157 | * |
||
158 | * @param string[optional] $path Path to write the data to |
||
159 | * |
||
160 | * @return string The minified data |
||
161 | */ |
||
162 | abstract public function execute($path = null); |
||
163 | |||
164 | /** |
||
165 | * Load data. |
||
166 | * |
||
167 | * @param string $data Either a path to a file or the content itself |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | protected function load($data) |
||
185 | |||
186 | /** |
||
187 | * Save to file. |
||
188 | * |
||
189 | * @param string $content The minified data |
||
190 | * @param string $path The path to save the minified data to |
||
191 | * |
||
192 | * @throws IOException |
||
193 | */ |
||
194 | protected function save($content, $path) |
||
202 | |||
203 | /** |
||
204 | * Register a pattern to execute against the source content. |
||
205 | * |
||
206 | * @param string $pattern PCRE pattern |
||
207 | * @param string|callable $replacement Replacement value for matched pattern |
||
208 | */ |
||
209 | protected function registerPattern($pattern, $replacement = '') |
||
216 | |||
217 | /** |
||
218 | * We can't "just" run some regular expressions against JavaScript: it's a |
||
219 | * complex language. E.g. having an occurrence of // xyz would be a comment, |
||
220 | * unless it's used within a string. Of you could have something that looks |
||
221 | * like a 'string', but inside a comment. |
||
222 | * The only way to accurately replace these pieces is to traverse the JS one |
||
223 | * character at a time and try to find whatever starts first. |
||
224 | * |
||
225 | * @param string $content The content to replace patterns in |
||
226 | * |
||
227 | * @return string The (manipulated) content |
||
228 | */ |
||
229 | protected function replace($content) |
||
300 | |||
301 | /** |
||
302 | * This is where a pattern is matched against $content and the matches |
||
303 | * are replaced by their respective value. |
||
304 | * This function will be called plenty of times, where $content will always |
||
305 | * move up 1 character. |
||
306 | * |
||
307 | * @param string $pattern Pattern to match |
||
308 | * @param string|callable $replacement Replacement value |
||
309 | * @param string $content Content to match pattern against |
||
310 | * |
||
311 | * @return string |
||
312 | */ |
||
313 | protected function replacePattern($pattern, $replacement, $content) |
||
321 | |||
322 | /** |
||
323 | * Strings are a pattern we need to match, in order to ignore potential |
||
324 | * code-like content inside them, but we just want all of the string |
||
325 | * content to remain untouched. |
||
326 | * |
||
327 | * This method will replace all string content with simple STRING# |
||
328 | * placeholder text, so we've rid all strings from characters that may be |
||
329 | * misinterpreted. Original string content will be saved in $this->extracted |
||
330 | * and after doing all other minifying, we can restore the original content |
||
331 | * via restoreStrings(). |
||
332 | * |
||
333 | * @param string[optional] $chars |
||
334 | */ |
||
335 | protected function extractStrings($chars = '\'"') |
||
372 | |||
373 | /** |
||
374 | * This method will restore all extracted data (strings, regexes) that were |
||
375 | * replaced with placeholder text in extract*(). The original content was |
||
376 | * saved in $this->extracted. |
||
377 | * |
||
378 | * @param string $content |
||
379 | * |
||
380 | * @return string |
||
381 | */ |
||
382 | protected function restoreExtractedData($content) |
||
395 | |||
396 | /** |
||
397 | * Check if the path is a regular file and can be read. |
||
398 | * |
||
399 | * @param string $path |
||
400 | * |
||
401 | * @return bool |
||
402 | */ |
||
403 | protected function canImportFile($path) |
||
407 | |||
408 | /** |
||
409 | * Attempts to open file specified by $path for writing. |
||
410 | * |
||
411 | * @param string $path The path to the file |
||
412 | * |
||
413 | * @return resource Specifier for the target file |
||
414 | * |
||
415 | * @throws IOException |
||
416 | */ |
||
417 | protected function openFileForWriting($path) |
||
425 | |||
426 | /** |
||
427 | * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. |
||
428 | * |
||
429 | * @param resource $handler The resource to write to |
||
430 | * @param string $content The content to write |
||
431 | * @param string $path The path to the file (for exception printing only) |
||
432 | * |
||
433 | * @throws IOException |
||
434 | */ |
||
435 | protected function writeToFile($handler, $content, $path = '') |
||
441 | |||
442 | /** |
||
443 | * Set Leave comment in compressed file parameter |
||
444 | * If true, minify will leave comment in compressed file |
||
445 | * if it starts with /*! or contains @license or @preserve tags. |
||
446 | * If false, all comments will be removed. |
||
447 | * |
||
448 | * @param bool $value |
||
449 | */ |
||
450 | public function setLeavePreservedComments($value) |
||
454 | } |
||
455 |
If you suppress an error, we recommend checking for the error condition explicitly: