PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Tpblog/ThinkPHP/Lib/Think/Core/View.class.php

http://tpblog.googlecode.com/
PHP | 486 lines | 258 code | 21 blank | 207 comment | 46 complexity | 66e4ac0672520c91f7c23227176f4b69 MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ThinkPHP ????
  15. * ?????????
  16. +------------------------------------------------------------------------------
  17. * @category Think
  18. * @package Think
  19. * @subpackage Core
  20. * @author liu21st <liu21st@gmail.com>
  21. * @version $Id$
  22. +------------------------------------------------------------------------------
  23. */
  24. class View extends Base
  25. {
  26. /**
  27. +----------------------------------------------------------
  28. * ????????????????????????
  29. +----------------------------------------------------------
  30. * @var array
  31. * @access protected
  32. +----------------------------------------------------------
  33. */
  34. protected $tVar = array();
  35. protected $trace = array();
  36. // ?????????
  37. protected $type = '';
  38. /**
  39. +----------------------------------------------------------
  40. * ????????
  41. +----------------------------------------------------------
  42. * @access public
  43. +----------------------------------------------------------
  44. * @return Template
  45. +----------------------------------------------------------
  46. */
  47. static function getInstance() {
  48. return get_instance_of(__CLASS__);
  49. }
  50. // ????
  51. public function __construct($type='') {
  52. if(!empty($type)) {
  53. $this->type = $type;
  54. }else{
  55. $this->type = strtoupper(C('TMPL_ENGINE_TYPE'));
  56. }
  57. }
  58. /**
  59. +----------------------------------------------------------
  60. * ??????
  61. +----------------------------------------------------------
  62. * @access public
  63. +----------------------------------------------------------
  64. * @param mixed $name
  65. * @param mixed $value
  66. +----------------------------------------------------------
  67. */
  68. public function assign($name,$value=''){
  69. if(is_array($name)) {
  70. $this->tVar = array_merge($this->tVar,$name);
  71. }elseif(is_object($name)){
  72. foreach($name as $key =>$val)
  73. {
  74. $this->tVar[$key] = $val;
  75. }
  76. }else {
  77. $this->tVar[$name] = $value;
  78. }
  79. }
  80. /**
  81. +----------------------------------------------------------
  82. * Trace????
  83. +----------------------------------------------------------
  84. * @access public
  85. +----------------------------------------------------------
  86. * @param mixed $name
  87. * @param mixed $value
  88. +----------------------------------------------------------
  89. */
  90. public function trace($title,$value='') {
  91. if(is_array($title)) {
  92. $this->trace = array_merge($this->trace,$title);
  93. }else {
  94. $this->trace[$title] = $value;
  95. }
  96. }
  97. /**
  98. +----------------------------------------------------------
  99. * ????????
  100. +----------------------------------------------------------
  101. * @access public
  102. +----------------------------------------------------------
  103. * @param string $name
  104. +----------------------------------------------------------
  105. * @return mixed
  106. +----------------------------------------------------------
  107. * @throws ThinkExecption
  108. +----------------------------------------------------------
  109. */
  110. public function get($name){
  111. if(isset($this->tVar[$name])) {
  112. return $this->tVar[$name];
  113. }else {
  114. return false;
  115. }
  116. }
  117. public function __set($name,$value) {
  118. $this->assign($name,$value);
  119. }
  120. public function __get($name) {
  121. return $this->get($name);
  122. }
  123. /**
  124. +----------------------------------------------------------
  125. * ????????? ????????
  126. +----------------------------------------------------------
  127. * @access public
  128. +----------------------------------------------------------
  129. * @param string $templateFile ????? ???????
  130. * @param string $charset ???????
  131. * @param string $contentType ????
  132. * @param string $varPrefix ??????
  133. +----------------------------------------------------------
  134. * @return mixed
  135. +----------------------------------------------------------
  136. * @throws ThinkExecption
  137. +----------------------------------------------------------
  138. */
  139. public function display($templateFile='',$charset='',$contentType='text/html',$varPrefix='')
  140. {
  141. $this->fetch($templateFile,$charset,$contentType,$varPrefix,true);
  142. }
  143. /**
  144. +----------------------------------------------------------
  145. * ????????????????????????
  146. +----------------------------------------------------------
  147. * @access protected
  148. +----------------------------------------------------------
  149. * @param string $startTime ????
  150. +----------------------------------------------------------
  151. * @return string
  152. +----------------------------------------------------------
  153. * @throws ThinkExecption
  154. +----------------------------------------------------------
  155. */
  156. protected function showTime($startTime) {
  157. if(C('SHOW_RUN_TIME')) {
  158. // ??????
  159. $endTime = microtime(TRUE);
  160. $total_run_time = number_format(($endTime - $GLOBALS['_beginTime']), 3);
  161. $showTime = 'Process: '.$total_run_time.'s ';
  162. if(C('SHOW_ADV_TIME')) {
  163. // ????????
  164. $_load_time = number_format(($GLOBALS['_loadTime'] -$GLOBALS['_beginTime'] ), 3);
  165. $_init_time = number_format(($GLOBALS['_initTime'] -$GLOBALS['_loadTime'] ), 3);
  166. $_exec_time = number_format(($startTime -$GLOBALS['_initTime'] ), 3);
  167. $_parse_time = number_format(($endTime - $startTime), 3);
  168. $showTime .= '( Load:'.$_load_time.'s Init:'.$_init_time.'s Exec:'.$_exec_time.'s Template:'.$_parse_time.'s )';
  169. }
  170. if(C('SHOW_DB_TIMES') && class_exists('Db',false) ) {
  171. // ?????????
  172. $db = Db::getInstance();
  173. $showTime .= ' | DB :'.$db->Q().' queries '.$db->W().' writes ';
  174. }
  175. if(C('SHOW_CACHE_TIMES') && class_exists('Cache',false)) {
  176. // ?????????
  177. $cache = Cache::getInstance();
  178. $showTime .= ' | Cache :'.$cache->Q().' gets '.$cache->W().' writes ';
  179. }
  180. if(MEMORY_LIMIT_ON && C('SHOW_USE_MEM')) {
  181. // ??????
  182. $startMem = array_sum(explode(' ', $GLOBALS['_startUseMems']));
  183. $endMem = array_sum(explode(' ', memory_get_usage()));
  184. $showTime .= ' | UseMem:'. number_format(($endMem - $startMem)/1024).' kb';
  185. }
  186. return $showTime;
  187. }
  188. }
  189. /**
  190. +----------------------------------------------------------
  191. * ??????
  192. +----------------------------------------------------------
  193. * @access public
  194. +----------------------------------------------------------
  195. * @param string $layout ??????????
  196. * @param string $charset ????
  197. * @param string $contentType ????
  198. * @param string $varPrefix ??????
  199. * @param string $display ??????
  200. +----------------------------------------------------------
  201. * @return void
  202. +----------------------------------------------------------
  203. */
  204. public function layout($layoutFile='',$charset='',$contentType='text/html',$varPrefix='',$display=true)
  205. {
  206. $startTime = microtime(TRUE);
  207. if(empty($layoutFile)) {
  208. // ???????????????
  209. $layoutFile = C('DEFAULT_LAYOUT');
  210. }
  211. if(false === strpos($layoutFile,':')) {
  212. // ????layout???????
  213. // ?????????? ?? Public:index
  214. $layoutFile = 'Layout:'.$layoutFile;
  215. }
  216. // ????????
  217. $content = $this->fetch($layoutFile,$charset,$contentType,$varPrefix);
  218. // ?????????
  219. $find = preg_match_all('/<!-- layout::(.+?)::(.+?) -->/is',$content,$matches);
  220. if($find) {
  221. for ($i=0; $i< $find; $i++) {
  222. // ???????????????
  223. if(0===strpos($matches[1][$i],'$')){
  224. // ????
  225. $matches[1][$i] = $this->get(substr($matches[1][$i],1));
  226. }
  227. if(0 != $matches[2][$i] ) {
  228. // ???????
  229. // ??????????
  230. $guid = md5($matches[1][$i]);
  231. $cache = S($guid);
  232. if($cache) {
  233. $layoutContent = $cache;
  234. }else{
  235. $layoutContent = $this->fetch($matches[1][$i],$charset,$contentType,$varPrefix);
  236. S($guid,$layoutContent,$matches[2][$i]);
  237. }
  238. }else{
  239. $layoutContent = $this->fetch($matches[1][$i],$charset,$contentType,$varPrefix);
  240. }
  241. $content = str_replace($matches[0][$i],$layoutContent,$content);
  242. }
  243. }
  244. if($display) {
  245. $showTime = $this->showTime($startTime);
  246. echo $content;
  247. if(C('SHOW_RUN_TIME')) {
  248. echo '<div id="think_run_time" class="think_run_time">'.$showTime.'</div>';
  249. }
  250. $this->showTrace($showTime,$charset);
  251. return null;
  252. }else{
  253. return $content;
  254. }
  255. }
  256. /**
  257. +----------------------------------------------------------
  258. * ??????????
  259. * ???????????
  260. +----------------------------------------------------------
  261. * @access public
  262. +----------------------------------------------------------
  263. * @param string $tmplTemplateFile ?????
  264. +----------------------------------------------------------
  265. * @return boolen
  266. +----------------------------------------------------------
  267. * @throws ThinkExecption
  268. +----------------------------------------------------------
  269. */
  270. protected function checkCache($tmplTemplateFile)
  271. {
  272. $tmplCacheFile = CACHE_PATH.md5($tmplTemplateFile).C('CACHFILE_SUFFIX');
  273. if(!file_exists($tmplCacheFile)){
  274. return false;
  275. }
  276. elseif (!C('TMPL_CACHE_ON')){
  277. return false;
  278. }elseif (filemtime($tmplTemplateFile) > filemtime($tmplCacheFile)) {
  279. // ????????????????
  280. return false;
  281. } elseif (C('TMPL_CACHE_TIME') != -1 && time() > filemtime($tmplCacheFile)+C('TMPL_CACHE_TIME')) {
  282. // ????????
  283. return false;
  284. }
  285. //????
  286. return true;
  287. }
  288. /**
  289. +----------------------------------------------------------
  290. * ?????????
  291. +----------------------------------------------------------
  292. * @access public
  293. +----------------------------------------------------------
  294. * @param string $templateFile ????? ???????
  295. * @param string $charset ???????
  296. * @param string $contentType ????
  297. * @param string $varPrefix ??????
  298. * @param integer $mode 0 ?? 1 ?? 2 ??
  299. +----------------------------------------------------------
  300. * @throws ThinkExecption
  301. +----------------------------------------------------------
  302. */
  303. public function fetch($templateFile='',$charset='',$contentType='text/html',$varPrefix='',$display=false)
  304. {
  305. $startTime = microtime(TRUE);
  306. if(null===$templateFile) {
  307. // ??null?????????????????
  308. return ;
  309. }
  310. if('layout::'==substr($templateFile,0,8)) {
  311. $this->layout(substr($templateFile,8));
  312. return ;
  313. }
  314. if(empty($charset)) {
  315. $charset = C('OUTPUT_CHARSET');
  316. }
  317. // ??????
  318. header("Content-Type:".$contentType."; charset=".$charset);
  319. header("Cache-control: private"); //??????
  320. // ??????
  321. ini_set('output_buffering',4096);
  322. $zlibCompress = ini_get('zlib.output_compression');
  323. if(empty($zlibCompress) && function_exists('ini_set')) {
  324. ini_set( 'zlib.output_compression', 1 );
  325. }
  326. $pluginOn = C('THINK_PLUGIN_ON');
  327. if($pluginOn) {
  328. // ???????
  329. apply_filter('ob_init');
  330. }
  331. //????
  332. ob_start();
  333. ob_implicit_flush(0);
  334. if($pluginOn) {
  335. // ??????????
  336. apply_filter('ob_start');
  337. // ???????
  338. $templateFile = apply_filter('template_file',$templateFile);
  339. }
  340. if(''==$templateFile) {
  341. // ????????? ????????
  342. $templateFile = C('TMPL_FILE_NAME');
  343. }elseif(strpos($templateFile,'@')){
  344. // ??????????? ???????? ?? blue@User:add
  345. $templateFile = TMPL_PATH.str_replace(array('@',':'),'/',$templateFile).C('TEMPLATE_SUFFIX');
  346. }elseif(strpos($templateFile,':')){
  347. // ???????????
  348. $templateFile = TEMPLATE_PATH.'/'.str_replace(':','/',$templateFile).C('TEMPLATE_SUFFIX');
  349. }elseif(!file_exists($templateFile)) {
  350. // ?????????????
  351. $templateFile = dirname(C('TMPL_FILE_NAME')).'/'.$templateFile.C('TEMPLATE_SUFFIX');
  352. }
  353. if(!file_exists_case($templateFile)){
  354. throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
  355. }
  356. if($pluginOn) {
  357. // ??????
  358. $this->tVar = apply_filter('template_var',$this->tVar);
  359. }
  360. $compiler = false;
  361. //????????????
  362. if('PHP'==$this->type || empty($this->type)) {
  363. // ??????????????
  364. extract($this->tVar, empty($varPrefix)? EXTR_OVERWRITE : EXTR_PREFIX_ALL,$varPrefix);
  365. // ????PHP??
  366. include $templateFile;
  367. }elseif('THINK'==$this->type){
  368. // ?????ThinkTemplate????
  369. if(!$this->checkCache($templateFile)) {
  370. // ???? ????
  371. $compiler = true;
  372. import('Think.Template.ThinkTemplate');
  373. $tpl = ThinkTemplate::getInstance();
  374. // ?????????
  375. $tpl->load($templateFile,$charset,$this->tVar,$varPrefix);
  376. }else{
  377. // ???? ????????
  378. // ??????????????
  379. extract($this->tVar, empty($varPrefix)? EXTR_OVERWRITE : EXTR_PREFIX_ALL,$varPrefix);
  380. //????????
  381. include CACHE_PATH.md5($templateFile).C('CACHFILE_SUFFIX');
  382. }
  383. }elseif($pluginOn) {
  384. // ????????????????
  385. use_compiler(C('TMPL_ENGINE_TYPE'),$templateFile,$this->tVar,$charset,$varPrefix);
  386. }
  387. // ???????
  388. $content = ob_get_clean();
  389. // ??????
  390. $content = auto_charset($content,C('TEMPLATE_CHARSET'),$charset);
  391. if($pluginOn) {
  392. // ????
  393. $content = apply_filter('ob_content',$content);
  394. }
  395. if(C('HTML_CACHE_ON')) {
  396. // ??????
  397. HtmlCache::writeHTMLCache($content);
  398. }
  399. if($display) {
  400. $showTime = $this->showTime($startTime);
  401. echo $content;
  402. if(C('SHOW_RUN_TIME')) {
  403. echo '<div id="think_run_time" class="think_run_time">'.$showTime.'</div>';
  404. }
  405. $this->showTrace($showTime,$charset,$compiler);
  406. return null;
  407. }else {
  408. return $content;
  409. }
  410. }
  411. /**
  412. +----------------------------------------------------------
  413. * ????Trace??
  414. +----------------------------------------------------------
  415. * @access protected
  416. +----------------------------------------------------------
  417. * @param string $showTime ??????
  418. * @param string $charset ???????
  419. * @param boolean $compiler ??????
  420. +----------------------------------------------------------
  421. * @throws ThinkExecption
  422. +----------------------------------------------------------
  423. */
  424. protected function showTrace($showTime,$charset,$compiler=true){
  425. if(C('SHOW_PAGE_TRACE')) {
  426. // ????Trace?? ??Trace????
  427. // ???? return array('????'=>$_SERVER['PHP_SELF'],'????'=>$_SERVER['SERVER_PROTOCOL'],...);
  428. $traceFile = CONFIG_PATH.'trace.php';
  429. if(file_exists_case($traceFile)) {
  430. $_trace = include $traceFile;
  431. }else{
  432. $_trace = array();
  433. }
  434. // ????????
  435. $this->trace('????', $_SERVER['PHP_SELF']);
  436. $this->trace('????', $_SERVER['REQUEST_METHOD']);
  437. $this->trace('????', $_SERVER['SERVER_PROTOCOL']);
  438. $this->trace('????', date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']));
  439. $this->trace('????', $_SERVER['HTTP_USER_AGENT']);
  440. $this->trace('??ID' , session_id());
  441. $this->trace('????', $showTime);
  442. $this->trace('????', $charset);
  443. $this->trace('????', $GLOBALS['include_file']);
  444. $this->trace('????', !empty($compiler)?'????':'????');
  445. if(isset(Log::$log[SQL_LOG_DEBUG])) {
  446. $log = Log::$log[SQL_LOG_DEBUG];
  447. $this->trace('SQL??',is_array($log)?count($log).'?SQL<br/>'.implode('<br/>',$log):'?SQL??');
  448. }else{
  449. $this->trace('SQL??','?SQL??');
  450. }
  451. if(isset(Log::$log[WEB_LOG_ERROR])) {
  452. $log = Log::$log[WEB_LOG_ERROR];
  453. $this->trace('????',is_array($log)?count($log).'???<br/>'.implode('<br/>',$log):'?????');
  454. }else{
  455. $this->trace('????','?????');
  456. }
  457. $_trace = array_merge($_trace,$this->trace);
  458. $_trace = auto_charset($_trace,'utf-8');
  459. $_title = auto_charset('??Trace??','utf-8');
  460. // ??Trace????
  461. include THINK_PATH.'/Tpl/PageTrace.tpl.php';
  462. }
  463. }
  464. }//
  465. ?>