/trunk/Tpblog/ThinkPHP/Lib/Think/Core/View.class.php
PHP | 486 lines | 258 code | 21 blank | 207 comment | 46 complexity | 66e4ac0672520c91f7c23227176f4b69 MD5 | raw file
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP
- // +----------------------------------------------------------------------
- // | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // $Id$
-
- /**
- +------------------------------------------------------------------------------
- * ThinkPHP ????
- * ?????????
- +------------------------------------------------------------------------------
- * @category Think
- * @package Think
- * @subpackage Core
- * @author liu21st <liu21st@gmail.com>
- * @version $Id$
- +------------------------------------------------------------------------------
- */
- class View extends Base
- {
- /**
- +----------------------------------------------------------
- * ????????????????????????
- +----------------------------------------------------------
- * @var array
- * @access protected
- +----------------------------------------------------------
- */
- protected $tVar = array();
-
- protected $trace = array();
-
- // ?????????
- protected $type = '';
-
- /**
- +----------------------------------------------------------
- * ????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return Template
- +----------------------------------------------------------
- */
- static function getInstance() {
- return get_instance_of(__CLASS__);
- }
-
- // ????
- public function __construct($type='') {
- if(!empty($type)) {
- $this->type = $type;
- }else{
- $this->type = strtoupper(C('TMPL_ENGINE_TYPE'));
- }
- }
-
- /**
- +----------------------------------------------------------
- * ??????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param mixed $name
- * @param mixed $value
- +----------------------------------------------------------
- */
- public function assign($name,$value=''){
- if(is_array($name)) {
- $this->tVar = array_merge($this->tVar,$name);
- }elseif(is_object($name)){
- foreach($name as $key =>$val)
- {
- $this->tVar[$key] = $val;
- }
- }else {
- $this->tVar[$name] = $value;
- }
- }
-
- /**
- +----------------------------------------------------------
- * Trace????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param mixed $name
- * @param mixed $value
- +----------------------------------------------------------
- */
- public function trace($title,$value='') {
- if(is_array($title)) {
- $this->trace = array_merge($this->trace,$title);
- }else {
- $this->trace[$title] = $value;
- }
- }
-
- /**
- +----------------------------------------------------------
- * ????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $name
- +----------------------------------------------------------
- * @return mixed
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- public function get($name){
- if(isset($this->tVar[$name])) {
- return $this->tVar[$name];
- }else {
- return false;
- }
- }
-
- public function __set($name,$value) {
- $this->assign($name,$value);
- }
-
- public function __get($name) {
- return $this->get($name);
- }
-
- /**
- +----------------------------------------------------------
- * ????????? ????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $templateFile ????? ???????
- * @param string $charset ???????
- * @param string $contentType ????
- * @param string $varPrefix ??????
- +----------------------------------------------------------
- * @return mixed
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- public function display($templateFile='',$charset='',$contentType='text/html',$varPrefix='')
- {
- $this->fetch($templateFile,$charset,$contentType,$varPrefix,true);
- }
-
- /**
- +----------------------------------------------------------
- * ????????????????????????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param string $startTime ????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- protected function showTime($startTime) {
- if(C('SHOW_RUN_TIME')) {
- // ??????
- $endTime = microtime(TRUE);
- $total_run_time = number_format(($endTime - $GLOBALS['_beginTime']), 3);
- $showTime = 'Process: '.$total_run_time.'s ';
- if(C('SHOW_ADV_TIME')) {
- // ????????
- $_load_time = number_format(($GLOBALS['_loadTime'] -$GLOBALS['_beginTime'] ), 3);
- $_init_time = number_format(($GLOBALS['_initTime'] -$GLOBALS['_loadTime'] ), 3);
- $_exec_time = number_format(($startTime -$GLOBALS['_initTime'] ), 3);
- $_parse_time = number_format(($endTime - $startTime), 3);
- $showTime .= '( Load:'.$_load_time.'s Init:'.$_init_time.'s Exec:'.$_exec_time.'s Template:'.$_parse_time.'s )';
- }
- if(C('SHOW_DB_TIMES') && class_exists('Db',false) ) {
- // ?????????
- $db = Db::getInstance();
- $showTime .= ' | DB :'.$db->Q().' queries '.$db->W().' writes ';
- }
- if(C('SHOW_CACHE_TIMES') && class_exists('Cache',false)) {
- // ?????????
- $cache = Cache::getInstance();
- $showTime .= ' | Cache :'.$cache->Q().' gets '.$cache->W().' writes ';
- }
- if(MEMORY_LIMIT_ON && C('SHOW_USE_MEM')) {
- // ??????
- $startMem = array_sum(explode(' ', $GLOBALS['_startUseMems']));
- $endMem = array_sum(explode(' ', memory_get_usage()));
- $showTime .= ' | UseMem:'. number_format(($endMem - $startMem)/1024).' kb';
- }
- return $showTime;
- }
- }
-
- /**
- +----------------------------------------------------------
- * ??????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $layout ??????????
- * @param string $charset ????
- * @param string $contentType ????
- * @param string $varPrefix ??????
- * @param string $display ??????
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- public function layout($layoutFile='',$charset='',$contentType='text/html',$varPrefix='',$display=true)
- {
- $startTime = microtime(TRUE);
- if(empty($layoutFile)) {
- // ???????????????
- $layoutFile = C('DEFAULT_LAYOUT');
- }
- if(false === strpos($layoutFile,':')) {
- // ????layout???????
- // ?????????? ?? Public:index
- $layoutFile = 'Layout:'.$layoutFile;
- }
- // ????????
- $content = $this->fetch($layoutFile,$charset,$contentType,$varPrefix);
- // ?????????
- $find = preg_match_all('/<!-- layout::(.+?)::(.+?) -->/is',$content,$matches);
- if($find) {
- for ($i=0; $i< $find; $i++) {
- // ???????????????
- if(0===strpos($matches[1][$i],'$')){
- // ????
- $matches[1][$i] = $this->get(substr($matches[1][$i],1));
- }
- if(0 != $matches[2][$i] ) {
- // ???????
- // ??????????
- $guid = md5($matches[1][$i]);
- $cache = S($guid);
- if($cache) {
- $layoutContent = $cache;
- }else{
- $layoutContent = $this->fetch($matches[1][$i],$charset,$contentType,$varPrefix);
- S($guid,$layoutContent,$matches[2][$i]);
- }
- }else{
- $layoutContent = $this->fetch($matches[1][$i],$charset,$contentType,$varPrefix);
- }
- $content = str_replace($matches[0][$i],$layoutContent,$content);
- }
- }
- if($display) {
- $showTime = $this->showTime($startTime);
- echo $content;
- if(C('SHOW_RUN_TIME')) {
- echo '<div id="think_run_time" class="think_run_time">'.$showTime.'</div>';
- }
- $this->showTrace($showTime,$charset);
- return null;
- }else{
- return $content;
- }
- }
-
- /**
- +----------------------------------------------------------
- * ??????????
- * ???????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $tmplTemplateFile ?????
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- protected function checkCache($tmplTemplateFile)
- {
- $tmplCacheFile = CACHE_PATH.md5($tmplTemplateFile).C('CACHFILE_SUFFIX');
- if(!file_exists($tmplCacheFile)){
- return false;
- }
- elseif (!C('TMPL_CACHE_ON')){
- return false;
- }elseif (filemtime($tmplTemplateFile) > filemtime($tmplCacheFile)) {
- // ????????????????
- return false;
- } elseif (C('TMPL_CACHE_TIME') != -1 && time() > filemtime($tmplCacheFile)+C('TMPL_CACHE_TIME')) {
- // ????????
- return false;
- }
- //????
- return true;
- }
-
- /**
- +----------------------------------------------------------
- * ?????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $templateFile ????? ???????
- * @param string $charset ???????
- * @param string $contentType ????
- * @param string $varPrefix ??????
- * @param integer $mode 0 ?? 1 ?? 2 ??
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- public function fetch($templateFile='',$charset='',$contentType='text/html',$varPrefix='',$display=false)
- {
- $startTime = microtime(TRUE);
- if(null===$templateFile) {
- // ??null?????????????????
- return ;
- }
- if('layout::'==substr($templateFile,0,8)) {
- $this->layout(substr($templateFile,8));
- return ;
- }
- if(empty($charset)) {
- $charset = C('OUTPUT_CHARSET');
- }
- // ??????
- header("Content-Type:".$contentType."; charset=".$charset);
- header("Cache-control: private"); //??????
-
- // ??????
- ini_set('output_buffering',4096);
- $zlibCompress = ini_get('zlib.output_compression');
- if(empty($zlibCompress) && function_exists('ini_set')) {
- ini_set( 'zlib.output_compression', 1 );
- }
- $pluginOn = C('THINK_PLUGIN_ON');
- if($pluginOn) {
- // ???????
- apply_filter('ob_init');
- }
- //????
- ob_start();
- ob_implicit_flush(0);
- if($pluginOn) {
- // ??????????
- apply_filter('ob_start');
- // ???????
- $templateFile = apply_filter('template_file',$templateFile);
- }
-
- if(''==$templateFile) {
- // ????????? ????????
- $templateFile = C('TMPL_FILE_NAME');
- }elseif(strpos($templateFile,'@')){
- // ??????????? ???????? ?? blue@User:add
- $templateFile = TMPL_PATH.str_replace(array('@',':'),'/',$templateFile).C('TEMPLATE_SUFFIX');
- }elseif(strpos($templateFile,':')){
- // ???????????
- $templateFile = TEMPLATE_PATH.'/'.str_replace(':','/',$templateFile).C('TEMPLATE_SUFFIX');
- }elseif(!file_exists($templateFile)) {
- // ?????????????
- $templateFile = dirname(C('TMPL_FILE_NAME')).'/'.$templateFile.C('TEMPLATE_SUFFIX');
- }
-
- if(!file_exists_case($templateFile)){
- throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
- }
- if($pluginOn) {
- // ??????
- $this->tVar = apply_filter('template_var',$this->tVar);
- }
- $compiler = false;
- //????????????
- if('PHP'==$this->type || empty($this->type)) {
- // ??????????????
- extract($this->tVar, empty($varPrefix)? EXTR_OVERWRITE : EXTR_PREFIX_ALL,$varPrefix);
- // ????PHP??
- include $templateFile;
- }elseif('THINK'==$this->type){
- // ?????ThinkTemplate????
- if(!$this->checkCache($templateFile)) {
- // ???? ????
- $compiler = true;
- import('Think.Template.ThinkTemplate');
- $tpl = ThinkTemplate::getInstance();
- // ?????????
- $tpl->load($templateFile,$charset,$this->tVar,$varPrefix);
- }else{
- // ???? ????????
- // ??????????????
- extract($this->tVar, empty($varPrefix)? EXTR_OVERWRITE : EXTR_PREFIX_ALL,$varPrefix);
- //????????
- include CACHE_PATH.md5($templateFile).C('CACHFILE_SUFFIX');
- }
- }elseif($pluginOn) {
- // ????????????????
- use_compiler(C('TMPL_ENGINE_TYPE'),$templateFile,$this->tVar,$charset,$varPrefix);
- }
- // ???????
- $content = ob_get_clean();
- // ??????
- $content = auto_charset($content,C('TEMPLATE_CHARSET'),$charset);
- if($pluginOn) {
- // ????
- $content = apply_filter('ob_content',$content);
- }
-
- if(C('HTML_CACHE_ON')) {
- // ??????
- HtmlCache::writeHTMLCache($content);
- }
-
- if($display) {
- $showTime = $this->showTime($startTime);
- echo $content;
- if(C('SHOW_RUN_TIME')) {
- echo '<div id="think_run_time" class="think_run_time">'.$showTime.'</div>';
- }
- $this->showTrace($showTime,$charset,$compiler);
- return null;
- }else {
- return $content;
- }
- }
-
- /**
- +----------------------------------------------------------
- * ????Trace??
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param string $showTime ??????
- * @param string $charset ???????
- * @param boolean $compiler ??????
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- protected function showTrace($showTime,$charset,$compiler=true){
- if(C('SHOW_PAGE_TRACE')) {
- // ????Trace?? ??Trace????
- // ???? return array('????'=>$_SERVER['PHP_SELF'],'????'=>$_SERVER['SERVER_PROTOCOL'],...);
- $traceFile = CONFIG_PATH.'trace.php';
- if(file_exists_case($traceFile)) {
- $_trace = include $traceFile;
- }else{
- $_trace = array();
- }
- // ????????
- $this->trace('????', $_SERVER['PHP_SELF']);
- $this->trace('????', $_SERVER['REQUEST_METHOD']);
- $this->trace('????', $_SERVER['SERVER_PROTOCOL']);
- $this->trace('????', date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']));
- $this->trace('????', $_SERVER['HTTP_USER_AGENT']);
- $this->trace('??ID' , session_id());
- $this->trace('????', $showTime);
- $this->trace('????', $charset);
- $this->trace('????', $GLOBALS['include_file']);
- $this->trace('????', !empty($compiler)?'????':'????');
- if(isset(Log::$log[SQL_LOG_DEBUG])) {
- $log = Log::$log[SQL_LOG_DEBUG];
- $this->trace('SQL??',is_array($log)?count($log).'?SQL<br/>'.implode('<br/>',$log):'?SQL??');
- }else{
- $this->trace('SQL??','?SQL??');
- }
- if(isset(Log::$log[WEB_LOG_ERROR])) {
- $log = Log::$log[WEB_LOG_ERROR];
- $this->trace('????',is_array($log)?count($log).'???<br/>'.implode('<br/>',$log):'?????');
- }else{
- $this->trace('????','?????');
- }
- $_trace = array_merge($_trace,$this->trace);
- $_trace = auto_charset($_trace,'utf-8');
- $_title = auto_charset('??Trace??','utf-8');
- // ??Trace????
- include THINK_PATH.'/Tpl/PageTrace.tpl.php';
- }
- }
- }//
- ?>