[李景山php]每天TP5-20170204|thinkphp5-Response.php

來源:互聯網
上載者:User

標籤:thinkphp5

<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <[email protected]>// +----------------------------------------------------------------------namespace think;// 使用命名空間use think\Config;use think\Debug;use think\Env;use think\response\Json as JsonResponse;use think\response\Jsonp as JsonpResponse;use think\response\Redirect as RedirectResponse;use think\response\View as ViewResponse;use think\response\Xml as XmlResponse;// 使用 其它資源class Response{    // 未經處理資料    protected $data;// 元素資料    // 當前的contentType    protected $contentType = ‘text/html‘;// 當前的 類型 content Type    // 字元集    protected $charset = ‘utf-8‘;// 保護的字元集    //狀態    protected $code = 200;// 返回狀態    // 輸出參數    protected $options = [];// 輸出的參數    // header參數    protected $header = [];// 參數    protected $content = null;// 內容    /**     * 架構函數     * @access   public     * @param mixed $data    輸出資料     * @param int   $code     * @param array $header     * @param array $options 輸出參數     */    public function __construct($data = ‘‘, $code = 200, array $header = [], $options = [])    {// 建構函式 初始化 各種資料        $this->data($data);// 處理 資料群組合方式        $this->header = $header;// 頭資訊 處理        $this->code   = $code;// 返回碼 預設 200        if (!empty($options)) {// 如果 不為空白 合并選項            $this->options = array_merge($this->options, $options);        }        $this->contentType($this->contentType, $this->charset);// 整理 類型 關於 內容    }    /**     * 建立Response對象     * @access public     * @param mixed  $data    輸出資料     * @param string $type    輸出類型     * @param int    $code     * @param array  $header     * @param array  $options 輸出參數     * @return Response|JsonResponse|ViewResponse|XmlResponse|RedirectResponse|JsonpResponse     */    public static function create($data = ‘‘, $type = ‘‘, $code = 200, array $header = [], $options = [])    {// 建構函式 初始化對象 跟 建立對象        $type = empty($type) ? ‘null‘ : strtolower($type);// 如果 為空白 null 並且 小寫        $class = false !== strpos($type, ‘\\‘) ? $type : ‘\\think\\response\\‘ . ucfirst($type);// 細分到其它的內部具體的調度類        if (class_exists($class)) {// 存在對應的處理分支            $response = new $class($data, $code, $header, $options);        } else {// 否則 用預設的 靜態 函數            $response = new static($data, $code, $header, $options);        }        return $response;// 返回 跟 單純的單列模式還不太一樣,哈哈    }    /**     * 發送資料到用戶端     * @access public     * @return mixed     * @throws \InvalidArgumentException     */    public function send()    {        // 處理輸出資料        $data = $this->getContent();// 處理 輸出的資料        // Trace調試注入        if (Env::get(‘app_trace‘, Config::get(‘app_trace‘))) {// 環境:: 擷取 是否調試 擷取 配置 關於調試            Debug::inject($this, $data);// Debug::inject() 注入當前的        }        if (!headers_sent() && !empty($this->header)) {// 如果需要發送頭資訊            // 發送狀態代碼            http_response_code($this->code);// 返回http 頭 狀態代碼            // 發送頭部資訊            foreach ($this->header as $name => $val) {// 遍曆頭資訊                header($name . ‘:‘ . $val);// 使用 header 標記選項 跟 數值            }        }        echo $data;// 直接列印資料        if (function_exists(‘fastcgi_finish_request‘)) {            // 提高頁面響應            fastcgi_finish_request();            // 此函數沖刷(flush)所有響應的資料給用戶端並結束請求。 這使得用戶端結束串連後,需要大量時間啟動並執行任務能夠繼續運行。            // 節省 用戶端 輸出 等待        }    }    /**     * 處理資料     * @access protected     * @param mixed $data 要處理的資料     * @return mixed     */    protected function output($data)    {// 處理資料 output 就直接 return 資料        return $data;    }    /**     * 輸出的參數     * @access public     * @param mixed $options 輸出參數     * @return $this     */    public function options($options = [])    {// 輸出的參數        $this->options = array_merge($this->options, $options);        return $this;    }// 命令鏈方式的 代碼 融合    /**     * 輸出資料設定     * @access public     * @param mixed $data 輸出資料     * @return $this     */    public function data($data)    {        $this->data = $data;        return $this;    }// 設定 資料 命令鏈    /**     * 設定回應標頭     * @access public     * @param string|array $name  參數名     * @param string       $value 參數值     * @return $this     */    public function header($name, $value = null)    {        if (is_array($name)) {            $this->header = array_merge($this->header, $name);        } else {            $this->header[$name] = $value;        }        return $this;    }// 設定 頭資訊 命令鏈    /**     * 設定頁面輸出內容     * @param $content     * @return $this     */    public function content($content)    {        if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([            $content,            ‘__toString‘,        ])        ) {            throw new \InvalidArgumentException(sprintf(‘variable type error: %s‘, gettype($content)));        }        $this->content = (string) $content;        return $this;    }// 設定 符合規矩的頁面內容 命令鏈    /**     * 發送HTTP狀態     * @param integer $code 狀態代碼     * @return $this     */    public function code($code)    {        $this->code = $code;        return $this;    }    /**     * LastModified     * @param string $time     * @return $this     */    public function lastModified($time)    {        $this->header[‘Last-Modified‘] = $time;        return $this;    }    /**     * Expires     * @param string $time     * @return $this     */    public function expires($time)    {        $this->header[‘Expires‘] = $time;        return $this;    }    /**     * ETag     * @param string $eTag     * @return $this     */    public function eTag($eTag)    {        $this->header[‘ETag‘] = $eTag;        return $this;    }    /**     * 頁面緩衝控制     * @param string $cache 狀態代碼     * @return $this     */    public function cacheControl($cache)    {        $this->header[‘Cache-control‘] = $cache;        return $this;    }    /**     * 頁面輸出類型     * @param string $contentType 輸出類型     * @param string $charset     輸出編碼     * @return $this     */    public function contentType($contentType, $charset = ‘utf-8‘)    {        $this->header[‘Content-Type‘] = $contentType . ‘; charset=‘ . $charset;        return $this;    }    /**     * 擷取頭部資訊     * @param string $name 頭部名稱     * @return mixed     */    public function getHeader($name = ‘‘)    {        return !empty($name) ? $this->header[$name] : $this->header;    }    /**     * 擷取未經處理資料     * @return mixed     */    public function getData()    {        return $this->data;    }    /**     * 擷取輸出資料     * @return mixed     */    public function getContent()    {        if (null == $this->content) {            $content = $this->output($this->data);            if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([                $content,                ‘__toString‘,            ])            ) {                throw new \InvalidArgumentException(sprintf(‘variable type error: %s‘, gettype($content)));            }            $this->content = (string) $content;        }        return $this->content;    }// 拋出異常    /**     * 擷取狀態代碼     * @return integer     */    public function getCode()    {        return $this->code;    }}


本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1890943

[李景山php]每天TP5-20170204|thinkphp5-Response.php

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.