Files
zhamao-framework/src/ZM/Http/Response.php

266 lines
5.6 KiB
PHP
Raw Normal View History

<?php
/**
* @noinspection PhpMissingReturnTypeInspection
* @noinspection PhpUnused
*/
2020-03-02 16:14:20 +08:00
declare(strict_types=1);
2020-03-02 16:14:20 +08:00
namespace ZM\Http;
2022-04-02 23:37:22 +08:00
use Stringable;
2020-03-02 16:14:20 +08:00
class Response
{
public $fd = 0;
public $socket;
2020-03-02 16:14:20 +08:00
public $header;
2020-03-02 16:14:20 +08:00
public $cookie;
public $trailer;
2020-03-02 16:14:20 +08:00
/**
* @var \Swoole\Http\Response
*/
private $response;
2020-03-02 16:14:20 +08:00
private $is_end = false;
private $status_code;
2020-03-02 16:14:20 +08:00
public function __construct(\Swoole\Http\Response $response)
{
2020-03-02 16:14:20 +08:00
$this->response = $response;
$this->fd = $response->fd;
$this->socket = $response->socket;
$this->header = $response->header;
$this->cookie = $response->cookie;
if (isset($response->trailer)) {
2020-03-02 16:14:20 +08:00
$this->trailer = $response->trailer;
}
}
public function __destruct()
{
2020-03-02 16:14:20 +08:00
}
/**
* @return mixed
*/
public function initHeader()
{
2020-03-02 16:14:20 +08:00
return $this->response->initHeader();
}
/**
2022-04-02 23:37:22 +08:00
* @param string $name 名称
* @param mixed ...$params 参数
* @return mixed 返回值
2020-03-02 16:14:20 +08:00
*/
2022-04-02 23:37:22 +08:00
public function cookie(string $name, ...$params)
{
return empty($params) ? $this->response->rawcookie($name) : $this->response->rawcookie($name, ...$params);
2020-03-02 16:14:20 +08:00
}
/**
* @param mixed ...$params
* @param mixed $name
2020-03-02 16:14:20 +08:00
* @return mixed
*/
public function setCookie($name, ...$params)
{
return empty($params) ? $this->response->rawcookie($name) : $this->response->setCookie($name, ...$params);
2020-03-02 16:14:20 +08:00
}
/**
* @param mixed ...$params
* @param mixed $name
2020-03-02 16:14:20 +08:00
* @return mixed
*/
public function rawcookie($name, ...$params)
{
return empty($params) ? $this->response->rawcookie($name) : $this->response->rawcookie($name, ...$params);
2020-03-02 16:14:20 +08:00
}
/**
2022-04-02 23:37:22 +08:00
* @param mixed ...$params
2020-03-02 16:14:20 +08:00
* @return mixed
*/
2022-04-02 23:37:22 +08:00
public function status(int $http_code, ...$params)
{
$this->status_code = $http_code;
if (!$this->is_end) {
return empty($params) ? $this->response->status($http_code) : $this->response->status($http_code, ...$params);
}
return false;
2020-03-02 16:14:20 +08:00
}
public function getStatusCode()
{
return $this->status_code ?? 200;
}
2020-03-02 16:14:20 +08:00
/**
2022-04-02 23:37:22 +08:00
* @param mixed ...$params
2020-03-02 16:14:20 +08:00
* @return mixed
*/
2022-04-02 23:37:22 +08:00
public function setStatusCode(int $http_code, ...$params)
{
if (!$this->is_end) {
return empty($params) ? $this->response->status($http_code) : $this->response->status($http_code, ...$params);
}
return false;
2020-03-02 16:14:20 +08:00
}
/**
2022-04-02 23:37:22 +08:00
* @param array|string $value
* @param null|array|string $ucwords
2020-03-02 16:14:20 +08:00
* @return mixed
*/
2022-04-02 23:37:22 +08:00
public function header(string $key, $value, $ucwords = null)
{
if (!$this->is_end) {
return $ucwords === null ? $this->response->header($key, $value) : $this->response->header($key, $value, $ucwords);
}
return false;
2020-03-02 16:14:20 +08:00
}
/**
2022-04-02 23:37:22 +08:00
* @param array|string $value
* @param null|array|string $ucwords
2020-03-02 16:14:20 +08:00
* @return mixed
*/
2022-04-02 23:37:22 +08:00
public function setHeader(string $key, $value, $ucwords = null)
{
if (!$this->is_end) {
return $ucwords === null ? $this->response->setHeader($key, $value) : $this->response->setHeader($key, $value, $ucwords);
}
return false;
2020-03-02 16:14:20 +08:00
}
/**
2022-04-02 23:37:22 +08:00
* @param array|string $value
2020-03-02 16:14:20 +08:00
* @return mixed
*/
2022-04-02 23:37:22 +08:00
public function trailer(string $key, $value)
{
2020-03-02 16:14:20 +08:00
return $this->response->trailer($key, $value);
}
/**
* @return mixed
*/
public function ping()
{
2020-03-02 16:14:20 +08:00
return $this->response->ping();
}
/**
2022-04-02 23:37:22 +08:00
* @param string|Stringable $content
2020-03-02 16:14:20 +08:00
* @return mixed
*/
public function write($content)
{
2020-03-02 16:14:20 +08:00
return $this->response->write($content);
}
/**
2022-04-02 23:37:22 +08:00
* @param null|string|Stringable $content
2020-03-02 16:14:20 +08:00
* @return mixed
*/
public function end($content = null)
{
2021-02-09 17:09:09 +08:00
if (!$this->is_end) {
2021-01-02 19:44:19 +08:00
$this->is_end = true;
return $this->response->end($content);
}
return false;
2020-03-02 16:14:20 +08:00
}
public function isEnd()
{
return $this->is_end;
}
2020-03-02 16:14:20 +08:00
public function endWithStatus($status_code = 200, $content = null)
{
$this->status($status_code);
$this->end($content);
}
2020-03-02 16:14:20 +08:00
/**
2022-04-02 23:37:22 +08:00
* @param null|int|string $offset
* @param null|int|string $length
2020-03-02 16:14:20 +08:00
* @return mixed
*/
2022-04-02 23:37:22 +08:00
public function sendfile(string $filename, $offset = null, $length = null)
{
2020-03-02 16:14:20 +08:00
return $this->response->sendfile($filename, $offset, $length);
}
/**
* @return mixed
*/
2022-04-02 23:37:22 +08:00
public function redirect(string $location, ?int $http_code = null)
{
$this->is_end = true;
return $this->response->redirect($location, $http_code);
2020-03-02 16:14:20 +08:00
}
/**
* @return mixed
*/
public function detach()
{
2020-03-02 16:14:20 +08:00
return $this->response->detach();
}
/**
2022-04-02 23:37:22 +08:00
* @param mixed $fd
2020-03-02 16:14:20 +08:00
* @return mixed
*/
public static function create($fd)
{
2020-03-02 16:14:20 +08:00
return \Swoole\Http\Response::create($fd);
}
/**
* @return mixed
*/
public function upgrade()
{
2020-03-02 16:14:20 +08:00
return $this->response->upgrade();
}
/**
2022-04-02 23:37:22 +08:00
* @param mixed $data
* @param mixed $opcode
* @param mixed $flags
2020-03-02 16:14:20 +08:00
* @return mixed
*/
public function push($data, $opcode = null, $flags = null)
{
2020-03-02 16:14:20 +08:00
return $this->response->push($data, $opcode, $flags);
}
/**
* @return mixed
*/
public function recv()
{
2020-03-02 16:14:20 +08:00
return $this->response->recv();
}
/**
* @return mixed
*/
public function close()
{
2020-03-02 16:14:20 +08:00
return $this->response->close();
}
}