forked from fex-team/fis-plus-smarty-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFISAutoPack.class.php
115 lines (97 loc) · 4.06 KB
/
FISAutoPack.class.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
<?php
/**
* FIS静态资源自动打包 smarty plugin 脚本
*
* @see http://solar.baidu.com/autopack/doc?file=FIS%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E8%87%AA%E5%8A%A8%E5%90%88%E5%B9%B6%E7%B3%BB%E7%BB%9F%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3
* http://gitlab.baidu.com/fis-dev/fis-plus-smarty-plugin/tree/autopack-fisplus
* @author Yang,junlong at 2015-07-03 16:43:24 update.
* @version $Id$
*/
if (!class_exists('FISResource')) {
require_once(dirname(__FILE__) . '/FISResource.class.php');
}
/**
* FIS自动打包页面静态资源统计脚本
* 提供API给产品线使用,统计页面中使用的静态资源,区分是否首屏
*/
class FISAutoPack {
private static $VER = 2; //统计版本,用来log切分时候区分
private static $sampleRate = 1; //采样率,默认0.1,贴吧等产品线PV较大
private static $fid = ""; //项目ID
private static $pageName = ""; //页面名称
private static $fsFinish = false; //是否完成首屏
public static $usedStatics = array('first_screen'=> array(),'other'=> array()); //静态资源数组
//设置fis id 一般为项目ID
public static function setFid($fid){
self::$fid = $fid;
}
//获取fis id
public static function getFid(){
return self::$fid;
}
//设置采样率
public static function setSampleRate($rate){
self::$sampleRate = $rate;
}
//获取采样率
public static function getSampleRate(){
return self::$sampleRate;
}
//根据采样率判断是否命中
private static function isSample($sample){
$tmp_sample = rand(1, 10000) / 10000;
return $sample >= $tmp_sample;
}
//获取页面名称
public static function getPageName(){
return self::$pageName;
}
//设置页面名称
public static function setPageName($page){
self::$pageName = $page;
}
public static function addHashTable($strId, $smarty){
$staticInfo = FISResource::getStaticInfo($strId, $smarty);
if($staticInfo['hash']){
self::addStatic($staticInfo['hash']);
}
}
//添加静态资源,首屏完成之后添加的资源标记为非首屏资源
public static function addStatic($hash){
$type = self::$fsFinish ? "other" : "first_screen"; //是否是首屏资源
self::$usedStatics[$type][] = $hash;
}
//设置是否已完成首屏
public static function setFRender(){
self::$fsFinish = true;
}
//获取渲染到前端的js统计代码
public static function getCountUrl(){
$code = "";
$sampleRate = self::getSampleRate();
$fid = self::getFid();
if(self::isSample($sampleRate) && $fid){
$pageName = self::getPageName();
//去重,另外如果资源同时存在于首屏与非首屏中,当成首屏资源
$first_screen = array_filter(array_unique(self::$usedStatics['first_screen']));
$other = array_filter(array_unique(self::$usedStatics['other']));
if(count($other) >0){
foreach ($other as $k => $val) {
if(in_array($val, $first_screen) ){
unset($other[$k]);
}
}
}
if (count($first_screen) >0 || count($other) >0 ) {
$timeStamp = time();
$hashStr = '';
$fsStr = implode(',', $first_screen);
$otherStr = implode(',', $other);
$hashStr .= ($fsStr . $otherStr);
$code .= '(new Image()).src="http://static.tieba.baidu.com/tb/pms/img/st.gif?pid=242&v=' . self::$VER . '&fs=' . $fsStr . "&otherStr=" . $otherStr . "&page=" . $pageName . '&sid=' . $timeStamp . '&hash=<STATIC_HASH>' . '&fid=' . $fid . '";';
$code = str_replace("<STATIC_HASH>", substr(md5($hashStr), 0, 10), $code);
}
}
return $code;
}
}