Completed
Push — master ( 4e8078...fd8f4b )
by Nazar
04:30
created

Assets_processing   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 400
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 99.37%

Importance

Changes 0
Metric Value
dl 0
loc 400
ccs 157
cts 158
cp 0.9937
rs 7.9487
c 0
b 0
f 0
wmc 52
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
C css() 0 85 10
C js() 0 83 11
A new_line_needed() 0 12 4
A html() 0 13 2
A has_relative_href() 0 10 4
C html_process_scripts() 0 45 8
C html_process_links_and_styles() 0 66 10
A is_relative_path_and_exists() 0 3 3

How to fix   Complexity   

Complex Class

Complex classes like Assets_processing often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Assets_processing, and based on these observations, apply Extract Interface, too.

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