[李景山php]每天TP5-20170130|thinkphp5-Request.php-2

// 綁定的屬性
protected $bind = [];// 邦定的屬性 倉庫

/**
 * 架構函數
 * @access public
 * @param array $options 參數
 */
public function __construct($options = [])
{// 構造函數
    foreach ($options as $name => $item) {// 參數設定
        if (property_exists($this, $name)) {// 如果存在合適的參數
            $this->$name = $item;// 進行設置 否則不管
        }
    }
    if (is_null($this->filter)) {// 如果爲空
        $this->filter = Config::get('default_filter');// 配置 獲取 當前默認的 配置選項
    }
}

public function __call($method, $args)// 調用 動態調用 不存在的函數
{
    if (array_key_exists($method, self::$hook)) {// 如果 鉤子函數 裏面存在的話
        array_unshift($args, $this);// 去掉裏面這裏 參數
        return call_user_func_array(self::$hook[$method], $args);// 調用 鉤子
    } else {// 否則拋出異常
        throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
    }
}

/**
 * Hook 方法注入
 * @access public
 * @param string|array  $method 方法名
 * @param mixed         $callback callable
 * @return void
 */
public static function hook($method, $callback = null)
{// Hook 方法 注入
    if (is_array($method)) {// 如果是數組
        self::$hook = array_merge(self::$hook, $method);// 更多hook 函數 調度,所謂的注入,其實跟我們的 類似
    } else {
        self::$hook[$method] = $callback;// 對應的 key value 的設置形式
    }
}

/**
 * 初始化
 * @access public
 * @param array $options 參數
 * @return \think\Request
 */
public static function instance($options = [])
{// 初始化 單列 模式初始化
    if (is_null(self::$instance)) {
        self::$instance = new static($options);
    }
    return self::$instance;
}

/**
 * 創建一個URL請求
 * @access public
 * @param string    $uri URL地址
 * @param string    $method 請求類型
 * @param array     $params 請求參數
 * @param array     $cookie
 * @param array     $files
 * @param array     $server
 * @param string    $content
 * @return \think\Request
 */
public static function create($uri, $method = 'GET', $params = [], $cookie = [], $files = [], $server = [], $content = null)
{// 創建 URL 裏面的 URL 地址 method 方法 請求參數 cookie 文件 files 文件  server 服務 content 內容
    $server['PATH_INFO']      = '';// 用小的 server 代替系統變量裏面的 SERVER
    $server['REQUEST_METHOD'] = strtoupper($method);// 獲取 方法 字符串 全部轉換成爲大寫
    $info                     = parse_url($uri);// 解析傳遞過來的 url
    if (isset($info['host'])) {// 如果設置了  host
        $server['SERVER_NAME'] = $info['host'];// 域名
        $server['HTTP_HOST']   = $info['host'];// ip 變成相同的 產品
    }
    if (isset($info['scheme'])) {// 如果存在計劃 跟 調度
        if ('https' === $info['scheme']) {
            $server['HTTPS']       = 'on';// 開啓安全傳輸
            $server['SERVER_PORT'] = 443;// 443
        } else {
            unset($server['HTTPS']);// 刪除 安全傳輸
            $server['SERVER_PORT'] = 80;// 默認 80端口
        }
    }
    if (isset($info['port'])) {// 如果 設置了 端口 目的 是 非 80端口
        $server['SERVER_PORT'] = $info['port'];// 給予系統端口
        $server['HTTP_HOST']   = $server['HTTP_HOST'] . ':' . $info['port'];// IP 預計進行了拼合,變成了 IP+ 端口
    }
    if (isset($info['user'])) {// 如果設置了 用戶
        $server['PHP_AUTH_USER'] = $info['user'];
    }
    if (isset($info['pass'])) {// 密碼
        $server['PHP_AUTH_PW'] = $info['pass'];
    }
    if (!isset($info['path'])) {// 路徑
        $info['path'] = '/';
    }
    $options     = [];// 初始化選項倉庫
    $queryString = '';// 查詢字符串 應該就是留在後面的字符串
    if (isset($info['query'])) {// 如果給定了 參數
        parse_str(html_entity_decode($info['query']), $query);// 解析參數
        if (!empty($params)) {//如果傳出的參數還有
            $params      = array_replace($query, $params);// 替換參數
            $queryString = http_build_query($query, '', '&');// 創建 queryString
        } else {// 否則 默認的 沒有對應設置的參數
            $params      = $query;
            $queryString = $info['query'];// 他就是 對應的字符串
        }
    } elseif (!empty($params)) {
        $queryString = http_build_query($params, '', '&');// 通過參數 設置 對應的 字符串
    }
    $server['REQUEST_URI']  = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');// 後面的參數 是空,還是 帶有問號的參數
    $server['QUERY_STRING'] = $queryString;// 請求的字符串
    $options['cookie']      = $cookie;// cookie
    $options['param']       = $params;// 參數
    $options['file']        = $files;// 文件
    $options['server']      = $server;// 服務
    $options['url']         = $server['REQUEST_URI']; // 簡寫
    $options['baseUrl']     = $info['path'];// 基礎路徑
    $options['pathinfo']    = '/' == $info['path'] ? '/' : ltrim($info['path'], '/');// 默認的路徑
    $options['method']      = $server['REQUEST_METHOD'];// 方法
    $options['domain']      = $info['scheme'] . '://' . $server['HTTP_HOST'];// 計劃端口號
    $options['content']     = $content;// 內容
    self::$instance         = new self($options);// 實例化 自己
    return self::$instance;// 返回自己
}

/**
 * 獲取當前包含協議的域名
 * @access public
 * @param string $domain 域名
 * @return string
 */
public function domain($domain = null)
{// 獲取當前包含協議的域名
    if (!is_null($domain)) {// 如果域名不爲空
        $this->domain = $domain;
        return $this;
    } elseif (!$this->domain) {
        $this->domain = $this->scheme() . '://' . $this->host();
    }
    return $this->domain;
}// 設置 + 獲取,設置 就返回對象 獲取 就獲取當前的域名

/**
 * 獲取當前完整URL 包括QUERY_STRING
 * @access public
 * @param string|true $url URL地址 true 帶域名獲取
 * @return string
 */
public function url($url = null)// 設置 與 獲取 創造 與 毀滅
{
    if (!is_null($url) && true !== $url) {
        $this->url = $url;
        return $this;
    } elseif (!$this->url) {// 默認爲空
        if (IS_CLI) {// 如果是命令行的方式 獲取的參數 爲 不過應該很少用
            $this->url = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
        } elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {// 如果重寫
            $this->url = $_SERVER['HTTP_X_REWRITE_URL'];
        } elseif (isset($_SERVER['REQUEST_URI'])) {// 如果存在
            $this->url = $_SERVER['REQUEST_URI'];
        } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {// 如果是 pathinfo 方式
            $this->url = $_SERVER['ORIG_PATH_INFO'] . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
        } else {
            $this->url = '';// 如果系統沒有,就是空
        }
    }
    return true === $url ? $this->domain() . $this->url : $this->url;// 拼接 帶有域名的,或者 不帶有域名的
}


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