微信搜索superit|邀请体验:大数据, 数据管理、OLAP分析与可视化平台 | 赞助作者:赞助作者

(原创)解析一下yaf在boostrap中是如何做分发请求的

php aide_941 37℃

1.boostrap.php中的以_下划线为开始,按顺序自动执行方法的!!

2.统一使用redis作缓存, 避免了原群机器上使用单一数据库无法作有状态请求

3.loadpackage 自动加载第三方适配插件,(需要检查第三方插件的安全性)

<?php
/**
 * @name Bootstrap
 * @author weisongyi
 * @desc 启动初始化
 * @date 2018-08-14
 */
class Bootstrap extends Yaf_Bootstrap_Abstract {

    public function _initConfig() {
        
        //注册本地类库前缀
        $loader = Yaf_Loader::getInstance();
        $loader->registerLocalNamespace(array('Abstract', 'Comm', 'Data', 'Helper', 'Thirdpart'));
        //$this->loadPackage();
        $this->define();
	}
	
	public function _initSEV(Yaf_Dispatcher $dispatcher){
	    //cli
	    !defined('IS_CLI') && define('IS_CLI', false);
	    if($dispatcher->getRequest()->isCli()){
	        
	        ini_set('yaf.name_suffix', '0');
	        !defined('IS_CLI') && define('IS_CLI', true);
	        
	    }
	    
	    $ini_file = APP_PATH . "/system/SRV". APP_ENV;
	    if (!is_file($ini_file)) {
	        throw new Yaf_Exception_DispatchFailed('Can\'t find the SRV.', 500);
	    }
	    $array2 = parse_ini_file($ini_file);
	    $_SERVER = array_merge($_SERVER, $array2);
	    
	    ini_set("session.save_handler", "redis");
	    ini_set("session.save_path", "tcp://{$_SERVER['SRV_REDIS0_HOST']}:{$_SERVER['SRV_REDIS0_PORT']}");//tcp://ip:port?auth=
	    ini_set("session.gc_maxlifetime", 60*60*24);
	}
	
	public function _initPlugin(Yaf_Dispatcher $dispatcher) {
	    $objSamplePlugin = new PluginMain();
	    $dispatcher->registerPlugin($objSamplePlugin);
	}

	public function _initRoute(Yaf_Dispatcher $dispatcher) {
		//在这里注册自己的路由协议,默认使用简单路由
	}
	
	public function _initView(Yaf_Dispatcher $dispatcher) {
		//在这里注册自己的view控制器
	}
	
	//d定义文件
	public function define(){
	    
	    //保存配置
	    $arrConfig = Yaf_Application::app()->getConfig();
	    Yaf_Registry::set('config', $arrConfig);
	    
	}
	
	//load file
	public function loadPackage(){
	    $lib= Yaf_Application::app()->getConfig()->application->directory . "/library/";
	    static $fileList = array();
	    
	    $packages = array(
	        'Comm/Mongodb/Interface/*.php',
	        'Comm/Mongodb/Model/*.php',
	        'Comm/Mongodb/Operation/*.php',
	        'Comm/Mongodb/*.php'
	    );
	    
	    if( !$fileList ){
	        foreach ($packages as $package){
	            foreach(glob($lib . $package) as $file_path){
    	            $s = pathinfo($file_path);
    	            $dirname = $s['dirname'];
    	            $basename = $s['basename'];
    	            $extension = $s['extension'];
    	            
    	            $path = substr($dirname, strlen($lib));
    	            if(is_file($file_path) && $extension == 'php') $fileList[] = $path .DIRECTORY_SEPARATOR. $basename;
    	            
    	        }
	        }
	    }
	    
	    try{
    	    foreach($fileList as $k=>$file){
    	        Yaf_Loader::import($file);
    	    }
	    }
	    catch (Exception $e){
	        var_dump($e);
	    }
	    
	}
	
	/**
	 * 执行结束后处理
	 *
	 * @return void
	 */
	public static function errorHandlerShutdown()
	{
	    $message = '';
	    $error = error_get_last();
	    if ($error) {
	        $separator = "\r\n";
	        $message .= '错误:' . $error['message'] . $separator;
	        $message .= '文件:' . $error['file'] . $separator;
	        $message .= '行数:' . $error['line'] . $separator;
	        $message = str_replace($separator, '<br />', $message);
	        var_dump($message);
	        exit;
	    } else {
	        header('Location:http://locahost/error.html');
	        exit;
	    }
	}
	
	
}

 

转载请注明:SuperIT » (原创)解析一下yaf在boostrap中是如何做分发请求的

喜欢 (0)or分享 (0)