Completed
Push — master ( 67d50f...8a7d08 )
by Nazar
04:56
created

RequireJS::get_requirejs_paths_find_package()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 4
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 12
rs 10
c 0
b 0
f 0
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\Includes;
9
use
10
	cs\Config,
11
	cs\Event;
12
13
trait RequireJS {
14
	/**
15
	 * @return string[]
16
	 */
17 4
	protected function get_requirejs_paths () {
18 4
		$Config                = Config::instance();
19 4
		$paths                 = [];
20
		$directories_to_browse = [
21 4
			DIR.'/bower_components',
22 4
			DIR.'/node_modules'
23
		];
24 4
		Event::instance()->fire(
25 4
			'System/Page/requirejs',
26
			[
27 4
				'paths'                 => &$paths,
28 4
				'directories_to_browse' => &$directories_to_browse
29
			]
30
		);
31 4
		foreach ($Config->components['modules'] as $module_name => $module_data) {
32 4
			if ($module_data['active'] == Config\Module_Properties::UNINSTALLED) {
33
				continue;
34
			}
35 4
			$paths += $this->get_requirejs_paths_add_aliases(MODULES."/$module_name");
36
		}
37 4
		foreach ($directories_to_browse as $dir) {
38 4
			foreach (get_files_list($dir, false, 'd', true) as $d) {
39 4
				$paths += $this->get_requirejs_paths_find_package($d);
40
			}
41
		}
42 4
		return $this->absolute_path_to_relative($paths);
43
	}
44
	/**
45
	 * @param string $dir
46
	 *
47
	 * @return string[]
48
	 */
49 4
	protected function get_requirejs_paths_add_aliases ($dir) {
50 4
		$paths = [];
51 4
		if (is_dir("$dir/includes/js")) {
52 4
			$name         = basename($dir);
53 4
			$paths[$name] = "$dir/includes/js";
54 4
			foreach ((array)@file_get_json("$dir/meta.json")['provide'] as $p) {
55 4
				if (strpos($p, '/') !== false) {
56 4
					$paths[$p] = $paths[$name];
57
				}
58
			}
59
		}
60 4
		return $paths;
61
	}
62
	/**
63
	 * @param string $dir
64
	 *
65
	 * @return string[]
66
	 */
67
	protected function get_requirejs_paths_find_package ($dir) {
68
		$path = $this->get_requirejs_paths_find_package_bower($dir) ?: $this->get_requirejs_paths_find_package_npm($dir);
69
		return $path ? [basename($dir) => substr($path, 0, -3)] : [];
70
	}
71
	/**
72
	 * @param string $dir
73
	 *
74
	 * @return string
75
	 */
76
	protected function get_requirejs_paths_find_package_bower ($dir) {
77
		$bower = @file_get_json("$dir/bower.json");
78
		foreach (@(array)$bower['main'] as $main) {
79
			if (preg_match('/\.js$/', $main)) {
80
				$main = substr($main, 0, -3);
81
				// There is a chance that minified file is present
82
				$main = file_exists_with_extension("$dir/$main", ['min.js', 'js']);
83
				if ($main) {
84
					return $main;
85
				}
86
			}
87
		}
88
		return null;
89
	}
90
	/**
91
	 * @param string $dir
92
	 *
93
	 * @return false|string
94
	 */
95
	protected function get_requirejs_paths_find_package_npm ($dir) {
96
		$package = @file_get_json("$dir/package.json");
97
		// If we have browser-specific declaration - use it
98
		/** @noinspection NestedTernaryOperatorInspection */
99
		$main = @$package['browser'] ?: (@$package['jspm']['main'] ?: @$package['main']);
100
		if (preg_match('/\.js$/', $main)) {
101
			$main = substr($main, 0, -3);
102
		}
103
		if ($main) {
104
			// There is a chance that minified file is present
105
			return file_exists_with_extension("$dir/$main", ['min.js', 'js']) ?: file_exists_with_extension("$dir/dist/$main", ['min.js', 'js']);
106
		}
107
		return false;
108
	}
109
}
110