emlog整站代码分析 dispatcher.php

emlog整站代码分析 dispatcher.php

作者:fly 发布于:2014-2-19 15:52 分类:Emlog

版权声明:本文系www.forwhat.cn原创 ,尊重作者劳动,转载请标明出处。本文转载自www.forwhat.cn谢谢

 

<?php
/**
 * 路由分发器
 * @copyright (c) Emlog All Rights Reserved
 */

class Dispatcher {

    static $_instance;
    
    /**
     * 请求模块
     */
    private $_model = '';

    /**
     * 请求模块方法
     */
    private $_method = '';

    /**
     * 请求参数
     */
    private $_params;

    /**
     * 路由表
     */
    private $_routingTable;

    /**
     * 访问路径
     */
    private $_path = NULL;
    
    //这个函数就是实例化当前模块的。
    public static function getInstance() {
        if(self::$_instance == null) {
            self::$_instance = new Dispatcher();
            return self::$_instance;
        } else {
            return self::$_instance;
        }
    }

    private function __construct() {
	
	//貌似是获取URL重写前的原始URL
        $this->_path = $this->setPath();
	
	//获取路由表。包含了。model,method,还有正则表达式等信息。
        $this->_routingTable = Option::getRoutingTable();

	//猜测是否开启URL重写?
        $urlMode = Option::get('isurlrewrite');
	
	//从路由表中获取,当前应该执行什么动作。
        foreach ($this->_routingTable as $route) {
	
            if (!isset($route['reg_' . $urlMode])) {
                $reg = isset($route['reg']) ? $route['reg'] : $route['reg_0'];
            } else {
                $reg = $route['reg_' . $urlMode];
            }
	    //preg_match执行一个正则表达式匹配
	    //正则表达式什么,自己上网搜吧。
	    //反正猜测大概意思是,根据当前的路径来判断该有哪个系统模块去执行。
            if (preg_match($reg, $this->_path, $matches)) {
                $this->_model = $route['model'];
                $this->_method = $route['method'];
                $this->_params = $matches;
                break;
            } elseif (preg_match($route['reg_0'], $this->_path, $matches)) {
                $this->_model = $route['model'];
                $this->_method = $route['method'];
                $this->_params = $matches;
                break;
            }
        }
	//没有找到可以执行当前动作的模块。这不大会执行吧,除非丢掉了首页显示模块?

        if (empty($this->_model)) {
            show_404_page();
        }
    }

    //执行构造函数中获取到的动作
    public function dispatch(){
        $module = new $this->_model();
        $method = $this->_method;
        $module->$method($this->_params);//开始执行动作
    }

    //路径,处理。获取重写之前的路径。构造函数中调用的,
    public static function setPath(){
        $path = '';
        if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { //iis
            $path = $_SERVER['HTTP_X_REWRITE_URL'];
        } elseif (isset($_SERVER['REQUEST_URI'])) {
            $path = $_SERVER['REQUEST_URI'];
        } else {
            if (isset($_SERVER['argv'])) {
                $path = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'][0];
            } else {
                $path = $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];
            }
        }

        //for iis6 path is GBK
        if (isset($_SERVER['SERVER_SOFTWARE']) && false !== stristr($_SERVER['SERVER_SOFTWARE'], 'IIS')) {
            if (function_exists('mb_convert_encoding')) {
                $path = mb_convert_encoding($path, 'UTF-8', 'GBK');
            } else {
                $path = @iconv('GBK', 'UTF-8', @iconv('UTF-8', 'GBK', $path)) == $path ? $path : @iconv('GBK', 'UTF-8', $path);
            }
        }
        //for ie6 header location
        $r = explode('#', $path, 2);
        $path = $r[0];
        //for iis6
        $path = str_ireplace('index.php', '', $path);
        //for subdirectory
        $t = parse_url(BLOG_URL);
        $path = str_replace($t['path'], '/', $path);

        return $path;
    }
}

 

发表评论:

 
Powered by emlog sitemap