-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcompiler.widget.php
123 lines (104 loc) · 5.1 KB
/
compiler.widget.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
<?php
class fis_widget_map {
private static $arrCached = array();
public static function lookup(&$strFilename, &$smarty){
$strPath = self::$arrCached[$strFilename];
if(isset($strPath)){
return $strPath;
} else {
$arrConfigDir = $smarty->getConfigDir();
foreach ($arrConfigDir as $strDir) {
$strPath = preg_replace('/[\\/\\\\]+/', '/', $strDir . '/' . $strFilename);
if(is_file($strPath)){
self::$arrCached[$strFilename] = $strPath;
return $strPath;
}
}
}
trigger_error('missing map file "' . $strFilename . '"', E_USER_ERROR);
}
}
function smarty_compiler_widget($arrParams, $smarty){
//支持1.X widget 通过path属性判断 同时判断是否有name属性
if (isset($arrParams['path'])) {
if(!isset($arrParams['name']) || strpos($arrParams['name'], ':') === false){
$path = $arrParams['path'];
unset($arrParams['path']);
return getWidgetStrCode($path, $arrParams);
}
}
$strResourceApiPath = preg_replace('/[\\/\\\\]+/', '/', dirname(__FILE__) . '/FISResource.class.php');
$strCode = '<?php if(!class_exists(\'FISResource\', false)){require_once(\'' . $strResourceApiPath . '\');}';
$strCall = isset($arrParams['call']) ? $arrParams['call'] : null;
$bHasCall = ($strCall != null);
$strName = $arrParams['name'];
unset($arrParams['name']);
//construct params
$strFuncParams = getFuncParams($arrParams);
if($bHasCall){
unset($arrParams['call']);
$strTplFuncName = '\'smarty_template_function_\'.' . $strCall;
$strCallTplFunc = 'call_user_func('. $strTplFuncName . ',$_smarty_tpl,' . $strFuncParams . ');';
$strCode .= 'if(is_callable('. $strTplFuncName . ')){';
$strCode .= $strCallTplFunc;
$strCode .= '}else{';
}
if($strName){
$strCode .= '$_tpl_path=FISResource::getUri(' . $strName . ',$_smarty_tpl->smarty);';
$strCode .= 'if(isset($_tpl_path)){';
$strCode .= 'if (array_key_exists(\'fis_debug\', $_GET)) {echo "<!--widget start '.str_replace("\"", '\"', $strName).'-->\n";}';
if($bHasCall){
$strCode .= '$_smarty_tpl->getSubTemplate($_tpl_path, $_smarty_tpl->cache_id, $_smarty_tpl->compile_id, $_smarty_tpl->caching, $_smarty_tpl->cache_lifetime, ' . $strFuncParams . ', Smarty::SCOPE_LOCAL);';
$strCode .= 'if(is_callable('. $strTplFuncName . ')){';
$strCode .= $strCallTplFunc;
$strCode .= '}else{';
$strCode .= 'trigger_error(\'missing function define "\'.' . $strTplFuncName . '.\'" in tpl "\'.$_tpl_path.\'"\', E_USER_ERROR);';
$strCode .= '}';
} else {
$strCode .= 'echo $_smarty_tpl->getSubTemplate($_tpl_path, $_smarty_tpl->cache_id, $_smarty_tpl->compile_id, $_smarty_tpl->caching, $_smarty_tpl->cache_lifetime, ' . $strFuncParams . ', Smarty::SCOPE_LOCAL);';
}
$strCode .= 'if (array_key_exists(\'fis_debug\', $_GET)) {echo "\n<!--widget end '.str_replace("\"", '\"', $strName).'-->";}';
$strCode .= '}else{';
$strCode .= 'trigger_error(\'unable to locale resource "\'.' . $strName . '.\'"\', E_USER_ERROR);';
$strCode .= '}';
$strCode .= 'FISResource::load('.$strName.', $_smarty_tpl->smarty);';
} else {
trigger_error('undefined widget name in file "' . $smarty->_current_file . '"', E_USER_ERROR);
}
if($bHasCall){
$strCode .= '}';
}
$strCode .= '?>';
return $strCode;
}
function getWidgetStrCode($path, $arrParams){
$strFuncParams = getFuncParams($arrParams);
$path = trim($path,"\"");
$fn = '"smarty_template_function_fis_' . strtr(substr($path, 0, strrpos($path, '/')), '/', '_') . '"';
$strCode = '<?php ';
$strCode .= 'if (array_key_exists(\'fis_debug\', $_GET)) {echo "<!--widget start '.str_replace("\"", '\"', $path).'-->\n";}';
$strCode .= 'if(is_callable(' . $fn . ')){';
$strCode .= 'return call_user_func(' . $fn . ',$_smarty_tpl,' . $strFuncParams . ');';
$strCode .= '}else{';
$strCode .= '$fis_widget_output = $_smarty_tpl->getSubTemplate("' . $path . '", $_smarty_tpl->cache_id, $_smarty_tpl->compile_id, null, null, '. $strFuncParams.', Smarty::SCOPE_LOCAL);';
$strCode .= 'if(is_callable(' . $fn .')){';
$strCode .= 'return call_user_func('. $fn . ',$_smarty_tpl,' . $strFuncParams . ');';
$strCode .= '}else{';
$strCode .= 'echo $fis_widget_output;';
$strCode .= '}';
$strCode .= 'if (array_key_exists(\'fis_debug\', $_GET)) {echo "\n<!--widget end '.str_replace("\"", '\"', $path).'-->";}';
$strCode .= '}?>';
return $strCode;
}
function getFuncParams($arrParams){
$arrFuncParams = array();
foreach ($arrParams as $_key => $_value) {
if (is_int($_key)) {
$arrFuncParams[] = "$_key=>$_value";
} else {
$arrFuncParams[] = "'$_key'=>$_value";
}
}
$strFuncParams = 'array(' . implode(',', $arrFuncParams) . ')';
return $strFuncParams;
}