1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2014-2017, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs\Page; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class includes few methods used for processing CSS, JS and HTML files before putting into cache |
12
|
|
|
* |
13
|
|
|
* This is because CSS and HTML files may include other CSS, JS files, images, fonts and so on with absolute and relative paths. |
14
|
|
|
* Methods of this class handle all this assets, applies basic minification to CSS and JS files and produce single resulting file, nested files are also copied |
15
|
|
|
* to target directory and processed if needed. |
16
|
|
|
*/ |
17
|
|
|
class Assets_processing { |
18
|
|
|
/** |
19
|
|
|
* Analyses file for images, fonts and css links and include they content into single resulting css file. |
20
|
|
|
* |
21
|
|
|
* Supports next file extensions for possible assets: |
22
|
|
|
* jpeg, jpe, jpg, gif, png, ttf, ttc, svg, svgz, woff, css |
23
|
|
|
* |
24
|
|
|
* @param string $data Content of processed file |
25
|
|
|
* @param string $file Path to file, that contains specified in previous parameter content |
26
|
|
|
* @param string $target_directory_path Target directory for resulting combined files |
27
|
|
|
* @param string[] $not_embedded_resources Some resources like images and fonts might not be embedded into resulting CSS because of their size |
28
|
|
|
* |
29
|
|
|
* @return string $data |
30
|
|
|
*/ |
31
|
10 |
|
public static function css ($data, $file, $target_directory_path = PUBLIC_CACHE, &$not_embedded_resources = []) { |
32
|
10 |
|
$dir = dirname($file); |
33
|
|
|
/** |
34
|
|
|
* Remove comments, tabs and new lines |
35
|
|
|
*/ |
36
|
10 |
|
$data = preg_replace('#(/\*.*?\*/)|\t|\n|\r#s', ' ', $data); |
37
|
|
|
/** |
38
|
|
|
* Remove unnecessary spaces |
39
|
|
|
*/ |
40
|
10 |
|
$data = preg_replace('/\s*([,;>{}(])\s*/', '$1', $data); |
41
|
10 |
|
$data = preg_replace('/\s+/', ' ', $data); |
42
|
|
|
/** |
43
|
|
|
* Return spaces required in media queries |
44
|
|
|
*/ |
45
|
10 |
|
$data = preg_replace('/\s(and|or)\(/', ' $1 (', $data); |
46
|
|
|
/** |
47
|
|
|
* Duplicated semicolons |
48
|
|
|
*/ |
49
|
10 |
|
$data = preg_replace('/;+/m', ';', $data); |
50
|
|
|
/** |
51
|
|
|
* Minify rgb colors declarations |
52
|
|
|
*/ |
53
|
10 |
|
$data = preg_replace_callback( |
54
|
10 |
|
'/rgb\(([0-9,.]+)\)/i', |
55
|
|
|
function ($rgb) { |
56
|
6 |
|
$rgb = explode(',', $rgb[1]); |
57
|
|
|
return |
58
|
|
|
'#'. |
59
|
6 |
|
str_pad(dechex($rgb[0]), 2, 0, STR_PAD_LEFT). |
60
|
6 |
|
str_pad(dechex($rgb[1]), 2, 0, STR_PAD_LEFT). |
61
|
6 |
|
str_pad(dechex($rgb[2]), 2, 0, STR_PAD_LEFT); |
62
|
10 |
|
}, |
63
|
|
|
$data |
64
|
|
|
); |
65
|
|
|
/** |
66
|
|
|
* Minify repeated colors declarations |
67
|
|
|
*/ |
68
|
10 |
|
$data = preg_replace('/#([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3/i', '#$1$2$3', $data); |
69
|
|
|
/** |
70
|
|
|
* Remove unnecessary zeros |
71
|
|
|
*/ |
72
|
10 |
|
$data = preg_replace('/(\D)0\.(\d+)/i', '$1.$2', $data); |
73
|
|
|
/** |
74
|
|
|
* Unnecessary spaces around colons (should have whitespace character after, otherwise `.class :disabled` will be handled incorrectly) |
75
|
|
|
*/ |
76
|
10 |
|
$data = preg_replace('/\s*:\s+/', ':', $data); |
77
|
|
|
/** |
78
|
|
|
* Assets processing |
79
|
|
|
*/ |
80
|
|
|
// TODO: replace by loop, track duplicated stuff that are subject to inlining and if they appear more than once, don't inline them |
81
|
10 |
|
$data = preg_replace_callback( |
82
|
10 |
|
'/url\((.*)\)|@import\s*(?:url\()?\s*([\'"].*[\'"])\s*\)??.*;/U', |
83
|
|
|
function ($match) use ($dir, $target_directory_path, &$not_embedded_resources) { |
84
|
10 |
|
$path_matched = $match[2] ?? $match[1]; |
85
|
10 |
|
$path = trim($path_matched, '\'" '); |
86
|
10 |
|
$link = explode('?', $path, 2)[0]; |
87
|
10 |
|
if (!static::is_relative_path_and_exists($link, $dir)) { |
88
|
4 |
|
return $match[0]; |
89
|
|
|
} |
90
|
10 |
|
$extension = file_extension($link); |
91
|
10 |
|
$absolute_path = static::absolute_path($link, $dir); |
92
|
10 |
|
$content = file_get_contents($absolute_path); |
93
|
|
|
/** |
94
|
|
|
* Process recursively CSS imports, but ignore non embedded resources there |
95
|
|
|
*/ |
96
|
10 |
|
if ($extension == 'css' && @$match[2]) { |
97
|
6 |
|
$content = static::css($content, $absolute_path, $target_directory_path); |
98
|
|
|
} |
99
|
10 |
|
$filename = static::file_put_contents_with_hash($target_directory_path, $extension, $content); |
100
|
10 |
|
if (strpos($path, '?') === false) { |
101
|
6 |
|
$not_embedded_resources[] = str_replace(getcwd(), '', "$target_directory_path/$filename"); |
102
|
|
|
} |
103
|
10 |
|
return str_replace($path_matched, "'./$filename'", $match[0]); |
104
|
10 |
|
}, |
105
|
|
|
$data |
106
|
|
|
); |
107
|
10 |
|
return trim($data); |
108
|
|
|
} |
109
|
|
|
/** |
110
|
|
|
* Put `$content` into `$dir` where filename is `md5($content)` with specified extension |
111
|
|
|
* |
112
|
|
|
* @param string $dir |
113
|
|
|
* @param string $extension |
114
|
|
|
* @param string $content |
115
|
|
|
* |
116
|
|
|
* @return string Filename (without full path) |
117
|
|
|
*/ |
118
|
10 |
|
protected static function file_put_contents_with_hash ($dir, $extension, $content) { |
119
|
10 |
|
$hash = md5($content); |
120
|
10 |
|
file_put_contents("$dir/$hash.$extension", $content, LOCK_EX | FILE_BINARY); |
121
|
10 |
|
return "$hash.$extension"; |
122
|
|
|
} |
123
|
|
|
/** |
124
|
|
|
* Simple and fast JS minification |
125
|
|
|
* |
126
|
|
|
* @param string $data |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
10 |
|
public static function js ($data) { |
131
|
|
|
/** |
132
|
|
|
* Split into array of lines |
133
|
|
|
*/ |
134
|
10 |
|
$data = explode("\n", $data); |
135
|
|
|
/** |
136
|
|
|
* Flag that is `true` when inside comment |
137
|
|
|
*/ |
138
|
10 |
|
$in_comment = false; |
139
|
10 |
|
$continue_after_position = -1; |
140
|
10 |
|
foreach ($data as $index => &$current_line) { |
141
|
10 |
|
if ($continue_after_position >= $index) { |
142
|
2 |
|
continue; |
143
|
|
|
} |
144
|
10 |
|
$next_line = isset($data[$index + 1]) ? trim($data[$index + 1]) : ''; |
145
|
|
|
/** |
146
|
|
|
* Remove starting and trailing spaces |
147
|
|
|
*/ |
148
|
10 |
|
$current_line = trim($current_line); |
149
|
|
|
/** |
150
|
|
|
* Remove single-line comments |
151
|
|
|
*/ |
152
|
10 |
|
if (mb_strpos($current_line, '//') === 0) { |
153
|
10 |
|
$current_line = ''; |
154
|
10 |
|
continue; |
155
|
|
|
} |
156
|
|
|
/** |
157
|
|
|
* Starts with multi-line comment |
158
|
|
|
*/ |
159
|
10 |
|
if (mb_strpos($current_line, '/*') === 0) { |
160
|
10 |
|
$in_comment = true; |
161
|
|
|
} |
162
|
10 |
|
if (!$in_comment) { |
163
|
10 |
|
$backticks_position = strpos($current_line, '`'); |
164
|
|
|
/** |
165
|
|
|
* Handling template strings can be tricky (since they might be multi-line), so let's fast-forward to the last backticks position and continue |
166
|
|
|
* from there |
167
|
|
|
*/ |
168
|
10 |
|
if ($backticks_position !== false) { |
169
|
6 |
|
$last_item_with_backticks = array_keys( |
170
|
|
|
array_filter( |
171
|
|
|
$data, |
172
|
|
|
function ($d) { |
173
|
6 |
|
return strpos($d, '`') !== false; |
174
|
6 |
|
} |
175
|
|
|
) |
176
|
|
|
); |
177
|
6 |
|
$last_item_with_backticks = array_pop($last_item_with_backticks); |
178
|
6 |
|
if ($last_item_with_backticks > $index) { |
179
|
2 |
|
$continue_after_position = $last_item_with_backticks; |
180
|
2 |
|
continue; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
/** |
184
|
|
|
* Add new line at the end if only needed |
185
|
|
|
*/ |
186
|
10 |
|
if (static::new_line_needed($current_line, $next_line)) { |
187
|
6 |
|
$current_line .= "\n"; |
188
|
|
|
} |
189
|
|
|
/** |
190
|
|
|
* Single-line comment |
191
|
|
|
*/ |
192
|
10 |
|
$current_line = preg_replace('#^\s*//[^\'"]+$#', '', $current_line); |
193
|
|
|
/** |
194
|
|
|
* If we are not sure - just add new line afterwards |
195
|
|
|
*/ |
196
|
10 |
|
$current_line = preg_replace('#//.*$#', "\\0\n", $current_line); |
197
|
|
|
} else { |
198
|
|
|
/** |
199
|
|
|
* End of multi-line comment |
200
|
|
|
*/ |
201
|
10 |
|
if (strpos($current_line, '*/') !== false) { |
202
|
10 |
|
$current_line = explode('*/', $current_line)[1]; |
203
|
10 |
|
$in_comment = false; |
204
|
|
|
} else { |
205
|
10 |
|
$current_line = ''; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
10 |
|
$data = implode('', $data); |
210
|
10 |
|
$data = str_replace('</script>', '<\/script>', $data); |
211
|
10 |
|
return trim($data, ';').';'; |
212
|
|
|
} |
213
|
|
|
/** |
214
|
|
|
* @param string $current_line |
215
|
|
|
* @param string $next_line |
216
|
|
|
* |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
10 |
|
protected static function new_line_needed ($current_line, $next_line) { |
220
|
|
|
/** |
221
|
|
|
* Set of symbols that are safe to be concatenated without new line with anything else |
222
|
|
|
*/ |
223
|
|
|
$regexp = /** @lang PhpRegExp */ |
224
|
10 |
|
'[:;,.+\-*/{}?><^\'"\[\]=&(]'; |
225
|
|
|
return |
226
|
10 |
|
$current_line && |
227
|
10 |
|
$next_line && |
228
|
10 |
|
!preg_match("#$regexp\$#", $current_line) && |
229
|
10 |
|
!preg_match("#^$regexp#", $next_line); |
230
|
|
|
} |
231
|
|
|
/** |
232
|
|
|
* Analyses file for scripts and styles, combines them into resulting files in order to optimize loading process |
233
|
|
|
* (files with combined scripts and styles will be created) |
234
|
|
|
* |
235
|
|
|
* @param string $data Content of processed file |
236
|
|
|
* @param string $file Path to file, that contains specified in previous parameter content |
237
|
|
|
* @param string $target_directory_path Target directory for resulting combined files |
238
|
|
|
* @param bool $vulcanization Whether to put combined files separately or to make included assets built-in (vulcanization) |
239
|
|
|
* @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
8 |
|
public static function html ($data, $file, $target_directory_path, $vulcanization, &$not_embedded_resources = []) { |
244
|
8 |
|
static::html_process_links_and_styles($data, $file, $target_directory_path, $vulcanization, $not_embedded_resources); |
245
|
8 |
|
static::html_process_scripts($data, $file, $target_directory_path, $vulcanization, $not_embedded_resources); |
246
|
|
|
// Removing HTML comments (those that are mostly likely comments, to avoid problems) |
247
|
8 |
|
$data = preg_replace_callback( |
248
|
8 |
|
'/^\s*<!--([^>-].*[^-])?-->/Ums', |
249
|
8 |
|
function ($matches) { |
250
|
8 |
|
return mb_strpos('--', $matches[1]) === false ? '' : $matches[0]; |
251
|
8 |
|
}, |
252
|
|
|
$data |
253
|
|
|
); |
254
|
8 |
|
return preg_replace("/\n+/", "\n", $data); |
255
|
|
|
} |
256
|
|
|
/** |
257
|
|
|
* @param string $data Content of processed file |
258
|
|
|
* @param string $file Path to file, that contains specified in previous parameter content |
259
|
|
|
* @param string $target_directory_path Target directory for resulting combined files |
260
|
|
|
* @param bool $vulcanization Whether to put combined files separately or to make included assets built-in (vulcanization) |
261
|
|
|
* @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
262
|
|
|
*/ |
263
|
8 |
|
protected static function html_process_scripts (&$data, $file, $target_directory_path, $vulcanization, &$not_embedded_resources) { |
264
|
8 |
|
if (!preg_match_all('/<script(.*)<\/script>/Uims', $data, $scripts)) { |
265
|
8 |
|
return; |
266
|
|
|
} |
267
|
8 |
|
$scripts_content = ''; |
268
|
8 |
|
$scripts_to_replace = []; |
269
|
8 |
|
$dir = dirname($file); |
270
|
8 |
|
foreach ($scripts[1] as $index => $script) { |
271
|
8 |
|
$script = explode('>', $script, 2); |
272
|
8 |
|
if (preg_match('/src\s*=\s*[\'"](.*)[\'"]/Uims', $script[0], $url)) { |
273
|
8 |
|
$url = $url[1]; |
274
|
8 |
|
if (!static::is_relative_path_and_exists($url, $dir)) { |
275
|
4 |
|
continue; |
276
|
|
|
} |
277
|
8 |
|
$scripts_to_replace[] = $scripts[0][$index]; |
278
|
8 |
|
$scripts_content .= file_get_contents("$dir/$url").";\n"; |
279
|
|
|
} else { |
280
|
8 |
|
$scripts_to_replace[] = $scripts[0][$index]; |
281
|
8 |
|
$scripts_content .= "$script[1];\n"; |
282
|
|
|
} |
283
|
|
|
} |
284
|
8 |
|
$scripts_content = static::js($scripts_content); |
285
|
8 |
|
if (!$scripts_to_replace) { |
286
|
4 |
|
return; |
287
|
|
|
} |
288
|
|
|
// Remove all scripts |
289
|
8 |
|
$data = str_replace($scripts_to_replace, '', $data); |
290
|
|
|
/** |
291
|
|
|
* If vulcanization is not used - put contents into separate file, and put link to it, otherwise put minified content back |
292
|
|
|
*/ |
293
|
8 |
|
if (!$vulcanization) { |
294
|
2 |
|
$filename = static::file_put_contents_with_hash($target_directory_path, 'js', $scripts_content); |
295
|
|
|
// Add script with combined content file to the end |
296
|
2 |
|
$data .= "<script src=\"./$filename\"></script>"; |
297
|
2 |
|
$not_embedded_resources[] = str_replace(getcwd(), '', "$target_directory_path/$filename"); |
298
|
|
|
} else { |
299
|
|
|
// Add combined content inline script to the end |
300
|
6 |
|
$data .= "<script>$scripts_content</script>"; |
301
|
|
|
} |
302
|
8 |
|
} |
303
|
|
|
/** |
304
|
|
|
* @param string $data Content of processed file |
305
|
|
|
* @param string $file Path to file, that contains specified in previous parameter content |
306
|
|
|
* @param string $target_directory_path Target directory for resulting combined files |
307
|
|
|
* @param bool $vulcanization Whether to put combined files separately or to make included assets built-in (vulcanization) |
308
|
|
|
* @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
309
|
|
|
*/ |
310
|
8 |
|
protected static function html_process_links_and_styles (&$data, $file, $target_directory_path, $vulcanization, &$not_embedded_resources) { |
311
|
8 |
|
if (!preg_match_all('/<link(.*)>|<style(.*)<\/style>/Uims', $data, $links_and_styles)) { |
312
|
8 |
|
return; |
313
|
|
|
} |
314
|
8 |
|
$dir = dirname($file); |
315
|
8 |
|
foreach ($links_and_styles[1] as $index => $link) { |
316
|
|
|
/** |
317
|
|
|
* For plain styles we do not do anything fancy besides minifying its sources (no rearrangement or anything like that) |
318
|
|
|
*/ |
319
|
8 |
|
if (mb_strpos($links_and_styles[0][$index], '</style>') > 0) { |
320
|
8 |
|
$content = explode('>', $links_and_styles[2][$index], 2)[1]; |
321
|
8 |
|
$data = str_replace( |
322
|
|
|
$content, |
323
|
8 |
|
static::css($content, $file, $target_directory_path, $not_embedded_resources), |
324
|
|
|
$data |
325
|
|
|
); |
326
|
8 |
|
continue; |
327
|
|
|
} |
328
|
8 |
|
if (!static::has_relative_href($link, $url, $dir)) { |
329
|
4 |
|
continue; |
330
|
|
|
} |
331
|
8 |
|
$import = preg_match('/rel\s*=\s*[\'"]import[\'"]/Uim', $link); |
332
|
|
|
/** |
333
|
|
|
* CSS imports are available in Polymer alongside with HTML imports |
334
|
|
|
*/ |
335
|
8 |
|
$css_import = $import && preg_match('/type\s*=\s*[\'"]css[\'"]/Uim', $link); |
336
|
8 |
|
$stylesheet = preg_match('/rel\s*=\s*[\'"]stylesheet[\'"]/Uim', $link); |
337
|
|
|
/** |
338
|
|
|
* TODO: Polymer only supports `custom-style > style`, but no `link`-based counterpart, so we can't provide CSP-compatibility in general, |
339
|
|
|
* thus always inlining styles into HTML |
340
|
|
|
*/ |
341
|
8 |
|
if ($css_import || $stylesheet) { |
342
|
|
|
/** |
343
|
|
|
* If content is link to CSS file |
344
|
|
|
*/ |
345
|
8 |
|
$css = static::css( |
346
|
8 |
|
file_get_contents("$dir/$url"), |
347
|
8 |
|
"$dir/$url", |
348
|
|
|
$target_directory_path, |
349
|
|
|
$not_embedded_resources |
350
|
|
|
); |
351
|
8 |
|
$data = preg_replace( |
352
|
8 |
|
'/'.$links_and_styles[0][$index].'.*<template>/Uims', |
353
|
8 |
|
"<template><style>$css</style>", |
354
|
|
|
$data |
355
|
|
|
); |
356
|
4 |
|
} elseif ($import) { |
357
|
|
|
/** |
358
|
|
|
* If content is HTML import |
359
|
|
|
*/ |
360
|
4 |
|
$data = str_replace( |
361
|
4 |
|
$links_and_styles[0][$index], |
362
|
4 |
|
static::html( |
363
|
4 |
|
file_get_contents("$dir/$url"), |
364
|
8 |
|
"$dir/$url", |
365
|
|
|
$target_directory_path, |
366
|
|
|
$vulcanization, |
367
|
|
|
$not_embedded_resources |
368
|
|
|
), |
369
|
|
|
$data |
370
|
|
|
); |
371
|
|
|
} |
372
|
|
|
} |
373
|
8 |
|
} |
374
|
|
|
/** |
375
|
|
|
* @param string $link |
376
|
|
|
* @param string $url |
377
|
|
|
* @param string $dir |
378
|
|
|
* |
379
|
|
|
* @return bool |
380
|
|
|
*/ |
381
|
8 |
|
protected static function has_relative_href ($link, &$url, $dir) { |
382
|
|
|
$result = |
383
|
8 |
|
$link && |
384
|
8 |
|
preg_match('/href\s*=\s*[\'"](.*)[\'"]/Uims', $link, $url); |
385
|
8 |
|
if ($result && static::is_relative_path_and_exists($url[1], $dir)) { |
386
|
8 |
|
$url = $url[1]; |
387
|
8 |
|
return true; |
388
|
|
|
} |
389
|
4 |
|
return false; |
390
|
|
|
} |
391
|
|
|
/** |
392
|
|
|
* @param string $path |
393
|
|
|
* @param string $dir |
394
|
|
|
* |
395
|
|
|
* @return bool |
396
|
|
|
*/ |
397
|
10 |
|
protected static function is_relative_path_and_exists ($path, $dir) { |
398
|
10 |
|
return $dir && !preg_match('#^https?://#i', $path) && file_exists(static::absolute_path($path, $dir)); |
399
|
|
|
} |
400
|
|
|
/** |
401
|
|
|
* @param string $path |
402
|
|
|
* @param string $dir |
403
|
|
|
* |
404
|
|
|
* @return string |
405
|
|
|
*/ |
406
|
10 |
|
protected static function absolute_path ($path, $dir) { |
407
|
10 |
|
if (strpos($path, '/') === 0) { |
408
|
6 |
|
return realpath(getcwd().$path); |
409
|
|
|
} |
410
|
10 |
|
return realpath("$dir/$path"); |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|