新增
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Exception;
|
||||
use think\Response;
|
||||
|
||||
/**
|
||||
* File Response
|
||||
*/
|
||||
class File extends Response
|
||||
{
|
||||
protected $expire = 360;
|
||||
protected $name;
|
||||
protected $mimeType;
|
||||
protected $isContent = false;
|
||||
protected $force = true;
|
||||
|
||||
public function __construct($data = '', int $code = 200)
|
||||
{
|
||||
$this->init($data, $code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function output($data)
|
||||
{
|
||||
if (!$this->isContent && !is_file($data)) {
|
||||
throw new Exception('file not exists:' . $data);
|
||||
}
|
||||
|
||||
while (ob_get_level() > 0) {
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
if (!empty($this->name)) {
|
||||
$name = $this->name;
|
||||
} else {
|
||||
$name = !$this->isContent ? pathinfo($data, PATHINFO_BASENAME) : '';
|
||||
}
|
||||
|
||||
if ($this->isContent) {
|
||||
$mimeType = $this->mimeType;
|
||||
$size = strlen($data);
|
||||
} else {
|
||||
$mimeType = $this->getMimeType($data);
|
||||
$size = filesize($data);
|
||||
}
|
||||
|
||||
$this->header['Pragma'] = 'public';
|
||||
$this->header['Content-Type'] = $mimeType ?: 'application/octet-stream';
|
||||
$this->header['Cache-control'] = 'max-age=' . $this->expire;
|
||||
$this->header['Content-Disposition'] = ($this->force ? 'attachment; ' : '') . 'filename="' . $name . '"';
|
||||
$this->header['Content-Length'] = $size;
|
||||
$this->header['Content-Transfer-Encoding'] = 'binary';
|
||||
$this->header['Expires'] = gmdate("D, d M Y H:i:s", time() + $this->expire) . ' GMT';
|
||||
|
||||
$this->lastModified(gmdate('D, d M Y H:i:s', time()) . ' GMT');
|
||||
|
||||
return $this->isContent ? $data : file_get_contents($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否为内容 必须配合mimeType方法使用
|
||||
* @access public
|
||||
* @param bool $content
|
||||
* @return $this
|
||||
*/
|
||||
public function isContent(bool $content = true)
|
||||
{
|
||||
$this->isContent = $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置有效期
|
||||
* @access public
|
||||
* @param integer $expire 有效期
|
||||
* @return $this
|
||||
*/
|
||||
public function expire(int $expire)
|
||||
{
|
||||
$this->expire = $expire;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置文件类型
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @return $this
|
||||
*/
|
||||
public function mimeType(string $mimeType)
|
||||
{
|
||||
$this->mimeType = $mimeType;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置文件强制下载
|
||||
* @access public
|
||||
* @param bool $force 强制浏览器下载
|
||||
* @return $this
|
||||
*/
|
||||
public function force(bool $force)
|
||||
{
|
||||
$this->force = $force;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件类型信息
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @return string
|
||||
*/
|
||||
protected function getMimeType(string $filename): string
|
||||
{
|
||||
if (!empty($this->mimeType)) {
|
||||
return $this->mimeType;
|
||||
}
|
||||
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
|
||||
return finfo_file($finfo, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置下载文件的显示名称
|
||||
* @access public
|
||||
* @param string $filename 文件名
|
||||
* @param bool $extension 后缀自动识别
|
||||
* @return $this
|
||||
*/
|
||||
public function name(string $filename, bool $extension = true)
|
||||
{
|
||||
$this->name = $filename;
|
||||
|
||||
if ($extension && !str_contains($filename, '.')) {
|
||||
$this->name .= '.' . pathinfo($this->data, PATHINFO_EXTENSION);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Cookie;
|
||||
use think\Response;
|
||||
|
||||
/**
|
||||
* Html Response
|
||||
*/
|
||||
class Html extends Response
|
||||
{
|
||||
/**
|
||||
* 输出type
|
||||
* @var string
|
||||
*/
|
||||
protected $contentType = 'text/html';
|
||||
|
||||
public function __construct(Cookie $cookie, $data = '', int $code = 200)
|
||||
{
|
||||
$this->init($data, $code);
|
||||
$this->cookie = $cookie;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Cookie;
|
||||
use think\Response;
|
||||
|
||||
/**
|
||||
* Json Response
|
||||
*/
|
||||
class Json extends Response
|
||||
{
|
||||
// 输出参数
|
||||
protected $options = [
|
||||
'json_encode_param' => JSON_UNESCAPED_UNICODE,
|
||||
];
|
||||
|
||||
protected $contentType = 'application/json';
|
||||
|
||||
public function __construct(Cookie $cookie, $data = '', int $code = 200)
|
||||
{
|
||||
$this->init($data, $code);
|
||||
$this->cookie = $cookie;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function output($data): string
|
||||
{
|
||||
try {
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
$data = json_encode($data, $this->options['json_encode_param']);
|
||||
|
||||
if (false === $data) {
|
||||
throw new \InvalidArgumentException(json_last_error_msg());
|
||||
}
|
||||
|
||||
return $data;
|
||||
} catch (\Exception $e) {
|
||||
if ($e->getPrevious()) {
|
||||
throw $e->getPrevious();
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Cookie;
|
||||
use think\Request;
|
||||
use think\Response;
|
||||
|
||||
/**
|
||||
* Jsonp Response
|
||||
*/
|
||||
class Jsonp extends Response
|
||||
{
|
||||
// 输出参数
|
||||
protected $options = [
|
||||
'var_jsonp_handler' => 'callback',
|
||||
'default_jsonp_handler' => 'jsonpReturn',
|
||||
'json_encode_param' => JSON_UNESCAPED_UNICODE,
|
||||
];
|
||||
|
||||
protected $contentType = 'application/javascript';
|
||||
|
||||
protected $request;
|
||||
|
||||
public function __construct(Cookie $cookie, Request $request, $data = '', int $code = 200)
|
||||
{
|
||||
$this->init($data, $code);
|
||||
|
||||
$this->cookie = $cookie;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function output($data): string
|
||||
{
|
||||
try {
|
||||
// 返回JSON数据格式到客户端 包含状态信息 [当url_common_param为false时是无法获取到$_GET的数据的,故使用Request来获取<xiaobo.sun@qq.com>]
|
||||
$varJsonpHandler = $this->request->param($this->options['var_jsonp_handler'], "");
|
||||
$handler = !empty($varJsonpHandler) ? $varJsonpHandler : $this->options['default_jsonp_handler'];
|
||||
|
||||
$data = json_encode($data, $this->options['json_encode_param']);
|
||||
|
||||
if (false === $data) {
|
||||
throw new \InvalidArgumentException(json_last_error_msg());
|
||||
}
|
||||
|
||||
$data = $handler . '(' . $data . ');';
|
||||
|
||||
return $data;
|
||||
} catch (\Exception $e) {
|
||||
if ($e->getPrevious()) {
|
||||
throw $e->getPrevious();
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Cookie;
|
||||
use think\Request;
|
||||
use think\Response;
|
||||
use think\Session;
|
||||
|
||||
/**
|
||||
* Redirect Response
|
||||
*/
|
||||
class Redirect extends Response
|
||||
{
|
||||
|
||||
protected $request;
|
||||
|
||||
public function __construct(Cookie $cookie, Request $request, Session $session, $data = '', int $code = 302)
|
||||
{
|
||||
$this->init((string) $data, $code);
|
||||
|
||||
$this->cookie = $cookie;
|
||||
$this->request = $request;
|
||||
$this->session = $session;
|
||||
|
||||
$this->cacheControl('no-cache,must-revalidate');
|
||||
}
|
||||
|
||||
public function data($data)
|
||||
{
|
||||
$this->header['Location'] = $data;
|
||||
return parent::data($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return string
|
||||
*/
|
||||
protected function output($data): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 重定向传值(通过Session)
|
||||
* @access protected
|
||||
* @param string|array $name 变量名或者数组
|
||||
* @param mixed $value 值
|
||||
* @return $this
|
||||
*/
|
||||
public function with($name, $value = null)
|
||||
{
|
||||
if (is_array($name)) {
|
||||
foreach ($name as $key => $val) {
|
||||
$this->session->flash($key, $val);
|
||||
}
|
||||
} else {
|
||||
$this->session->flash($name, $value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记住当前url后跳转
|
||||
* @access public
|
||||
* @return $this
|
||||
*/
|
||||
public function remember($complete = false)
|
||||
{
|
||||
$this->session->set('redirect_url', $this->request->url($complete));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳转到上次记住的url
|
||||
* @access public
|
||||
* @return $this
|
||||
*/
|
||||
public function restore()
|
||||
{
|
||||
if ($this->session->has('redirect_url')) {
|
||||
$this->data = $this->session->get('redirect_url');
|
||||
$this->session->delete('redirect_url');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Cookie;
|
||||
use think\Response;
|
||||
use think\View as BaseView;
|
||||
|
||||
/**
|
||||
* View Response
|
||||
*/
|
||||
class View extends Response
|
||||
{
|
||||
/**
|
||||
* 输出参数
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* 输出变量
|
||||
* @var array
|
||||
*/
|
||||
protected $vars = [];
|
||||
|
||||
/**
|
||||
* 输出过滤
|
||||
* @var mixed
|
||||
*/
|
||||
protected $filter;
|
||||
|
||||
/**
|
||||
* 输出type
|
||||
* @var string
|
||||
*/
|
||||
protected $contentType = 'text/html';
|
||||
|
||||
/**
|
||||
* View对象
|
||||
* @var BaseView
|
||||
*/
|
||||
protected $view;
|
||||
|
||||
/**
|
||||
* 是否内容渲染
|
||||
* @var bool
|
||||
*/
|
||||
protected $isContent = false;
|
||||
|
||||
public function __construct(Cookie $cookie, BaseView $view, $data = '', int $code = 200)
|
||||
{
|
||||
$this->init($data, $code);
|
||||
|
||||
$this->cookie = $cookie;
|
||||
$this->view = $view;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否为内容渲染
|
||||
* @access public
|
||||
* @param bool $content
|
||||
* @return $this
|
||||
*/
|
||||
public function isContent(bool $content = true)
|
||||
{
|
||||
$this->isContent = $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return string
|
||||
*/
|
||||
protected function output($data): string
|
||||
{
|
||||
// 渲染模板输出
|
||||
$this->view->filter($this->filter);
|
||||
return $this->isContent ?
|
||||
$this->view->display($data, $this->vars) :
|
||||
$this->view->fetch($data, $this->vars);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取视图变量
|
||||
* @access public
|
||||
* @param string $name 模板变量
|
||||
* @return mixed
|
||||
*/
|
||||
public function getVars(string $name = null)
|
||||
{
|
||||
if (is_null($name)) {
|
||||
return $this->vars;
|
||||
} else {
|
||||
return $this->vars[$name] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板变量赋值
|
||||
* @access public
|
||||
* @param string|array $name 模板变量
|
||||
* @param mixed $value 变量值
|
||||
* @return $this
|
||||
*/
|
||||
public function assign(string|array $name, $value = null)
|
||||
{
|
||||
if (is_array($name)) {
|
||||
$this->vars = array_merge($this->vars, $name);
|
||||
} else {
|
||||
$this->vars[$name] = $value;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 视图内容过滤
|
||||
* @access public
|
||||
* @param callable $filter
|
||||
* @return $this
|
||||
*/
|
||||
public function filter(callable $filter = null)
|
||||
{
|
||||
$this->filter = $filter;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查模板是否存在
|
||||
* @access public
|
||||
* @param string $name 模板名
|
||||
* @return bool
|
||||
*/
|
||||
public function exists(string $name): bool
|
||||
{
|
||||
return $this->view->exists($name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace think\response;
|
||||
|
||||
use think\Collection;
|
||||
use think\Cookie;
|
||||
use think\Model;
|
||||
use think\Response;
|
||||
|
||||
/**
|
||||
* XML Response
|
||||
*/
|
||||
class Xml extends Response
|
||||
{
|
||||
// 输出参数
|
||||
protected $options = [
|
||||
// 根节点名
|
||||
'root_node' => 'think',
|
||||
// 根节点属性
|
||||
'root_attr' => '',
|
||||
//数字索引的子节点名
|
||||
'item_node' => 'item',
|
||||
// 数字索引子节点key转换的属性名
|
||||
'item_key' => 'id',
|
||||
// 数据编码
|
||||
'encoding' => 'utf-8',
|
||||
];
|
||||
|
||||
protected $contentType = 'text/xml';
|
||||
|
||||
public function __construct(Cookie $cookie, $data = '', int $code = 200)
|
||||
{
|
||||
$this->init($data, $code);
|
||||
$this->cookie = $cookie;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @access protected
|
||||
* @param mixed $data 要处理的数据
|
||||
* @return mixed
|
||||
*/
|
||||
protected function output($data): string
|
||||
{
|
||||
if (is_string($data)) {
|
||||
if (!str_starts_with($data, '<?xml')) {
|
||||
$encoding = $this->options['encoding'];
|
||||
$xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
|
||||
$data = $xml . $data;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
// XML数据转换
|
||||
return $this->xmlEncode($data, $this->options['root_node'], $this->options['item_node'], $this->options['root_attr'], $this->options['item_key'], $this->options['encoding']);
|
||||
}
|
||||
|
||||
/**
|
||||
* XML编码
|
||||
* @access protected
|
||||
* @param mixed $data 数据
|
||||
* @param string $root 根节点名
|
||||
* @param string $item 数字索引的子节点名
|
||||
* @param mixed $attr 根节点属性
|
||||
* @param string $id 数字索引子节点key转换的属性名
|
||||
* @param string $encoding 数据编码
|
||||
* @return string
|
||||
*/
|
||||
protected function xmlEncode($data, string $root, string $item, $attr, string $id, string $encoding): string
|
||||
{
|
||||
if (is_array($attr)) {
|
||||
$array = [];
|
||||
foreach ($attr as $key => $value) {
|
||||
$array[] = "{$key}=\"{$value}\"";
|
||||
}
|
||||
$attr = implode(' ', $array);
|
||||
}
|
||||
|
||||
$attr = trim($attr);
|
||||
$attr = empty($attr) ? '' : " {$attr}";
|
||||
$xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
|
||||
$xml .= "<{$root}{$attr}>";
|
||||
$xml .= $this->dataToXml($data, $item, $id);
|
||||
$xml .= "</{$root}>";
|
||||
|
||||
return $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据XML编码
|
||||
* @access protected
|
||||
* @param mixed $data 数据
|
||||
* @param string $item 数字索引时的节点名称
|
||||
* @param string $id 数字索引key转换为的属性名
|
||||
* @return string
|
||||
*/
|
||||
protected function dataToXml($data, string $item, string $id): string
|
||||
{
|
||||
$xml = $attr = '';
|
||||
|
||||
if ($data instanceof Collection || $data instanceof Model) {
|
||||
$data = $data->toArray();
|
||||
}
|
||||
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_numeric($key)) {
|
||||
$id && $attr = " {$id}=\"{$key}\"";
|
||||
$key = $item;
|
||||
}
|
||||
$xml .= "<{$key}{$attr}>";
|
||||
$xml .= (is_array($val) || is_object($val)) ? $this->dataToXml($val, $item, $id) : $val;
|
||||
$xml .= "</{$key}>";
|
||||
}
|
||||
|
||||
return $xml;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user