Completed
Push — master ( aaed3a...1f335a )
by Nazar
04:30
created

Assets_processing::get_content()   C

Complexity

Conditions 9
Paths 16

Size

Total Lines 52
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 52
rs 6.5703
cc 9
eloc 40
nc 16
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Config,
12
	Exception,
13
	cs\Page\Includes_processing,
14
	Less_Parser,
15
	Leafo\ScssPhp\Compiler as Scss_compiler;
16
17
class Assets_processing {
18
	/**
19
	 * @param string     $package_name
20
	 * @param string     $package_dir
21
	 * @param string     $target_dir
22
	 * @param string[][] $includes_map
23
	 */
24
	static function run ($package_name, $package_dir, $target_dir, &$includes_map) {
25
		self::save_content(
26
			self::get_content(
27
				self::get_files($package_name),
28
				$package_name,
29
				$package_dir,
30
				$target_dir
31
			),
32
			$package_name,
33
			$target_dir,
34
			$includes_map
35
		);
36
	}
37
	/**
38
	 * @param string $package_name
39
	 *
40
	 * @return string[]
41
	 */
42
	protected static function get_files ($package_name) {
43
		$Config = Config::instance();
44
		$files  = [];
45
		foreach ($Config->components['modules'] as $module_name => $module_data) {
46
			if ($module_data['active'] == Config\Module_Properties::UNINSTALLED) {
47
				continue;
48
			}
49
			if (file_exists(MODULES."/$module_name/meta.json")) {
50
				$meta    = file_get_json(MODULES."/$module_name/meta.json");
51
				$files[] = self::extract_files($meta, $package_name);
52
			}
53
		}
54
		foreach ($Config->components['plugins'] as $plugin_name) {
55
			if (file_exists(PLUGINS."/$plugin_name/meta.json")) {
56
				$meta    = file_get_json(PLUGINS."/$plugin_name/meta.json");
57
				$files[] = self::extract_files($meta, $package_name);
58
			}
59
		}
60
		return array_unique(call_user_func_array('array_merge', $files));
61
	}
62
	/**
63
	 * @param array  $meta
64
	 * @param string $package_name
65
	 *
66
	 * @return string[]
67
	 */
68
	protected static function extract_files ($meta, $package_name) {
69
		$meta += ['require_bower' => [], 'require_npm' => []];
70
		$packages = $meta['require_bower'] + $meta['require_npm'];
71
		return isset($packages[$package_name]['files']) ? $packages[$package_name]['files'] : [];
72
	}
73
	/**
74
	 * @param string[] $files
75
	 * @param string   $package_name
76
	 * @param string   $package_dir
77
	 * @param string   $target_dir
78
	 *
79
	 * @return string[][]
80
	 */
81
	protected static function get_content ($files, $package_name, $package_dir, $target_dir) {
82
		$content = [];
83
		if ($files) {
84
			@mkdir($target_dir, 0770, true);
85
			file_put_contents(
86
				"$target_dir/.htaccess",
87
				<<<HTACCESS
88
<FilesMatch "\.css$">
89
	Header set Content-Type text/css
90
</FilesMatch>
91
HTACCESS
92
			);
93
		}
94
		foreach ($files as $file) {
95
			$file = "$package_dir/$file";
96
			switch (file_extension($file)) {
97
				case 'js':
98
					$content['js'][] = file_get_contents($file);
99
					break;
100
				case 'css':
101
					$content['css'][] = Includes_processing::css(
102
						file_get_contents($file),
103
						$file
104
					);
105
					break;
106
				case 'html':
107
					$content['html'][] = Includes_processing::html(
108
						file_get_contents($file),
109
						$file,
110
						$package_name,
111
						$target_dir
112
					);
113
					break;
114
				case 'less':
115
					try {
116
						$content['css'][] = Includes_processing::css(
117
							(new Less_Parser)->parseFile($file)->getCss(),
118
							$file
119
						);
120
					} catch (Exception $e) {
121
					}
122
					break;
123
				case 'scss':
124
					$content['css'][] = Includes_processing::css(
125
						(new Scss_compiler)->compile(file_get_contents($file)),
126
						$file
127
					);
128
					break;
129
			}
130
		}
131
		return $content;
132
	}
133
	/**
134
	 * @param string[][] $content
135
	 * @param string     $package_name
136
	 * @param string     $target_dir
137
	 * @param string[][] $includes_map
138
	 */
139
	protected static function save_content ($content, $package_name, $target_dir, &$includes_map) {
140
		foreach ($content as $extension => $c) {
141
			$target_file = "$target_dir/index.$extension";
142
			file_put_contents($target_file, implode('', $c), FILE_APPEND);
143
			$includes_map[$package_name][$extension][] = $target_file;
144
		}
145
	}
146
}
147