Completed
Push — master ( 1eb968...413ea6 )
by Nazar
04:16
created

Assets_processing   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 34
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 169
rs 9.2

6 Methods

Rating   Name   Duplication   Size   Complexity  
D get_requirejs_paths() 0 47 14
A run() 0 13 1
B get_files() 0 20 6
A extract_files() 0 5 2
D get_content() 0 44 9
A save_content() 0 7 2
1
<?php
2
/**
3
 * @package   Composer assets
4
 * @category  plugins
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2015-2016, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\plugins\Composer_assets;
10
use
11
	cs\Cache,
12
	cs\Config,
13
	Exception,
14
	cs\Page\Includes_processing,
15
	Less_Parser,
16
	Leafo\ScssPhp\Compiler as Scss_compiler;
17
18
class Assets_processing {
19
	static function get_requirejs_paths () {
20
		$vendor_dir  = STORAGE.'/Composer/vendor';
21
		$paths       = [];
22
		$directories = array_merge(
23
			get_files_list("$vendor_dir/bower-asset", false, 'd', true),
24
			get_files_list("$vendor_dir/npm-asset", false, 'd', true)
25
		);
26
		foreach ($directories as $module_dir) {
27
			$module_name         = basename($module_dir);
28
			$relative_module_dir = 'storage'.substr($module_dir, strlen(STORAGE));
29
			// Hopefully, bower.json is present and contains necessary information
30
			$bower = @file_get_json("$module_dir/bower.json");
31
			foreach (@(array)$bower['main'] as $main) {
32
				if (preg_match('/\.js$/', $main)) {
33
					$main = substr($main, 0, -3);
34
					// There is a chance that minified file is present
35
					if (file_exists("$module_dir/$main.min.js")) {
36
						$main .= '.min';
37
					}
38
					if (file_exists("$module_dir/$main.js")) {
39
						$paths[$module_name] = "$relative_module_dir/$main";
40
						continue 2;
41
					}
42
				}
43
			}
44
			// If not - try package.json from npm
45
			$package = @file_get_json("$module_dir/package.json");
46
			// If we have browser-specific declaration - use it
47
			$main = @$package['browser'] ?: (@$package['jspm']['main'] ?: @$package['main']);
48
			if (preg_match('/\.js$/', $main)) {
49
				$main = substr($main, 0, -3);
50
			}
51
			if ($main) {
52
				// There is a chance that minified file is present
53
				if (file_exists("$module_dir/$main.min.js")) {
54
					$paths[$module_name] = "$relative_module_dir/$main.min";
55
				} elseif (file_exists("$module_dir/$main.js")) {
56
					$paths[$module_name] = "$relative_module_dir/$main";
57
				} elseif (file_exists("$module_dir/dist/$main.min.js")) {
58
					$paths[$module_name] = "$relative_module_dir/dist/$main.min";
59
				} elseif (file_exists("$module_dir/dist/$main.js")) {
60
					$paths[$module_name] = "$relative_module_dir/dist/$main";
61
				}
62
			}
63
		}
64
		return $paths;
65
	}
66
	/**
67
	 * @param string     $package_name
68
	 * @param string     $package_dir
69
	 * @param string     $target_dir
70
	 * @param string[][] $includes_map
71
	 */
72
	static function run ($package_name, $package_dir, $target_dir, &$includes_map) {
73
		self::save_content(
74
			self::get_content(
75
				self::get_files($package_name),
76
				$package_name,
77
				$package_dir,
78
				$target_dir
79
			),
80
			$package_name,
81
			$target_dir,
82
			$includes_map
83
		);
84
	}
85
	/**
86
	 * @param string $package_name
87
	 *
88
	 * @return string[]
89
	 */
90
	protected static function get_files ($package_name) {
91
		$Config = Config::instance();
92
		$files  = [];
93
		foreach ($Config->components['modules'] as $module_name => $module_data) {
94
			if ($module_data['active'] == Config\Module_Properties::UNINSTALLED) {
95
				continue;
96
			}
97
			if (file_exists(MODULES."/$module_name/meta.json")) {
98
				$meta    = file_get_json(MODULES."/$module_name/meta.json");
99
				$files[] = self::extract_files($meta, $package_name);
100
			}
101
		}
102
		foreach ($Config->components['plugins'] as $plugin_name) {
103
			if (file_exists(PLUGINS."/$plugin_name/meta.json")) {
104
				$meta    = file_get_json(PLUGINS."/$plugin_name/meta.json");
105
				$files[] = self::extract_files($meta, $package_name);
106
			}
107
		}
108
		return array_unique(call_user_func_array('array_merge', $files));
109
	}
110
	/**
111
	 * @param array  $meta
112
	 * @param string $package_name
113
	 *
114
	 * @return string[]
115
	 */
116
	protected static function extract_files ($meta, $package_name) {
117
		$meta += ['require_bower' => [], 'require_npm' => []];
118
		$packages = $meta['require_bower'] + $meta['require_npm'];
119
		return isset($packages[$package_name]['files']) ? $packages[$package_name]['files'] : [];
120
	}
121
	/**
122
	 * @param string[] $files
123
	 * @param string   $package_name
124
	 * @param string   $package_dir
125
	 * @param string   $target_dir
126
	 *
127
	 * @return string[][]
128
	 */
129
	protected static function get_content ($files, $package_name, $package_dir, $target_dir) {
130
		$content = [];
131
		if ($files) {
132
			@mkdir($target_dir, 0770, true);
133
		}
134
		foreach ($files as $file) {
135
			$file = "$package_dir/$file";
136
			switch (file_extension($file)) {
137
				case 'js':
138
					$content['js'][] = file_get_contents($file);
139
					break;
140
				case 'css':
141
					$content['css'][] = Includes_processing::css(
142
						file_get_contents($file),
143
						$file
144
					);
145
					break;
146
				case 'html':
147
					$content['html'][] = Includes_processing::html(
148
						file_get_contents($file),
149
						$file,
150
						$package_name,
151
						$target_dir
152
					);
153
					break;
154
				case 'less':
155
					try {
156
						$content['css'][] = Includes_processing::css(
157
							(new Less_Parser)->parseFile($file)->getCss(),
158
							$file
159
						);
160
					} catch (Exception $e) {
161
					}
162
					break;
163
				case 'scss':
164
					$content['css'][] = Includes_processing::css(
165
						(new Scss_compiler)->compile(file_get_contents($file)),
166
						$file
167
					);
168
					break;
169
			}
170
		}
171
		return $content;
172
	}
173
	/**
174
	 * @param string[][] $content
175
	 * @param string     $package_name
176
	 * @param string     $target_dir
177
	 * @param string[][] $includes_map
178
	 */
179
	protected static function save_content ($content, $package_name, $target_dir, &$includes_map) {
180
		foreach ($content as $extension => $c) {
181
			$target_file = "$target_dir/index.$extension";
182
			file_put_contents($target_file, implode('', $c), FILE_APPEND);
183
			$includes_map[$package_name][$extension][] = $target_file;
184
		}
185
	}
186
}
187