Completed
Push — master ( e51619...2bc3cc )
by Nazar
04:36
created

Assets_processing::html_process_scripts()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 7

Importance

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