destoon 增加js合并功能

2015-01-12  金城  2314

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>我的模版</title>

<!--以前是这样的-->
<link rel="stylesheet"type="text/css"href="/skin/default/style.css"/>
<link rel="stylesheet"type="text/css"href="/skin/default/article.css"/>
<script type="text/javascript"src="/lang/zh-cn/lang.js"></script>
<script type="text/javascript"src="/file/script/config.js"</script>
<script type="text/javascript"src="/file/script/common.js"</script>
<script type="text/javascript"src="/file/script/page.js"</script>
<script type="text/javascript"src="/jquery/jquery.min.js"</script>

<!--现在是这样的, 上边的可以去掉了-->
<?php MergerLoadCss(array('/skin/default/style.css','/skin/default/article.css'));?>
<?php MergerLoadJs(array('/jquery/jquery.min.js','/lang/zh-cn/lang.js','/file/script/config.js','/file/script/common.js','/file/script/page.js'));?>

<!--运行后是酱的-->
<link rel="stylesheet"type="text/css"href="/file/cache/css/0bb6b3d47fa256777c7a7d8ecbace04c.css">
<script type="text/javascript"src="/file/cache/js/8f72145cda52d7aa1df763638b157d88.js"</script>

</head>

<body>
我的模版内容
</body>
</html>
# 放到全局文件中/global.func.php
functionMergerLoadJs($filepathArr){
 $cache_file_name=md5(serialize($filepathArr)).'.js';
 $catche_file_path=DT_CACHE.'/js/'.$cache_file_name;
 if(!file_exists($cache_file_path)){
  foreach($filepathArr as $f)
   $content .=file_get_contents(DT_ROOT.$f)."\r\n";
  $content=preg_replace(array(
   '/[\t ]+/iu',
   '/ ?([\{\}\(\);:,=]) ?/iu',
   '/([\{,;:])\s*([^\/])/iu',
   '/[\r\n]\s*\*[^\/][^\r\n]*[\r\n]/iu',
  ),array(
   ' ',
   '$1',
   '$1$2',
   '',
  ),$content);
  file_put_contents($catche_file_path,$content);
 }
 echo '$lt;script type="text/javascript" src="/file/cache/js/'.$cache_file_name.'">$lt;/script>';
}
functionMergerLoadCss($filepathArr){
 $cache_file_name=md5(serialize($filepathArr)).'.css';
 $catche_file_path=DT_CACHE.'/css/'.$cache_file_name;
 if(!file_exists($cache_file_path)){
  foreach($filepathArr as $f)
   $content .=file_get_contents(DT_ROOT.$f)."\r\n";
  $content=preg_replace(array(
   '/[\t ]+/iu',
   '/ ?([\{\}\(\);:,=]) ?/iu',
   '/([\{,;:])\s*([^\/])/iu',
   '/[\r\n]\s*\*[^\/][^\r\n]*[\r\n]/iu',
  ),array(
   ' ',
   '$1',
   '$1$2',
   '',
  ),$content);
  file_put_contents($catche_file_path,$content);
 }
 echo '$lt;link rel="stylesheet" type="text/css" href="/file/cache/css/'.$cache_file_name.'"/>';
}