mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 07:12:47 +08:00
改进App、Error和View类
改进Log类 简化驱动类的定义
This commit is contained in:
@@ -133,7 +133,7 @@ class App {
|
|||||||
$var_c = $config['var_controller'];
|
$var_c = $config['var_controller'];
|
||||||
$var_a = $config['var_action'];
|
$var_a = $config['var_action'];
|
||||||
$var_p = $config['var_pathinfo'];
|
$var_p = $config['var_pathinfo'];
|
||||||
if(!empty($_GET[$var_p])) { // 判断URL里面是否有兼容模式参数
|
if(isset($_GET[$var_p])) { // 判断URL里面是否有兼容模式参数
|
||||||
$_SERVER['PATH_INFO'] = $_GET[$var_p];
|
$_SERVER['PATH_INFO'] = $_GET[$var_p];
|
||||||
unset($_GET[$var_p]);
|
unset($_GET[$var_p]);
|
||||||
}elseif(IS_CLI){ // CLI模式下 index.php module/controller/action/params/...
|
}elseif(IS_CLI){ // CLI模式下 index.php module/controller/action/params/...
|
||||||
@@ -185,7 +185,7 @@ class App {
|
|||||||
// 监听path_info
|
// 监听path_info
|
||||||
Tag::listen('path_info');
|
Tag::listen('path_info');
|
||||||
// 分析PATHINFO信息
|
// 分析PATHINFO信息
|
||||||
if(empty($_SERVER['PATH_INFO']) && $_SERVER['SCRIPT_NAME'] != $_SERVER['PHP_SELF']) {
|
if(!isset($_SERVER['PATH_INFO']) && $_SERVER['SCRIPT_NAME'] != $_SERVER['PHP_SELF']) {
|
||||||
$types = explode(',',$config['pathinfo_fetch']);
|
$types = explode(',',$config['pathinfo_fetch']);
|
||||||
foreach ($types as $type){
|
foreach ($types as $type){
|
||||||
if(0===strpos($type,':')) {// 支持函数判断
|
if(0===strpos($type,':')) {// 支持函数判断
|
||||||
@@ -199,6 +199,9 @@ class App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 定位模块
|
// 定位模块
|
||||||
|
if(empty($_SERVER['PATH_INFO'])) {
|
||||||
|
$_SERVER['PATH_INFO'] = '';
|
||||||
|
}
|
||||||
$part = pathinfo($_SERVER['PATH_INFO']);
|
$part = pathinfo($_SERVER['PATH_INFO']);
|
||||||
define('__EXT__', isset($part['extension'])?strtolower($part['extension']):'');
|
define('__EXT__', isset($part['extension'])?strtolower($part['extension']):'');
|
||||||
$_SERVER['PATH_INFO'] = trim(preg_replace('/\.('.trim($config['url_html_suffix'],'.').')$/i', '',$_SERVER['PATH_INFO']),'/');
|
$_SERVER['PATH_INFO'] = trim(preg_replace('/\.('.trim($config['url_html_suffix'],'.').')$/i', '',$_SERVER['PATH_INFO']),'/');
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ class Error {
|
|||||||
case E_COMPILE_ERROR:
|
case E_COMPILE_ERROR:
|
||||||
case E_USER_ERROR:
|
case E_USER_ERROR:
|
||||||
$errorStr = "[$errno] $errstr ".$errfile." 第 $errline 行.";
|
$errorStr = "[$errno] $errstr ".$errfile." 第 $errline 行.";
|
||||||
Log::write($errorStr,'ERROR');
|
Log::record($errorStr,'ERROR');
|
||||||
self::halt($errorStr);
|
self::halt($errorStr);
|
||||||
break;
|
break;
|
||||||
case E_STRICT:
|
case E_STRICT:
|
||||||
|
|||||||
@@ -12,22 +12,74 @@
|
|||||||
namespace Think;
|
namespace Think;
|
||||||
class Log {
|
class Log {
|
||||||
|
|
||||||
static protected $handler = null;
|
// 日志信息
|
||||||
|
static protected $log = [];
|
||||||
|
static protected $level = ['ERR','NOTIC','DEBUG','SQL','INFO'];
|
||||||
|
static protected $storage = null;
|
||||||
|
|
||||||
// 日志初始化
|
// 日志初始化
|
||||||
static public function init($config=[]){
|
static public function init($config=[]){
|
||||||
if(!empty($config['type'])) { // 读取log驱动
|
$type = isset($config['type'])?$config['type']:'File';
|
||||||
$class = 'Think\\Log\\Driver\\'. ucwords(strtolower($config['type']));
|
$class = 'Think\\Log\\Driver\\'. ucwords($type);
|
||||||
// 检查驱动类
|
unset($config['type']);
|
||||||
unset($config['type']);
|
self::$storage = new $class($config);
|
||||||
self::$handler = new $class($config);
|
}
|
||||||
return self::$handler;
|
|
||||||
}
|
/**
|
||||||
|
* 记录日志 并且会过滤未经设置的级别
|
||||||
|
* @access public
|
||||||
|
* @param string $message 日志信息
|
||||||
|
* @param string $level 日志级别
|
||||||
|
* @param boolean $record 是否强制记录
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
static public function record($message,$level='INFO') {
|
||||||
|
self::$log[$level][] = "{$level}: {$message}";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取内存中的日志信息
|
||||||
|
* @access public
|
||||||
|
* @param string $level 日志级别
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
static public function getLog($level=''){
|
||||||
|
return $level?self::$log[$level]:self::$log;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志保存
|
||||||
|
* @access public
|
||||||
|
* @param string $destination 写入目标
|
||||||
|
* @param string $level 保存的日志级别
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
static public function save($destination='',$level='') {
|
||||||
|
$log = $level?self::$log[$level]:self::$log;
|
||||||
|
if(empty($log)) return ;
|
||||||
|
$message = '';
|
||||||
|
if($level) {
|
||||||
|
$message .= implode("\r\n",$log);
|
||||||
|
self::$log[$level] = [];
|
||||||
|
}else{
|
||||||
|
foreach($log as $info){
|
||||||
|
$message .= implode("\r\n",$info)."\r\n";
|
||||||
|
}
|
||||||
|
self::$log = [];
|
||||||
|
}
|
||||||
|
self::$storage->write($message,$destination);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志直接写入
|
||||||
|
* @access public
|
||||||
|
* @param string $log 日志信息
|
||||||
|
* @param string $level 日志级别
|
||||||
|
* @param string $destination 写入目标
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
static public function write($log,$level,$destination='') {
|
||||||
|
self::$storage->write("{$level}: {$log}",$destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用驱动类的方法
|
|
||||||
static public function __callStatic($method, $params){
|
|
||||||
return call_user_func_array(array(self::$handler, $method), $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -12,80 +12,18 @@
|
|||||||
namespace Think\Log\Driver;
|
namespace Think\Log\Driver;
|
||||||
|
|
||||||
class File {
|
class File {
|
||||||
|
|
||||||
// 日志记录方式
|
|
||||||
const SYSTEM = 0;
|
|
||||||
const MAIL = 1;
|
|
||||||
const FILE = 3;
|
|
||||||
const SAPI = 4;
|
|
||||||
|
|
||||||
// 日志信息
|
protected $config = [
|
||||||
protected $log = [];
|
'log_time_format' => ' c ',
|
||||||
protected $config = array(
|
|
||||||
'log_time_format' => '[ c ]',
|
|
||||||
'log_file_size' => 2097152,
|
'log_file_size' => 2097152,
|
||||||
'log_allow_level' => array('ERR','NOTIC','DEBUG','SQL','INFO'),
|
'log_path' => '',
|
||||||
);
|
];
|
||||||
|
|
||||||
|
// 实例化并传入参数
|
||||||
public function __construct($config=[]){
|
public function __construct($config=[]){
|
||||||
$this->config = array_merge($this->config,$config);
|
$this->config = array_merge($this->config,$config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 记录日志 并且会过滤未经设置的级别
|
|
||||||
* @access public
|
|
||||||
* @param string $message 日志信息
|
|
||||||
* @param string $level 日志级别
|
|
||||||
* @param boolean $record 是否强制记录
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function record($message,$level='INFO',$record=false) {
|
|
||||||
if($record || false !== array_search($level,$this->config['log_allow_level'])) {
|
|
||||||
$this->log[$level][] = "{$level}: {$message}\r\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取内存中的日志信息
|
|
||||||
* @access public
|
|
||||||
* @param string $level 日志级别
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getLog($level=''){
|
|
||||||
return $level?$this->log[$level]:$this->log;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 日志保存
|
|
||||||
* @access public
|
|
||||||
* @param string $destination 写入目标
|
|
||||||
* @param string $level 保存的日志级别
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function save($destination='',$level='') {
|
|
||||||
$log = $level?$this->log[$level]:$this->log;
|
|
||||||
if(empty($log)) return ;
|
|
||||||
if(empty($destination))
|
|
||||||
$destination = $this->config['log_path'].date('y_m_d').'.log';
|
|
||||||
|
|
||||||
//检测日志文件大小,超过配置大小则备份日志文件重新生成
|
|
||||||
if(is_file($destination) && floor($this->config['log_file_size']) <= filesize($destination) )
|
|
||||||
rename($destination,dirname($destination).'/'.time().'-'.basename($destination));
|
|
||||||
|
|
||||||
$message = date($this->config['log_time_format']).' '.$_SERVER['REMOTE_ADDR'].' '.$_SERVER['REQUEST_URI']."\r\n";
|
|
||||||
if($level) {
|
|
||||||
$message .= implode('',$log)."\r\n";
|
|
||||||
$this->log[$level] = [];
|
|
||||||
}else{
|
|
||||||
foreach($log as $info){
|
|
||||||
$message .= implode('',$info)."\r\n";
|
|
||||||
}
|
|
||||||
$this->log = [];
|
|
||||||
}
|
|
||||||
error_log($message, 3,$destination);
|
|
||||||
//clearstatcache();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 日志直接写入
|
* 日志直接写入
|
||||||
* @access public
|
* @access public
|
||||||
@@ -94,13 +32,13 @@ class File {
|
|||||||
* @param string $destination 写入目标
|
* @param string $destination 写入目标
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function write($log,$level,$destination='') {
|
public function write($log,$destination='') {
|
||||||
$now = date($this->config['log_time_format']);
|
$now = date($this->config['log_time_format']);
|
||||||
if(empty($destination))
|
if(empty($destination))
|
||||||
$destination = $this->config['log_path'].date('y_m_d').'.log';
|
$destination = $this->config['log_path'].date('y_m_d').'.log';
|
||||||
//检测日志文件大小,超过配置大小则备份日志文件重新生成
|
//检测日志文件大小,超过配置大小则备份日志文件重新生成
|
||||||
if(is_file($destination) && floor($this->config['log_file_size']) <= filesize($destination) )
|
if(is_file($destination) && floor($this->config['log_file_size']) <= filesize($destination) )
|
||||||
rename($destination,dirname($destination).'/'.time().'-'.basename($destination));
|
rename($destination,dirname($destination).'/'.time().'-'.basename($destination));
|
||||||
error_log("{$now} {$level}: {$log}\r\n", 3,$destination);
|
error_log("[{$now}] ".$_SERVER['REMOTE_ADDR'].' '.$_SERVER['REQUEST_URI']."\r\n{$log}\r\n", 3,$destination);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ class View {
|
|||||||
* @param boolean $return 是否返回
|
* @param boolean $return 是否返回
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function display($template='',$vars=[],$cacheId,$return=false) {
|
public function display($template='',$vars=[],$cacheId='',$return=false) {
|
||||||
Tag::listen('view_begin',$template);
|
Tag::listen('view_begin',$template);
|
||||||
// 解析并获取模板内容
|
// 解析并获取模板内容
|
||||||
$content = $this->fetch($template,$vars,$cacheId);
|
$content = $this->fetch($template,$vars,$cacheId);
|
||||||
|
|||||||
Reference in New Issue
Block a user