[李景山php]每天TP5-20170201|thinkphp5-Request.php-4

/**
 * 設置獲取獲取當前請求的參數
 * @access public
 * @param string|array  $name 變量名
 * @param mixed         $default 默認值
 * @param string|array  $filter 過濾方法
 * @return mixed
 */
public function param($name = '', $default = null, $filter = null)
{// 設置 或者 獲取 當前請求的參數
    if (empty($this->param)) {// 如果當前的參數 爲空
        $method = $this->method(true);// 首先 獲取方法 也就獲取數據的類型
        // 自動獲取請求變量
        switch ($method) {// 根據不同的情況 設置
            case 'POST':// post 參數通過 post 方式進行獲取
                $vars = $this->post(false);
                break;
            case 'PUT':
            case 'DELETE':
            case 'PATCH':
                $vars = $this->put(false);// 其它的通過 put 方式獲取
                break;
            default:
                $vars = [];// 默認初始化 空數據
        }
        // 當前請求參數和URL地址中的參數合併
        $this->param = array_merge($this->get(false), $vars, $this->route(false));// 然後 合併 路由 get 及當前獲取的參數
        // 最後還都是大雜燴,
    }
    if (true === $name) {// 如果 名字 爲真
        // 獲取包含文件上傳信息的數組
        $file = $this->file();// 包含文件
        $data = array_merge($this->param, $file);// 合併參數 及文件,統稱爲 data
        return $this->input($data, '', $default, $filter);// 返回 輸入的數據
    }
    return $this->input($this->param, $name, $default, $filter);// 返回輸入的參數
}

/**
 * 設置獲取獲取路由參數
 * @access public
 * @param string|array  $name 變量名
 * @param mixed         $default 默認值
 * @param string|array  $filter 過濾方法
 * @return mixed
 */
public function route($name = '', $default = null, $filter = null)
{// 設置獲取路由參數
    if (is_array($name)) {// 如果是數組形式 這個即時優點,也是缺點
        $this->param        = [];// 拼合 參數
        return $this->route = array_merge($this->route, $name);// 返回 路由數據
    }
    return $this->input($this->route, $name, $default, $filter);// 否則 簡單的返回
}

/**
 * 設置獲取獲取GET參數
 * @access public
 * @param string|array  $name 變量名
 * @param mixed         $default 默認值
 * @param string|array  $filter 過濾方法
 * @return mixed
 */
public function get($name = '', $default = null, $filter = null)
{// get 方式獲取參數
    if (empty($this->get)) {// 如果爲空
        $this->get = $_GET;
    }
    if (is_array($name)) {// 如果是數組
        $this->param      = [];
        return $this->get = array_merge($this->get, $name);
    }
    return $this->input($this->get, $name, $default, $filter);
}// 函數內部的容錯機制需要特別的強悍

/**
 * 設置獲取獲取POST參數
 * @access public
 * @param string        $name 變量名
 * @param mixed         $default 默認值
 * @param string|array  $filter 過濾方法
 * @return mixed
 */
public function post($name = '', $default = null, $filter = null)
{
    if (empty($this->post)) {
        $this->post = $_POST;
    }
    if (is_array($name)) {
        $this->param       = [];
        return $this->post = array_merge($this->post, $name);
    }
    return $this->input($this->post, $name, $default, $filter);
}// 獲取 post 數據

/**
 * 設置獲取獲取PUT參數
 * @access public
 * @param string|array      $name 變量名
 * @param mixed             $default 默認值
 * @param string|array      $filter 過濾方法
 * @return mixed
 */
public function put($name = '', $default = null, $filter = null)
{// 獲取 PUT參數
    if (is_null($this->put)) {// 類型1
        $content = file_get_contents('php://input');// 這樣的數據流讀入
        if (strpos($content, '":')) {// 如果有數據,應該是這個表單
            $this->put = json_decode($content, true);// json 格式解析
        } else {
            parse_str($content, $this->put);// 另外的格式的話,就需要解析字符串
        }
    }
    if (is_array($name)) {// 如果是數組
        $this->param      = [];// 參數獲取
        return $this->put = is_null($this->put) ? $name : array_merge($this->put, $name);
    }// 返回參數信息

    return $this->input($this->put, $name, $default, $filter);// 返回 默認的信息
}

/**
 * 設置獲取獲取DELETE參數
 * @access public
 * @param string|array      $name 變量名
 * @param mixed             $default 默認值
 * @param string|array      $filter 過濾方法
 * @return mixed
 */
public function delete($name = '', $default = null, $filter = null)
{// 設置獲取 delete 參數
    return $this->put($name, $default, $filter);
}

/**
 * 設置獲取獲取PATCH參數
 * @access public
 * @param string|array      $name 變量名
 * @param mixed             $default 默認值
 * @param string|array      $filter 過濾方法
 * @return mixed
 */
public function patch($name = '', $default = null, $filter = null)
{
    return $this->put($name, $default, $filter);
}// 同上的 patch

/**
 * 獲取request變量
 * @param string        $name 數據名稱
 * @param string        $default 默認值
 * @param string|array  $filter 過濾方法
 * @return mixed
 */
public function request($name = '', $default = null, $filter = null)
{// 請求參數
    if (empty($this->request)) {
        $this->request = $_REQUEST;
    }
    if (is_array($name)) {
        $this->param          = [];
        return $this->request = array_merge($this->request, $name);
    }
    return $this->input($this->request, $name, $default, $filter);
}

/**
 * 獲取session數據
 * @access public
 * @param string|array  $name 數據名稱
 * @param string        $default 默認值
 * @param string|array  $filter 過濾方法
 * @return mixed
 */
public function session($name = '', $default = null, $filter = null)
{// session 字段
    if (empty($this->session)) {
        $this->session = Session::get();
    }// 設置session 字段
    if (is_array($name)) {
        return $this->session = array_merge($this->session, $name);
    }// 數組 賦值 組合 返回 一氣呵成 這個倒是不錯,哈哈
    return $this->input($this->session, $name, $default, $filter);
}

/**
 * 獲取cookie參數
 * @access public
 * @param string|array  $name 數據名稱
 * @param string        $default 默認值
 * @param string|array  $filter 過濾方法
 * @return mixed
 */
public function cookie($name = '', $default = null, $filter = null)
{// cookie 參數
    if (empty($this->cookie)) {
        $this->cookie = $_COOKIE;
    }
    if (is_array($name)) {
        return $this->cookie = array_merge($this->cookie, $name);
    }
    return $this->input($this->cookie, $name, $default, $filter);
}// 同上 類似

/**
 * 獲取server參數
 * @access public
 * @param string|array  $name 數據名稱
 * @param string        $default 默認值
 * @param string|array  $filter 過濾方法
 * @return mixed
 */
public function server($name = '', $default = null, $filter = null)
{// 獲取 server 參數
    if (empty($this->server)) {
        $this->server = $_SERVER;
    }
    if (is_array($name)) {
        return $this->server = array_merge($this->server, $name);
    }
    return $this->input($this->server, false === $name ? false : strtoupper($name), $default, $filter);
}// 同上

/**
 * 獲取上傳的文件信息
 * @access public
 * @param string|array $name 名稱
 * @return null|array|\think\File
 */
public function file($name = '')
{
    if (empty($this->file)) {
        $this->file = isset($_FILES) ? $_FILES : [];
    }// 如果爲空,獲取全部信息
    if (is_array($name)) {
        return $this->file = array_merge($this->file, $name);
    }// 信息拼合
    $files = $this->file;// 文件 暫存處理
    if (!empty($files)) {
        // 處理上傳文件
        $array = [];
        foreach ($files as $key => $file) {
            if (is_array($file['name'])) {// 如果是多文件上傳
                $item  = [];
                $keys  = array_keys($file);
                $count = count($file['name']);
                for ($i = 0; $i < $count; $i++) {
                    if (empty($file['tmp_name'][$i])) {
                        continue;
                    }
                    $temp['key'] = $key;
                    foreach ($keys as $_key) {
                        $temp[$_key] = $file[$_key][$i];
                    }
                    $item[] = (new File($temp['tmp_name']))->setUploadInfo($temp);
                }
                $array[$key] = $item;
            } else {
                if ($file instanceof File) {// 如果是單獨的上傳文件
                    $array[$key] = $file;
                } else {
                    if (empty($file['tmp_name'])) {
                        continue;
                    }
                    $array[$key] = (new File($file['tmp_name']))->setUploadInfo($file);
                }
            }
        }
        if (strpos($name, '.')) {// 如果文件名的格式比較特殊
            list($name, $sub) = explode('.', $name);
        }
        if ('' === $name) {// 如果 文件名
            // 獲取全部文件
            return $array;
        } elseif (isset($sub) && isset($array[$name][$sub])) {// 設置
            return $array[$name][$sub];// 返回子
        } elseif (isset($array[$name])) {
            return $array[$name];
        }
    }
    return null;// 默認 返回空
}

/**
 * 獲取環境變量
 * @param string|array  $name 數據名稱
 * @param string        $default 默認值
 * @param string|array  $filter 過濾方法
 * @return mixed
 */
public function env($name = '', $default = null, $filter = null)
{// 獲取環境變量
    if (empty($this->env)) {// 如果爲空
        $this->env = $_ENV;
    }
    if (is_array($name)) {
        return $this->env = array_merge($this->env, $name);
    }
    return $this->input($this->env, false === $name ? false : strtoupper($name), $default, $filter);
}// 同上的 其它方式


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