php 读取js文件,PHP读取封装JS文件

PHP读取打包JS文件

RewriteRule ^((.*/js/)?.*\.js)$ combinejs.php?path=$1&pre=$2&type=js [NC,L,QSA]

RewriteCond %{REQUEST_URI} !\.js$

RewriteCond %{REQUEST_URI} !combinejs\.php$

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ http://js.wcdn.cn/$1 [NC,L,QSA,R]

require_once 'combine/JSCombineProcessor.php';

//t4/home/js/comp/content/publisherTop.js

$path = @$_GET['path'];

//t4/home/js/

$pre = @$_GET['pre'];

//D:\sina\t4\home\js\comp\content\publisherTop.js

$real_path = realpath($path);

if($real_path == false) {

$path = str_replace("/js/" , "/js/conf/" , $path);

$real_path = realpath($path);

if($real_path == false) {

$uri = $_SERVER['REQUEST_URI'];

JSCombineProcessor::getOnlineSource($uri);

return;

}

}

header ( 'Content-Type:application/x-javascript;Charset=UTF-8' );

//D:/sina/t4/home/js/comp/content/publisherTop.js

$real_path = str_replace('\\' , '/' , $real_path);

//D:/sina

$basepath = str_replace($path, "", $real_path);

//处理打包逻辑

$processor = new JSCombineProcessor($basepath , $path , $pre);

//获取文件内容

$fileContent = $processor -> getContent();

echo $fileContent;

?>

class JSCombineProcessor {

public $basePath;

public $relativePath;

public $prePath;

public $resultArr;

public $map;

public static function getOnlineSource($path) {

$api = "http://js.wcdn.cn".$path;

header('Location:'.$api);

/*

$api = "http://js.t.sinajs.cn".$path;

$host_ip = '211.101.171.217'; //ip地址

$dns_info = dns_get_record('js.t.sinajs.cn' , DNS_A);

if(count($dns_info) >= 1) {

$host_ip = $dns_info[0]['ip'];

}

$url_info = parse_url($api);

$api = str_replace($url_info['host'], $host_ip, $api); // 将域名替换成ip地址,指定到此ip地址下运行

$ch = curl_init($api);

curl_setopt($ch, CURLOPT_URL, $api);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$curl_host = array("Host:{$url_info['host']}"); //绑定域名到此ip地址上

curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_host); // 传送头信息

$result = curl_exec($ch);

$content_type = '';

// Check if any error occured

if(!curl_errno($ch))

{

$info = curl_getinfo($ch);

$content_type = $info['content_type'];

header('Content-Type:'.$content_type);

}

echo $result;

// Close handle

curl_close($ch);

*/

}

public function __construct($basePath , $relativePath , $prePath) {

$this->basePath = $basePath;

$this->relativePath = $relativePath;

$this->prePath = $basePath.$prePath;

$this->resultArr = array();

$this->map = array();

}

public function checkFileBom($path) {

$chars = array(239,187,191);

$lines = file($path);

$bom = false;

if(count($lines) >= 1) {

$line = $lines[0] ;

if(strlen($line) >= 3) {

$bom = true;

for($i = 0 ; $i < 3 ; $i++) {

if(ord($line[$i]) != $chars[$i]) {

$bom = false;

break;

}

}

}

}

return $bom;

}

public function getContent() {

$entry = $this->basePath.$this->relativePath;

$this->processOneFile($entry);

return implode('',$this->resultArr);

}

public function processOneFile($filePath , $mapkey=false) {

$real_path = realpath($filePath);

if($real_path == false && $mapkey) {

$this->map[$mapkey] = 1;

return;

}

$isutf8bom = $this->checkFileBom($filePath);

if($isutf8bom) {

echo "alert('文件:".$filePath." 有utf8 bom 头,请修改')";

exit;

}

$list = file($filePath);

foreach($list as $line) {

preg_match ( '/^\s*\$import\s*\(\s*([\'"])\s*(.+)\s*\1\s*\).*$/i', $line, $aMatch );

if($aMatch) {

$namespace = $aMatch[2];

$replace = str_replace(".", "/", $namespace);

$entry = $this->prePath.$replace.".js";

if(!array_key_exists($namespace, $this->map)) {

$this->map[$namespace] = 1;

$this->processOneFile($entry , $namespace);

}

} else {

$this->resultArr[] = $line;

}

}

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值