您的位置:首页 > 编程语言 > PHP开发

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

2017-01-11 09:03 218 查看
<?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 <liu21st@gmail.com>
// +----------------------------------------------------------------------

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  think php5