This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
/
Abstract.php
150 lines (132 loc) · 5 KB
/
Abstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
class CM_Asset_Javascript_Abstract extends CM_Asset_Abstract {
/** @var CM_Frontend_JavascriptContainer */
protected $_js;
/** @var CM_Site_Abstract */
protected $_site;
/** @var bool */
protected $_debug;
/**
* @param CM_Site_Abstract $site
* @param bool|null $debug
*/
public function __construct(CM_Site_Abstract $site, $debug = null) {
$this->_site = $site;
$this->_debug = (bool) $debug;
$this->_js = new CM_Frontend_JavascriptContainer();
}
public function get() {
$content = $this->_js->compile();
if (!$this->_isDebug()) {
$content = $this->_minify($content);
}
return $content;
}
/**
* @param string $content
* @return string
*/
protected function _minify($content) {
$md5 = md5($content);
$cacheKey = CM_CacheConst::App_Resource . '_md5:' . $md5;
$cache = CM_Cache_Persistent::getInstance();
if (false === ($contentMinified = $cache->get($cacheKey))) {
$uglifyCommand = 'uglifyjs --no-copyright';
/**
* Quote keys in literal objects, otherwise some browsers break.
* E.g. "select2.js" on "Android 4.0.4"
*/
$uglifyCommand .= ' --beautify beautify=false,quote-keys=true';
$contentMinified = CM_Util::exec($uglifyCommand, null, $content);
$cache->set($cacheKey, $contentMinified);
}
return $contentMinified;
}
/**
* @param string[] $mainPaths
* @param string[] $sourcePaths
* @param bool $generateSourceMaps
* @return string
*/
protected function _browserify(array $mainPaths, array $sourcePaths, $generateSourceMaps) {
if (!count($mainPaths)) {
return '';
}
$involvedFiles = [];
foreach ($sourcePaths as $sourcePath) {
$involvedFiles = array_merge($involvedFiles, CM_Util::rglob('*.js', $sourcePath));
}
foreach ($mainPaths as $mainPath) {
$involvedFiles[] = $mainPath;
}
$cacheKeyContent = \Functional\reduce_left(array_unique($involvedFiles), function ($path, $index, $collection, $carry) {
return md5($carry . (new CM_File($path))->read());
}, '');
$cacheKeyMainPaths = \Functional\reduce_left($mainPaths, function ($path, $index, $collection, $carry) {
return md5($carry . $path);
}, '');
$cache = CM_Cache_Persistent::getInstance();
$cacheKey = $cache->key(__METHOD__, $cacheKeyContent, $cacheKeyMainPaths, $generateSourceMaps);
return $cache->get($cacheKey, function () use ($mainPaths, $sourcePaths, $generateSourceMaps) {
$args = $mainPaths;
if ($generateSourceMaps) {
$args[] = '--debug';
}
return CM_Util::exec('NODE_PATH="' . implode(':', $sourcePaths) . '" browserify', $args);
});
}
/**
* @param string $path
*/
protected function _appendDirectoryGlob($path) {
foreach (array_reverse($this->_site->getModules()) as $moduleName) {
$initPath = $this->_getPathInModule($moduleName, $path);
foreach (CM_Util::rglob('*.js', $initPath) as $filePath) {
$content = (new CM_File($filePath))->read();
$this->_js->append($content);
}
}
}
/**
* @param string $mainPath
* @param bool $generateSourceMaps
* @throws CM_Exception
*/
protected function _appendDirectoryBrowserify($mainPath, $generateSourceMaps) {
$sourceMainPaths = [];
foreach (array_reverse($this->_site->getModules()) as $moduleName) {
$sourceMainPaths = array_merge($sourceMainPaths, glob($this->_getPathInModule($moduleName, $mainPath) . '*.js'));
}
$this->_appendSourceMainBrowserify($sourceMainPaths, $generateSourceMaps);
}
/**
* @param string[] $sourceMainPaths
* @param bool $generateSourceMaps
* @throws CM_Exception
*/
protected function _appendSourceMainBrowserify($sourceMainPaths, $generateSourceMaps) {
if ($generateSourceMaps && !$this->_js->isEmpty()) {
throw new CM_Exception('Cannot generate source maps when output container already contains code.');
}
$sourcePaths = [];
foreach (array_reverse($this->_site->getModules()) as $moduleName) {
$sourcePaths[] = $this->_getPathInModule($moduleName, 'client-vendor/source');
}
$content = $this->_browserify($sourceMainPaths, $sourcePaths, $generateSourceMaps);
$this->_js->append($content);
}
/**
* @param string $moduleName
* @param string $path
* @return string
*/
protected function _getPathInModule($moduleName, $path) {
return DIR_ROOT . CM_Bootloader::getInstance()->getModulePath($moduleName) . $path;
}
/**
* @return bool
*/
protected function _isDebug() {
return $this->_debug;
}
}