|
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\Assets; |
|
9
|
|
|
use |
|
10
|
|
|
cs\Config, |
|
11
|
|
|
cs\Event; |
|
12
|
|
|
|
|
13
|
|
|
class RequireJS { |
|
14
|
|
|
/** |
|
15
|
|
|
* @return string[] |
|
16
|
|
|
*/ |
|
17
|
6 |
|
public static function get_config () { |
|
18
|
6 |
|
$Config = Config::instance(); |
|
19
|
6 |
|
$paths = []; |
|
20
|
6 |
|
$packages = []; |
|
21
|
|
|
$directories_to_browse = [ |
|
22
|
6 |
|
DIR.'/bower_components', |
|
23
|
6 |
|
DIR.'/node_modules' |
|
24
|
|
|
]; |
|
25
|
6 |
|
Event::instance()->fire( |
|
26
|
6 |
|
'System/Page/requirejs', |
|
27
|
|
|
[ |
|
28
|
6 |
|
'paths' => &$paths, |
|
29
|
6 |
|
'packages' => &$packages, |
|
30
|
6 |
|
'directories_to_browse' => &$directories_to_browse |
|
31
|
|
|
] |
|
32
|
|
|
); |
|
33
|
6 |
|
foreach ($Config->components['modules'] as $module_name => $module_data) { |
|
34
|
6 |
|
if ($module_data['active'] == Config\Module_Properties::UNINSTALLED) { |
|
35
|
2 |
|
continue; |
|
36
|
|
|
} |
|
37
|
6 |
|
$paths += static::add_aliases(MODULES."/$module_name"); |
|
38
|
|
|
} |
|
39
|
6 |
|
foreach ($directories_to_browse as $dir) { |
|
40
|
6 |
|
foreach (get_files_list($dir, false, 'd', true) as $d) { |
|
41
|
6 |
|
$packages[] = static::find_package_main_path($d); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
return [ |
|
45
|
6 |
|
'paths' => _substr($paths, strlen(DIR)), |
|
46
|
6 |
|
'packages' => array_values(array_filter($packages)) |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $dir |
|
51
|
|
|
* |
|
52
|
|
|
* @return string[] |
|
53
|
|
|
*/ |
|
54
|
6 |
|
protected static function add_aliases ($dir) { |
|
55
|
6 |
|
$paths = []; |
|
56
|
6 |
|
if (is_dir("$dir/assets/js")) { |
|
57
|
6 |
|
$name = basename($dir); |
|
58
|
6 |
|
$paths[$name] = "$dir/assets/js"; |
|
59
|
6 |
|
foreach ((array)@file_get_json("$dir/meta.json")['provide'] as $p) { |
|
60
|
6 |
|
if (strpos($p, '/') === false) { |
|
61
|
6 |
|
$paths[$p] = $paths[$name]; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
6 |
|
return $paths; |
|
66
|
|
|
} |
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $dir |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
|
|
|
|
|
71
|
|
|
*/ |
|
72
|
2 |
|
protected static function find_package_main_path ($dir) { |
|
73
|
2 |
|
$path = static::find_package_bower($dir) ?: static::find_package_npm($dir); |
|
74
|
2 |
|
if (!$path) { |
|
75
|
2 |
|
return []; |
|
76
|
|
|
} |
|
77
|
|
|
return [ |
|
78
|
2 |
|
'name' => basename($dir), |
|
79
|
2 |
|
'main' => substr($path, strlen($dir) + 1, -3), |
|
80
|
2 |
|
'location' => substr($dir, strlen(DIR)) |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $dir |
|
85
|
|
|
* |
|
86
|
|
|
* @return false|string |
|
87
|
|
|
*/ |
|
88
|
2 |
|
protected static function find_package_bower ($dir) { |
|
89
|
2 |
|
$bower = @file_get_json("$dir/bower.json"); |
|
90
|
2 |
|
foreach (@(array)$bower['main'] as $main) { |
|
91
|
2 |
|
if (preg_match('/\.js$/', $main)) { |
|
92
|
2 |
|
$main = substr($main, 0, -3); |
|
93
|
|
|
// There is a chance that minified file is present |
|
94
|
2 |
|
$main = file_exists_with_extension("$dir/$main", ['min.js', 'js']); |
|
95
|
2 |
|
if ($main) { |
|
96
|
2 |
|
return $main; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
2 |
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $dir |
|
104
|
|
|
* |
|
105
|
|
|
* @return false|string |
|
106
|
|
|
*/ |
|
107
|
2 |
|
protected static function find_package_npm ($dir) { |
|
108
|
2 |
|
$package = @file_get_json("$dir/package.json"); |
|
109
|
|
|
// If we have browser-specific declaration - use it |
|
110
|
|
|
/** @noinspection NestedTernaryOperatorInspection */ |
|
111
|
2 |
|
$main = @$package['browser'] ?: (@$package['jspm']['main'] ?: @$package['main']); |
|
112
|
2 |
|
if (preg_match('/\.js$/', $main)) { |
|
113
|
2 |
|
$main = substr($main, 0, -3); |
|
114
|
|
|
} |
|
115
|
2 |
|
if ($main) { |
|
116
|
|
|
// There is a chance that minified file is present |
|
117
|
2 |
|
return file_exists_with_extension("$dir/$main", ['min.js', 'js']) ?: file_exists_with_extension("$dir/dist/$main", ['min.js', 'js']); |
|
118
|
|
|
} |
|
119
|
2 |
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]orarray<String>.