PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/sop/2.0beta1/ThinkPHP/Mode/Compat/View.class.php

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