[李景山php]每天TP5-20170117|thinkphp5-Url.php-2

    // 檢測域名
    protected static function parseDomain(&$url, $domain)
    {// 根據 url 及傳入的 域名進行解析
        if (!$domain) {// 如果沒有傳入合適的數據,直接 返回
            return '';
        }
        $request = Request::instance();// 請求實例化
        if (true === $domain) {// 如果傳入的域名爲 true 則自動判定域名
            // 自動判斷域名
            $domain = $request->host();// 獲取域名
            if (Config::get('url_domain_deploy')) {// 是否根域名 依賴
                // 根域名
                $urlDomainRoot = Config::get('url_domain_root');// 通過 配置文件 獲取 跟域名
                $domains       = Route::rules('domain');// 路由尋找 域名規則
                $route_domain  = array_keys($domains);// 獲取 跟 域名 對應的 路由規則
                foreach ($route_domain as $domain_prefix) {// 遍歷循環 此前的 根域名
                    if (0 === strpos($domain_prefix, '*.') && strpos($domain, ltrim($domain_prefix, '*.')) !== false) {
                        // 能夠進行 域名匹配
                        foreach ($domains as $key => $rule) {// 對路由規則進行遍歷
                            $rule = is_array($rule) ? $rule[0] : $rule;// 如果 值 仍然是 數組,獲取第一個元素 否則是 整個元素,
                            // 弄這麼複雜的配置項 弄啥嘞
                            if (is_string($rule) && false === strpos($key, '*') && 0 === strpos($url, $rule)) {
                                $url    = ltrim($url, $rule);// 當規則是個字符串,並且沒有*號,並且是用rule 開頭的
                                // 刪掉 開頭的 這個 規則匹配符號
                                $domain = $key;// 將相應的域名 賦值到 域名裏面 修正自動獲取的域名
                                // 生成對應子域名
                                if (!empty($urlDomainRoot)) {// 不爲空,在去拼合 域名的 跟目錄文件
                                    $domain .= $urlDomainRoot;
                                }
                                break;//跳出
                            } else if (false !== strpos($key, '*')) {// 如果是*
                                if (!empty($urlDomainRoot)) {
                                    $domain .= $urlDomainRoot;// 直接拼合 自動獲取的域名 跟 跟目錄
                                }
                                break;
                            }
                        }
                    }
                }
            }
        } elseif (!strpos($domain, '.')) {// 以. 開頭,或者沒有 .
            $rootDomain = Config::get('url_domain_root');// 獲取根目錄
            if (empty($rootDomain)) {// 根域名 爲空
                $host       = $request->host();// 請求 跟域名
                $rootDomain = substr_count($host, '.') > 1 ? substr(strstr($host, '.'), 1) : $host;
            }
            $domain .= '.' . $rootDomain;
        }
        return ($request->isSsl() ? 'https://' : 'http://') . $domain;// 不同的協議
    }// 總結:不同的方式進行了不同的 域名生成 規則的判定

    // 解析URL後綴
    protected static function parseSuffix($suffix)
    {// 解析 URL 後綴
        if ($suffix) {// 配置文件
            $suffix = true === $suffix ? Config::get('url_html_suffix') : $suffix;// 如果傳入的參數是 true
            if ($pos = strpos($suffix, '|')) {//如果有分割線,獲取第一組數據,那要分割線還有什麼用途呢???
                $suffix = substr($suffix, 0, $pos);
            }
        }// 後綴 是否有 . 無所謂,做了處理
        return (empty($suffix) || 0 === strpos($suffix, '.')) ? $suffix : '.' . $suffix;
    }

    // 匹配路由地址
    public static function getRuleUrl($rule, &$vars = [])
    {
        foreach ($rule as $item) {// 遍歷路由規則
            list($url, $pattern, $domain) = $item;// 每個item 都會擁有這麼多的選項
            if (empty($pattern)) {// 如果父級爲空,直接返回
                return [$url, $domain];
            }
            foreach ($pattern as $key => $val) {//對父級不爲空的情況進行處理
                if (isset($vars[$key])) {//如果對應的 配合變量
                    $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key . '', '<' . $key . '>'], $vars[$key], $url);
                    unset($vars[$key]);
                    $result = [$url, $domain];// 返回 組合後的 url
                } elseif (2 == $val) {
                    $url    = str_replace(['/[:' . $key . ']', '[:' . $key . ']', '<' . $key . '?>'], '', $url);
                    $result = [$url, $domain];// 返回另外一種組合的url
                } else {
                    break;
                }
            }
            if (isset($result)) {// 如果有結果 返回結果
                return $result;
            }
        }
        return false;
    }//匹配當前目錄

    // 指定當前生成URL地址的root
    public static function root($root)
    {
        self::$root = $root;
        Request::instance()->root($root);
    }// 獲取根地址
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章