From d28e27270c8be2775911bc3aa84ec86e7c1197ce Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 24 Feb 2015 10:45:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B1=BB=E5=BA=93=E5=88=AB=E5=90=8D=E5=92=8C?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E4=BF=AE=E6=AD=A3=20=E5=A2=9E=E5=8A=A0sae?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E9=A9=B1=E5=8A=A8=20=E6=94=B9=E8=BF=9BHook?= =?UTF-8?q?=E7=B1=BB=E7=9A=84exec=E6=96=B9=E6=B3=95=20=E8=A1=8C=E4=B8=BA?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E4=BC=98=E5=85=88=E6=A3=80=E6=B5=8B=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E6=96=B9=E6=B3=95=20=E4=B8=8D=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E5=88=99=E8=B0=83=E7=94=A8run=E6=96=B9=E6=B3=95=20NOW=5FTIME?= =?UTF-8?q?=E6=94=B9=E7=94=A8REQUEST=5FTIME=5FFLOAT=20=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.php | 2 +- library/behavior/content_replace.php | 6 +-- library/behavior/location_template.php | 9 ++--- library/behavior/show_page_trace.php | 4 +- library/think/cache.php | 2 +- library/think/config.php | 34 ++++++++-------- library/think/debug.php | 34 +++++++++------- library/think/hook.php | 6 +-- library/think/lang.php | 18 ++++----- library/think/log/driver/sae.php | 49 +++++++++++++++++++++++ library/think/template.php | 4 +- library/think/view.php | 2 +- library/think/view/driver/think.php | 2 +- mode/common.php | 54 +++++++++++++------------- mode/sae.php | 18 +++++---- start.php | 6 +-- 16 files changed, 148 insertions(+), 102 deletions(-) create mode 100644 library/think/log/driver/sae.php diff --git a/base.php b/base.php index 0124b472..c72365eb 100644 --- a/base.php +++ b/base.php @@ -40,7 +40,7 @@ define('IS_CGI', strpos(PHP_SAPI, 'cgi') === 0 ? 1 : 0); define('IS_WIN', strstr(PHP_OS, 'WIN') ? 1 : 0); define('IS_CLI', PHP_SAPI=='cli' ? 1 : 0); define('IS_AJAX', (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false); -define('NOW_TIME', $_SERVER['REQUEST_TIME']); +define('NOW_TIME', $_SERVER['REQUEST_TIME_FLOAT']); define('REQUEST_METHOD', $_SERVER['REQUEST_METHOD']); define('IS_GET', REQUEST_METHOD =='GET' ? true : false); define('IS_POST', REQUEST_METHOD =='POST' ? true : false); diff --git a/library/behavior/content_replace.php b/library/behavior/content_replace.php index 737a1af2..a8bba806 100644 --- a/library/behavior/content_replace.php +++ b/library/behavior/content_replace.php @@ -10,14 +10,10 @@ // +---------------------------------------------------------------------- namespace think\behavior; -use think\config; +use think\Config; /** * 系统行为扩展:模板内容输出替换 - * @category Think - * @package Think - * @subpackage Behavior - * @author liu21st */ class ContentReplace { diff --git a/library/behavior/location_template.php b/library/behavior/location_template.php index e14b20f6..7683e1ca 100644 --- a/library/behavior/location_template.php +++ b/library/behavior/location_template.php @@ -13,17 +13,14 @@ namespace think\behavior; /** * 系统行为扩展:定位模板文件 - * @category Think - * @package Think - * @subpackage Behavior - * @author liu21st */ class LocationTemplate { // 行为扩展的执行入口必须是run public function run(&$templateFile){ // 自动定位模板文件 - if(!is_file($templateFile)) + if(!is_file($templateFile)){ $templateFile = $this->parseTemplateFile($templateFile); + } } /** @@ -42,7 +39,7 @@ class LocationTemplate { }elseif(false === strpos($template,'.')) { $template = $template; } - $templateFile = MODULE_PATH.'View/'.$template.'.html'; + $templateFile = MODULE_PATH.'view/'.$template.'.html'; return $templateFile; } } diff --git a/library/behavior/show_page_trace.php b/library/behavior/show_page_trace.php index 7d56869f..dc989119 100644 --- a/library/behavior/show_page_trace.php +++ b/library/behavior/show_page_trace.php @@ -42,7 +42,7 @@ class ShowPageTrace { $info[] = $file.' ( '.number_format(filesize($file)/1024,2).' KB )'; } $trace = []; - Debug::remark('START',$GLOBALS['startTime']); + Debug::remark('START',NOW_TIME); $base = [ '请求信息' => date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']).' '.$_SERVER['SERVER_PROTOCOL'].' '.$_SERVER['REQUEST_METHOD'].' : '.$_SERVER['PHP_SELF'], '运行时间' => Debug::getUseTime('START','END',6).'s', @@ -84,7 +84,7 @@ class ShowPageTrace { unset($files,$info,$base,$debug); // 调用Trace页面模板 ob_start(); - include Config::has('tmpl_trace_file')?Config::get('tmpl_trace_file'):THINK_PATH.'tpl/page_trace.tpl'; + include Config::has('tmpl_trace_file') ? Config::get('tmpl_trace_file') : THINK_PATH.'tpl/page_trace.tpl'; return ob_get_clean(); } } diff --git a/library/think/cache.php b/library/think/cache.php index b2dee9a8..4b613a70 100644 --- a/library/think/cache.php +++ b/library/think/cache.php @@ -27,7 +27,7 @@ class Cache { */ static public function connect($options=[]) { $type = !empty($options['type'])?$options['type']:'File'; - $class = 'Think\\Cache\\Driver\\'.ucwords($type); + $class = 'think\\cache\\driver\\'.ucwords($type); self::$handler = new $class($options); return self::$handler; } diff --git a/library/think/config.php b/library/think/config.php index c44fe49f..af99bab1 100644 --- a/library/think/config.php +++ b/library/think/config.php @@ -12,12 +12,12 @@ namespace think; class Config { - static private $_config = []; // 配置参数 - static private $_range = '_sys_'; // 参数作用域 + static private $config = []; // 配置参数 + static private $range = '_sys_'; // 参数作用域 // 设定配置参数的作用域 static public function range($range){ - self::$_range = $range; + self::$range = $range; } // 解析其他格式的配置参数 @@ -36,55 +36,55 @@ class Config { // 检测配置是否存在 static public function has($name,$range=''){ - $range = $range ? $range : self::$_range; + $range = $range ? $range : self::$range; $name = strtolower($name); if (!strpos($name, '.')) { - return isset(self::$_config[$range][$name]); + return isset(self::$config[$range][$name]); }else{ // 二维数组设置和获取支持 $name = explode('.', $name); - return isset(self::$_config[$range][$name[0]][$name[1]]); + return isset(self::$config[$range][$name[0]][$name[1]]); } } // 获取配置参数 为空则获取所有配置 static public function get($name=null,$range='') { - $range = $range ? $range : self::$_range; + $range = $range ? $range : self::$range; // 无参数时获取所有 if (empty($name)) { - return self::$_config[$range]; + return self::$config[$range]; } $name = strtolower($name); if (!strpos($name, '.')) { - return isset(self::$_config[$range][$name]) ? self::$_config[$range][$name] : null; + return isset(self::$config[$range][$name]) ? self::$config[$range][$name] : null; }else{ // 二维数组设置和获取支持 $name = explode('.', $name); - return isset(self::$_config[$range][$name[0]][$name[1]]) ? self::$_config[$range][$name[0]][$name[1]] : null; + return isset(self::$config[$range][$name[0]][$name[1]]) ? self::$config[$range][$name[0]][$name[1]] : null; } } // 设置配置参数 name为数组则为批量设置 static public function set($name, $value=null,$range='') { - $range = $range ? $range : self::$_range; - if(!isset(self::$_config[$range])) { - self::$_config[$range] = []; + $range = $range ? $range : self::$range; + if(!isset(self::$config[$range])) { + self::$config[$range] = []; } if (is_string($name)) { $name = strtolower($name); if (!strpos($name, '.')) { - self::$_config[$range][$name] = $value; + self::$config[$range][$name] = $value; }else{ // 二维数组设置和获取支持 $name = explode('.', $name); - self::$_config[$range][$name[0]][$name[1]] = $value; + self::$config[$range][$name[0]][$name[1]] = $value; } return; }elseif (is_array($name)){ // 批量设置 - self::$_config[$range] = array_merge(self::$_config[$range], array_change_key_case($name)); - return self::$_config[$range]; + self::$config[$range] = array_merge(self::$config[$range], array_change_key_case($name)); + return self::$config[$range]; }else{ return null; // 避免非法参数 } diff --git a/library/think/debug.php b/library/think/debug.php index 91f26fcc..8ff58746 100644 --- a/library/think/debug.php +++ b/library/think/debug.php @@ -13,8 +13,8 @@ namespace think; class Debug { - static protected $_info = []; - static protected $_mem = []; + static protected $info = []; + static protected $mem = []; /** * 记录时间(微秒)和内存使用情况 @@ -24,10 +24,10 @@ class Debug { */ static public function remark($name,$value='') { // 记录时间和内存使用 - self::$_info[$name] = is_float($value) ? $value : microtime(true); + self::$info[$name] = is_float($value) ? $value : microtime(true); if('time' != $value ) { - self::$_mem['mem'][$name] = is_float($value) ? $value : memory_get_usage(); - self::$_mem['peak'][$name] = function_exists('memory_get_peak_usage') ? memory_get_peak_usage() : self::$_mem['mem'][$name]; + self::$mem['mem'][$name] = is_float($value) ? $value : memory_get_usage(); + self::$mem['peak'][$name] = memory_get_peak_usage(); } } @@ -39,9 +39,10 @@ class Debug { * @return mixed */ static public function getUseTime($start,$end,$dec=6) { - if(!isset(self::$_info[$end])) - self::$_info[$end] = microtime(true); - return number_format((self::$_info[$end]-self::$_info[$start]),$dec); + if(!isset(self::$info[$end])) { + self::$info[$end] = microtime(true); + } + return number_format((self::$info[$end]-self::$info[$start]),$dec); } /** @@ -52,9 +53,10 @@ class Debug { * @return mixed */ static public function getUseMem($start,$end,$dec=2) { - if(!isset(self::$_mem['mem'][$end])) - self::$_mem['mem'][$end] = memory_get_usage(); - $size = self::$_mem['mem'][$end]-self::$_mem['mem'][$start]; + if(!isset(self::$mem['mem'][$end])) { + self::$mem['mem'][$end] = memory_get_usage(); + } + $size = self::$mem['mem'][$end]-self::$mem['mem'][$start]; $a = ['B', 'KB', 'MB', 'GB', 'TB']; $pos = 0; while ($size >= 1024) { @@ -72,9 +74,10 @@ class Debug { * @return mixed */ static public function getMemPeak($start,$end,$dec=2) { - if(!isset(self::$_mem['peak'][$end])) - self::$_mem['peak'][$end] = function_exists('memory_get_peak_usage') ? memory_get_peak_usage() : memory_get_usage(); - $size = self::$_mem['peak'][$end]-self::$_mem['peak'][$start]; + if(!isset(self::$mem['peak'][$end])) { + self::$mem['peak'][$end] = memory_get_peak_usage() ; + } + $size = self::$mem['peak'][$end]-self::$mem['peak'][$start]; $a = ['B', 'KB', 'MB', 'GB', 'TB']; $pos = 0; while ($size >= 1024) { @@ -108,7 +111,8 @@ class Debug { if ($echo) { echo($output); return null; - }else + }else{ return $output; + } } } diff --git a/library/think/hook.php b/library/think/hook.php index d8bb4e2e..8129fcbb 100644 --- a/library/think/hook.php +++ b/library/think/hook.php @@ -76,11 +76,7 @@ class Hook { if($name instanceof \Closure) { return $name($params); } - if('Behavior' == substr($name,-8) ){ - // 行为扩展必须用run入口方法 - $tag = 'run'; - } $addon = new $name(); - return $addon->$tag($params); + return method_exists($addon,$tag) ? $addon->$tag($params): $addon->run($params); } } diff --git a/library/think/lang.php b/library/think/lang.php index 6a0e21a2..749398ee 100644 --- a/library/think/lang.php +++ b/library/think/lang.php @@ -12,12 +12,12 @@ namespace think; class Lang { - static private $_lang = []; // 语言参数 - static private $_range = '_sys_'; // 作用域 + static private $lang = []; // 语言参数 + static private $range = '_sys_'; // 作用域 // 设定语言参数的作用域 static public function range($range){ - self::$_range = $range; + self::$range = $range; } /** @@ -28,12 +28,12 @@ class Lang { * @return mixed */ static public function set($name, $value=null,$range='') { - $range = $range?$range:self::$_range; + $range = $range?$range:self::$range; // 批量定义 if (is_array($name)){ - return self::$_lang[$range] = array_merge(self::$_lang[$range], array_change_key_case($name)); + return self::$lang[$range] = array_merge(self::$lang[$range], array_change_key_case($name)); }else{ - return self::$_lang[$range][strtolower($name)] = $value; + return self::$lang[$range][strtolower($name)] = $value; } } @@ -44,11 +44,11 @@ class Lang { * @return mixed */ static public function get($name=null, $range='') { - $range = $range?$range:self::$_range; + $range = $range?$range:self::$range; // 空参数返回所有定义 if (empty($name)) - return self::$_lang[$range]; + return self::$lang[$range]; $name = strtolower($name); - return isset(self::$_lang[$range][$name]) ? self::$_lang[$range][$name] : $name; + return isset(self::$lang[$range][$name]) ? self::$lang[$range][$name] : $name; } } diff --git a/library/think/log/driver/sae.php b/library/think/log/driver/sae.php new file mode 100644 index 00000000..78ae8b91 --- /dev/null +++ b/library/think/log/driver/sae.php @@ -0,0 +1,49 @@ + +// +---------------------------------------------------------------------- + +namespace think\log\driver; + +class Sae { + + protected $config = [ + 'log_time_format' => ' c ', + ]; + + // 实例化并传入参数 + public function __construct($config=[]){ + $this->config = array_merge($this->config,$config); + } + + /** + * 日志写入接口 + * @access public + * @param string $log 日志信息 + * @param string $destination 写入目标 + * @return void + */ + public function write($log,$destination='') { + static $is_debug = null; + $now = date($this->config['log_time_format']); + $logstr = "[{$now}] ".$_SERVER['REMOTE_ADDR'].' '.$_SERVER['REQUEST_URI']."\r\n{$log}\r\n"; + if(is_null($is_debug)){ + preg_replace('@(\w+)\=([^;]*)@e', '$appSettings[\'\\1\']="\\2";', $_SERVER['HTTP_APPCOOKIE']); + $is_debug = in_array($_SERVER['HTTP_APPVERSION'], explode(',', $appSettings['debug'])) ? true : false; + } + if($is_debug){ + sae_set_display_errors(false);//记录日志不将日志打印出来 + } + sae_debug($logstr); + if($is_debug){ + sae_set_display_errors(true); + } + + } +} diff --git a/library/think/template.php b/library/think/template.php index b937ece7..7bf14055 100644 --- a/library/think/template.php +++ b/library/think/template.php @@ -60,7 +60,7 @@ class Template { // 初始化模板编译存储器 $type = $this->config['compile_type']?$this->config['compile_type']:'File'; - $class = '\Think\Template\Driver\\'.ucwords($type); + $class = '\\think\\template\\driver\\'.ucwords($type); $this->storage = new $class(); } @@ -439,7 +439,7 @@ class Template { protected function parseTagLib($tagLib,&$content,$hide=false) { $begin = $this->config['taglib_begin']; $end = $this->config['taglib_end']; - $className = '\\Think\\Template\\Taglib\\'.ucwords($tagLib); + $className = '\\think\\template\\taglib\\'.ucwords($tagLib); $tLib = new $className($this); foreach ($tLib->getTags() as $name=>$val){ $tags = [$name]; diff --git a/library/think/view.php b/library/think/view.php index a69cc35b..bffed796 100644 --- a/library/think/view.php +++ b/library/think/view.php @@ -72,7 +72,7 @@ class View { * @return View */ public function engine($engine, array $config = []){ - $class = '\\Think\\View\\Driver\\' . ucwords($engine); + $class = '\\think\\view\\driver\\' . ucwords($engine); $this->engine = new $class($config); return $this; } diff --git a/library/think/view/driver/think.php b/library/think/view/driver/think.php index 3a2dc980..a4542fcb 100644 --- a/library/think/view/driver/think.php +++ b/library/think/view/driver/think.php @@ -10,7 +10,7 @@ // +---------------------------------------------------------------------- namespace think\view\driver; -use think\template; +use think\Template; class Think { private $template = null; diff --git a/mode/common.php b/mode/common.php index 091091b4..43ed6719 100644 --- a/mode/common.php +++ b/mode/common.php @@ -52,20 +52,20 @@ return [ 'log' => [ 'type' => 'File', 'path' => LOG_PATH, - ] + ], 'cache' => [ 'type' => 'File', 'path' => CACHE_PATH, 'prefix' => '', 'expire' => 0, - ] + ], 'session' => [ 'prefix' => 'think', 'type' => '', 'auto_start' => true, - ] + ], /* 数据库设置 */ 'database' => [ @@ -89,29 +89,31 @@ return [ // 别名定义 'alias' => [ - 'Think\App' => CORE_PATH . 'App.php', - 'Think\Log' => CORE_PATH . 'Log.php', - 'Think\Log\Driver\File' => CORE_PATH . 'Log/Driver/File.php', - 'Think\Config' => CORE_PATH . 'Config.php', - 'Think\Route' => CORE_PATH . 'Route.php', - 'Think\Exception' => CORE_PATH . 'Exception.php', - 'Think\Model' => CORE_PATH . 'Model.php', - 'Think\Db' => CORE_PATH . 'Db.php', - 'Think\Db\Driver' => CORE_PATH . 'Db/Driver.php', - 'Think\Template' => CORE_PATH . 'Template.php', - 'Think\Error' => CORE_PATH . 'Error.php', - 'Think\Cache' => CORE_PATH . 'Cache.php', - 'Think\Cache\Driver\File' => CORE_PATH . 'Cache/Driver/File.php', - 'Think\Hook' => CORE_PATH . 'Hook.php', - 'Think\Session' => CORE_PATH . 'Session.php', - 'Think\Cookie' => CORE_PATH . 'Cookie.php', - 'Think\Controller' => CORE_PATH . 'Controller.php', - 'Think\View' => CORE_PATH . 'View.php', - 'Think\Url' => CORE_PATH . 'Url.php', - 'Think\Debug' => CORE_PATH . 'Debug.php', - 'Think\Input' => CORE_PATH . 'Input.php', - 'Think\Parser' => CORE_PATH . 'Parser.php', - 'Think\Lang' => CORE_PATH . 'Lang.php', + 'think\App' => CORE_PATH . 'app'.EXT, + 'think\Log' => CORE_PATH . 'log'.EXT, + 'think\log\driver\File' => CORE_PATH . 'log/driver/file'.EXT, + 'think\Config' => CORE_PATH . 'config'.EXT, + 'think\Route' => CORE_PATH . 'route'.EXT, + 'think\Exception' => CORE_PATH . 'exception'.EXT, + 'think\Model' => CORE_PATH . 'model'.EXT, + 'think\Db' => CORE_PATH . 'db'.EXT, + 'think\Db\Driver' => CORE_PATH . 'db/driver'.EXT, + 'think\Template' => CORE_PATH . 'template'.EXT, + 'think\view\driver\Think' => CORE_PATH . 'view\driver\think'.EXT, + 'think\template\driver\File' => CORE_PATH . 'template\driver\file'.EXT, + 'think\Error' => CORE_PATH . 'error'.EXT, + 'think\Cache' => CORE_PATH . 'cache'.EXT, + 'think\cache\driver\File' => CORE_PATH . 'cache/driver/file'.EXT, + 'think\Hook' => CORE_PATH . 'hook'.EXT, + 'think\Session' => CORE_PATH . 'session'.EXT, + 'think\Cookie' => CORE_PATH . 'cookie'.EXT, + 'think\Controller' => CORE_PATH . 'controller'.EXT, + 'think\View' => CORE_PATH . 'view'.EXT, + 'think\Url' => CORE_PATH . 'url'.EXT, + 'think\Debug' => CORE_PATH . 'debug'.EXT, + 'think\Input' => CORE_PATH . 'input'.EXT, + 'think\Parser' => CORE_PATH . 'parser'.EXT, + 'think\Lang' => CORE_PATH . 'lang'.EXT, ], 'init' => [], diff --git a/mode/sae.php b/mode/sae.php index 08d6ffee..b5aa8925 100644 --- a/mode/sae.php +++ b/mode/sae.php @@ -42,14 +42,16 @@ return [ // 别名定义 'alias' => [ - 'Think\Log' => CORE_PATH . 'Log'.EXT, - 'Think\Log\Driver\File' => CORE_PATH . 'Log/Driver/File'.EXT, - 'Think\Exception' => CORE_PATH . 'Exception'.EXT, - 'Think\Model' => CORE_PATH . 'Model'.EXT, - 'Think\Db' => CORE_PATH . 'Db'.EXT, - 'Think\Template' => CORE_PATH . 'Template'.EXT, - 'Think\Cache' => CORE_PATH . 'Cache'.EXT, - 'Think\Cache\Driver\File' => CORE_PATH . 'Cache/Driver/File'.EXT, + 'think\Log' => CORE_PATH . 'log'.EXT, + 'think\log\driver\File' => CORE_PATH . 'log/driver/file'.EXT, + 'think\log\driver\Sae' => CORE_PATH . 'log/driver/sae'.EXT, + 'think\Exception' => CORE_PATH . 'exception'.EXT, + 'think\Model' => CORE_PATH . 'model'.EXT, + 'think\Db' => CORE_PATH . 'db'.EXT, + 'think\Template' => CORE_PATH . 'template'.EXT, + 'think\Cache' => CORE_PATH . 'cache'.EXT, + 'think\cache\driver\File' => CORE_PATH . 'cache/driver/file'.EXT, + 'think\cache\driver\Memcache' => CORE_PATH . 'cache/driver/memcache'.EXT, ], ]; diff --git a/start.php b/start.php index 993085c3..b0ef56b8 100644 --- a/start.php +++ b/start.php @@ -23,9 +23,9 @@ require CORE_PATH.'loader.php'; Loader::register(); // 注册错误和异常处理机制 -register_shutdown_function(['Think\Error','appShutdown']); -set_error_handler(['Think\Error','appError']); -set_exception_handler(['Think\Error','appException']); +register_shutdown_function(['think\Error','appShutdown']); +set_error_handler(['think\Error','appError']); +set_exception_handler(['think\Error','appException']); // 加载模式定义文件 $mode = require MODE_PATH.APP_MODE.EXT;