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