94hwan-PHP框架基本原理

Source:94hwan 与众不同  Author:网络部
2011-09-20 15:18

在引导文件中,调试和session等接口处理的主要是下面代码:

  1. //debug设置 
  2. $_debug_safe_ip = false; 
  3. require PATH_LIBRARY.'/debug/lib_debug.php'; 
  4. if( in_array( util::get_client_ip(), $GLOBALS['config']['safe_client_ip']) || OPEN_DEBUG === true ) 
  5. { 
  6.     $_debug_safe_ip = true; 
  7.     ini_set('display_errors', 'On'); 
  8. } 
  9. else 
  10. { 
  11.     ini_set('display_errors', 'Off'); 
  12. } 
  13. set_exception_handler('handler_debug_exception'); 
  14. set_error_handler('handler_debug_error', E_ALL); 
  15. register_shutdown_function('handler_php_shutdown'); 
  16.  
  17. //session接口(使用session前需自行调用session_start) 
  18. require CORE.'/session.php'; 

Debug程序是通过

  • set_exception_handler('handler_debug_exception'); 
  • set_error_handler('handler_debug_error', E_ALL);

这里对系统的debug信息进行了接管(接口函数定义在 core/library/debug/lib_debug.php )
除了支持基本的调试外,也支持使用xhprof进行性能调试

关于错误处理时具体方法请参考:错误处理与程序Debug方法>>

core/session.php 分两种情况,一种是使用memcache的情况,仅是修改php参数,不会对session接口重写,另一种情况是使用文件hash缓存(如果单服务器运行的程序,这种模式会更稳定),接口进行了重写,考虑性能原因,默认并没有初始化缓存,如果需要使用session,还是需要用户自行在使用前用 @session_start() 初始化。。

此外在 init.php 还定义了几个特殊函数:
 

  1. /** 
  2.  * 程序结束后执行的动作 
  3.  */ 
  4. function handler_php_shutdown() 
  5. { 
  6.     show_debug_error(); 
  7.     log::save(); 
  8.     if( defined('CLS_CACHE') ) { 
  9.         cache::free(); 
  10.     } 
  11.     if( defined('CLS_CACHE_NATIVE') ) { 
  12.         cls_cache_native::close(); 
  13.     } 
  14. } 
  15.  
  16. /** 
  17.  * 致命错误处理接口 
  18.  * 系统发生致命错误后的提示 
  19.  * (致命错误是指发生错误后要直接中断程序的错误,如数据库连接失败、找不到控制器等) 
  20.  */ 
  21. function handler_fatal_error( $errtype, $msg ) 
  22. { 
  23.     global $_debug_safe_ip; 
  24.     $log_str = $errtype.':'.$msg; 
  25.     if ( OPEN_DEBUG === true || $_debug_safe_ip ) 
  26.     { 
  27.         throw new Exception( $log_str ); 
  28.     } 
  29.     else 
  30.     {         
  31.         log::add('fatal_error', $msg); 
  32.         header ( "location:/404.html" ); 
  33.         exit(); 
  34.     } 
  35. } 
  36.  
  37. /** 
  38.  * 路由控制 
  39.  * 
  40.  * @param $ctl  控制器 
  41.  * @parem $ac   动作 
  42.  * @return void 
  43.  */ 
  44. function run_controller() 
  45. { 
  46.     try 
  47.     { 
  48.         $ac = preg_replace("/[^0-9a-z_]/i", '', req::item('ac', 'index') ); 
  49.         $ac = emptyempty ( $ac ) ? $ac = 'index' : $ac; 
  50.              
  51.         $ctl = 'ctl_'.preg_replace("/[^0-9a-z_]/i", '', req::item('ct', 'index') ); 
  52.         $path_file = PATH_CONTROL . '/' . $ctl . '.php'; 
  53.  
  54.         if( file_exists( $path_file ) ) 
  55.         { 
  56.             require $path_file; 
  57.         } 
  58.         else 
  59.         { 
  60.             throw new Exception ( "Contrl {$ctl}--{$path_file} is not exists!" ); 
  61.         } 
  62.         if( method_exists ( $ctl, $ac ) === true ) 
  63.         { 
  64.             $instance = new $ctl ( ); 
  65.             $instance->$ac (); 
  66.         } 
  67.         else 
  68.         { 
  69.             throw new Exception ( "Method {$ctl}::{$ac}() is not exists!" ); 
  70.         } 
  71.     } 
  72.     catch ( Exception $e ) 
  73.     { 
  74.         handler_fatal_error( 'init.php run_controller()', $e->getMessage().' url:'.util::get_cururl() ); 
  75.     } 
  76. } 
  77.  
  78. /** 
  79.  * 自动加载类库处理 
  80.  * 加载优先级 /core/library => 应用目录/model => 根目录/model 
  81.  * (如果不在这些位置, 则需自行手工加载,对于小型项目,也可以把model全放到library以减少类文件查找时间) 
  82.  * @return void 
  83.  */ 
  84. function __autoload( $classname ) 
  85. { 
  86.     $classname = preg_replace("/[^0-9a-z_]/i", '', $classname); 
  87.     if( class_exists ( $classname ) ) { 
  88.         return true; 
  89.     } 
  90.     $classfile = $classname.'.php'; 
  91.     try 
  92.     { 
  93.         if ( file_exists ( PATH_LIBRARY.'/'.$classfile ) ) 
  94.         { 
  95.             require PATH_LIBRARY.'/'.$classfile; 
  96.         } 
  97.         else if( file_exists ( PATH_MODEL.'/'.$classfile ) )  
  98.         { 
  99.             require PATH_MODEL.'/'.$classfile; 
  100.         } 
  101.         else if( file_exists ( require PATH_ROOT.'/model/'.$classfile ) )  
  102.         { 
  103.             require PATH_ROOT.'/model/'.$classfile; 
  104.         } 
  105.         else 
  106.         { 
  107.             return false; 
  108.             throw new Exception ( 'Error: Cannot find the '.$classname ); 
  109.         } 
  110.     } 
  111.     catch ( Exception $e ) 
  112.     { 
  113.         handler_fatal_error( 'init.php __autoload()', $e->getMessage().'|'.$classname.' url:'.util::get_cururl() ); 
  114.     } 
  115. } 
  116.  
  117. /** 
  118.  * req::item 别名函数 
  119.  */ 
  120. function request($key, $df='') 
  121. { 
  122.     return req::item($key, $df); 
  123. } 

 

...