-
Notifications
You must be signed in to change notification settings - Fork 22
/
gmu2fis.php
362 lines (321 loc) · 14.2 KB
/
gmu2fis.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//初始化
define("DS", DIRECTORY_SEPARATOR);
define("PS", PATH_SEPARATOR);
define("BASE_DIR", dirname(__FILE__).DS);
header("Content-type: text/html; charset=utf-8");
class GMU2FIS {
const TYPE_WEBAPP = "widget";
protected $type;
protected $outputDir;
protected $srcDir;
protected $cssDir;
protected $components;
protected $_cache_files = array();
protected $_dependences = array();
protected $_zipObj;
protected function __construct($type = GMU2FIS::TYPE_WEBAPP, $srcDir = null, $cssDir=null, $dir = '/static/common/lib/gmu'){
$this->type = $type;
$this->outputDir = $dir;
$this->srcDir = $srcDir?$srcDir:BASE_DIR.'_src';
$this->cssDir = $cssDir?$cssDir:BASE_DIR.'assets';
}
protected function parse(){
$this->components = array();
$this->parseZepto();
$this->parseBase();
$this->parseWidget();
}
protected function render(){
$tmpFile = tempnam('./', 'gmu2fis');
$this->_zipObj = $zipObj = new ZipArchive();
if(!$zipObj->open($tmpFile, ZIPARCHIVE::CREATE)){
throw new Exception("Zip 无法写入");
}
foreach($this->components as $component) {
$this->renderComponent($component);
}
//生成gmuDeps.php文件
$deps = array();
foreach($this->components as $component){
if(!empty($component['dependences'])){
$_deps = array();
foreach($component['dependences'] as $_dep){
$_com = $this->components[$_dep];
$_js = $this->outputDir.'/'.$_com['name'].'/'.$_com['component'].'.js';
$_deps[$_js] = $_js;
}
$js = $this->outputDir.'/'.$component['name'].'/'.$component['component'].'.js';
$deps[$js] = $_deps;
}
}
$zipObj->addFromString('gmuDeps.php', '<?php $gmuDeps = ' . var_export($deps, true) . ';');
$zipObj->close();
$fileName = "gmu_".$this->type."2.0.0.zip";
if(@copy($tmpFile, $fileName)) {
echo '已经生成了一个ZIP文件在当前目录,文件名为'.$fileName;
}
@unlink($tmpFile);
}
protected function renderComponent($component){
$zipObj = $this->_zipObj;
$content = '';
foreach($component['dependences'] as $require) {
$content .= 'require(\'gmu:'.$this->getNameByPath($require).'\');'."\n";
}
$content.=$component['content']."\nexports = ".$component['export'].";";
$zipObj->addFromString($component['name'].'/'.$component['component'].'.js', $content);
//收集看有多少主题要处理
$widgetComponent=null;
if(!empty($component['plugin'])){
$widgetComponent = & $this->components[$component['widgetPath']];
} else if(!empty($component['widget'])){
$widgetComponent = & $component;
}
if($widgetComponent){
$themes = array();
if(!empty($widgetComponent['css']['themes'])){
$themes = array_merge($themes, array_keys($widgetComponent['css']['themes']));
}
if(!empty($widgetComponent['plugins']))foreach($widgetComponent['plugins'] as $plugin){
$plugin = $this->components[$plugin];
if(!empty($plugin['css']['themes'])){
$themes = array_merge($themes, array_keys($plugin['css']['themes']));
}
}
$themes = array_unique($themes);
foreach($themes as $theme){
$file = $component['widget'].'.'.$theme.'/'.(!empty($component['plugin'])?$component['component'].'/'.$component['component'].'.js':$component['widget'].'.'.$theme.'.js');
$zipObj->addFromString($file, (!empty($component['plugin'])?"require('gmu:".$component['widget'].".".$theme."');\n":"").
"exports = require('gmu:".$component['name']."');"
);
}
}
if(!empty($component['css'])){
$css = $component['css'];
$cssContent = $this->getFileConetent($css['relativePath'], 'css');
if(!empty($css['dependences'])){
foreach($css['dependences'] as $dependence){
$dependencePath = $this->cssDir.'/'.$dependence;
if(!file_exists($dependencePath))continue;
$cssContent = "@import url('".$this->outputDir."/".'commoncss/'.$dependence."');\n".$cssContent;
$this->renderCss('commoncss/', $dependence, file_get_contents($dependencePath), $dependencePath);
}
}
$this->getFileConetent($css['relativePath'], 'css') && $this->renderCss($component['name'].'/', $component['component'].'.css', $cssContent, $css['path']);
//处理theme
if(!empty($css['themes']))foreach($css['themes'] as $theme => $file){
$cssContent = $this->getFileConetent($file, 'css');
if(!empty($css['dependences'])){
foreach($css['dependences'] as $dependence){
$dependence = preg_replace('/\.css$/i', '.'.$theme.'.css', $dependence);
$dependencePath = $this->cssDir.'/'.$dependence;
if(!file_exists($dependencePath))continue;
$cssContent = "@import url('".$this->outputDir."/".'commoncss/'.$dependence."');\n".$cssContent;
$this->renderCss('commoncss/', $dependence, file_get_contents($dependencePath), $dependencePath);
}
}
$path = $component['widget'].'.'.$theme.'/'.(!empty($component['plugin'])?$component['component'].'/':'');
$this->renderCss($path, !empty($component['plugin'])?$component['component'].'.css':$component['widget'].'.'.$theme.'.css', $cssContent, $this->cssDir.'/'.$file);
}
}
}
protected function renderCss($path, $fileName, $content, $realpath){
$zipObj = $this->_zipObj;
preg_match_all('/url\((([\'"]?)(?!data)([^\'"]+?)\2)\)/im', $content, $m);
if(isset($m[3])) {
foreach($m[3] as $image) {
if(!preg_match('/\.(gif|png|jpg|jpeg)$/i', $image))continue;
$imagePath = realpath(dirname($realpath)."/".$image);
$baseName = basename($imagePath);
if(file_exists($imagePath)) {
$zipObj->addFromString($path.$baseName, file_get_contents($imagePath));
}
$content = str_replace($image, $baseName, $content);
}
}
$zipObj->addFromString($path.$fileName, $content);
}
protected function getNameByPath($relativePath){
foreach($this->components as $component) {
if($component['relativePath'] == $relativePath) {
return $component['name'];
}
}
return null;
}
protected function getPathByName($name){
foreach($this->components as $component) {
if($component['name'] == $name) {
return $component['relativePath'];
}
}
return null;
}
protected function getFileConetent($file, $type='js'){
if(!isset($this->_cache_files[$file]) && is_file($path = ($type=='js'?$this->srcDir:$this->cssDir)."/".$file)) {
$this->_cache_files[$file] = file_get_contents($path);
}
return $this->_cache_files[$file];
}
protected function buildJsDependences($file){
if(!isset($this->_dependences[$file])) {
$content = $this->getFileConetent($file);
if(preg_match('/@import\s(.+?)$/im', $content, $match)){
$dependences = explode(",", $match[1]);
$this->_dependences[$file] = array();
foreach($dependences as $dependence) {
$dependence = trim($dependence);
if(!$dependence || !$this->getFileConetent($dependence))continue;
$this->buildJsDependences($dependence);
$this->_dependences[$file][] = $dependence;
}
}
}
}
protected function getJsDependences($file, &$result = array()){
$this->buildJsDependences($file);
if(isset($this->_dependences[$file]) && !in_array($file, $result)) {
foreach($this->_dependences[$file] as $dempendence) {
if(isset($this->_dependences[$file])){
$this->getJsDependences($dempendence, $result);
}
$result[] = $dempendence;
}
$result = array_unique($result);
}
return $result;
}
protected function parseFile($jsFile, Array $attch = array()) {
$relativePath = isset($attch['relativePath'])?$attch['relativePath']:substr($jsFile, strlen($this->srcDir)+1);
//解析@import, 获得js依赖
$dependences = $this->getJsDependences($relativePath);
$info = array_merge(array(
'path' => $jsFile,
'content' => $this->getFileConetent($relativePath),
'dependences' => $dependences
), $attch);
//查找css文件,并且加上依赖
if($info['plugin']) {
$cssFile = $this->cssDir.'/'.$this->type.'/'.$info['widget'].'/'.$info['widget'].'.'.$info['plugin'].'.css';
} else {
$cssFile = $this->cssDir.'/'.$this->type.'/'.$info['widget'].'/'.$info['widget'].'.css';
}
$themes = array();
$themeRE = '/(?:\/|^)'.$info['widget'].(!empty($info['plugin'])?'.'.$info['plugin']:'').'\.(default|blue|dark)\.css$/i';
$files = self::rScandir($this->cssDir.'/'.$this->type.'/'.$info['widget'], $themeRE);
foreach($files as $file){
if(preg_match($themeRE, $file, $m)){
$themes[$m[1]] = substr($file, strlen($this->cssDir)+1);
}
}
$cssDeps = array();
if(preg_match('/@importcss\s(.+?)$/im', $this->getFileConetent($relativePath), $match)){
$dependences = explode(",", $match[1]);
foreach($dependences as $dependence) {
$dependence = trim($dependence);
if(!$dependence)continue;
$cssDeps[] = $dependence;
}
}
$info['css'] = array(
'dependences' => array_unique($cssDeps),
'themes' => $themes
);
if(is_file($cssFile)) {
$info['css']['path'] = $cssFile;
$info['css']['relativePath'] = substr($cssFile, strlen($this->cssDir)+1);
}
return $info;
}
protected function parseZepto(){
$component = $this->parseFile(BASE_DIR.'_src/core/zepto.js', array(
'name' => 'zepto',
'export' => 'Zepto',
'component' => 'zepto',
'relativePath' => 'core/zepto.js'
));
$component['content'] = str_replace('window.Zepto = Zepto', '', $component['content']);
$component['content'] = str_replace('\'$\' in window || (window.$ = Zepto)', '', $component['content']);
$this->components['core/zepto.js'] = $component;
}
protected function parseBase(){
$jsFiles = self::rScandir($this->srcDir.'/core', '/\.js$/i');
$excude = '/^core\/(zepto\.js$|zepto\.min\.js$|zepto\.fx\.js$|zepto\/.*)/i';
$exportsRule = array(
'^zepto\.ui\.js$' => 'Zepto.ui',
'default' => 'Zepto',
);
foreach($jsFiles as $file) {
$relativePath = substr($file, strlen($this->srcDir)+1);
if(preg_match($excude, $relativePath)) {
continue;
}
$basename = basename($file);
if(!preg_match('/^zepto\.(.+)\.js$/', $basename, $match))continue;
$export = '';
foreach($exportsRule as $key=>$val){
$export = $val;
if(preg_match('/'.$key.'/i', $basename)){
break;
}
}
$this->components[$relativePath] = $this->parseFile($file, array(
'name' => 'base/'.$match[1],
'component' => $match[1],
'export' => $export,
'relativePath' => $relativePath
));
}
}
protected function parseWidget(){
$jsFiles = self::rScandir($this->srcDir.'/'.$this->type.'/', '/\.js$/i');
foreach($jsFiles as $file) {
$relativePath = substr($file, strlen($this->srcDir)+1);
$basename = basename($file);
if(!preg_match('/^(.*?)(?:\.(.+))?\.js$/', $basename, $match))continue;
$info = $this->parseFile($file, array(
'name' => $match[1].(isset($match[2])?'/'.$match[2]:''),
'component' => isset($match[2])?$match[2]:$match[1],
'plugin' => isset($match[2])?$match[2]:'',
'widget' => $match[1],
'export' => 'Zepto.ui.'.$match[1],
'relativePath' => $relativePath
));
$this->components[$relativePath] = $info;
}
foreach($jsFiles as $file) {
$relativePath = substr($file, strlen($this->srcDir)+1);
$info = &$this->components[$relativePath];
if($info['plugin'] && ($path = $this->getPathByName($info['widget']))) { //如果是plugin
$this->components[$path]['plugins'][] = $relativePath;
$info['widgetPath'] = $path;
}
}
}
public function build(){
$this->parse();
$this->render();
}
public static function run() {
$script = new GMU2FIS('widget');
$script->build();
}
public static function rScandir($path, $exts, &$res=array()) {
$path = rtrim(str_replace('\\', '/', $path), '/\\');
$files = scandir($path);
foreach ($files as $file) {
$filepath = $path . '/' . $file;
if (is_dir($filepath) && $file[0] != '.') {
self::rScandir($filepath, $exts, $res);
} else {
if (preg_match($exts, $file) && is_file($filepath)) {
$res[] = $filepath;
}
}
}
return $res;
}
}
GMU2FIS::run();