Files
kiri-core/HttpServer/Http/Response.php
T

333 lines
6.3 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/24 0024
* Time: 19:39
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace HttpServer\Http;
2021-02-20 17:33:28 +08:00
use HttpServer\Abstracts\HttpService;
2020-08-31 01:27:08 +08:00
use HttpServer\Http\Formatter\HtmlFormatter;
use HttpServer\Http\Formatter\JsonFormatter;
use HttpServer\Http\Formatter\XmlFormatter;
use Exception;
2021-03-05 11:57:58 +08:00
use JetBrains\PhpStorm\Pure;
2020-08-31 01:27:08 +08:00
use Snowflake\Core\Help;
use Snowflake\Snowflake;
use Swoole\Http\Response as SResponse;
2020-09-16 20:24:37 +08:00
use Swoole\Http2\Response as S2Response;
2020-08-31 01:27:08 +08:00
/**
* Class Response
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Http
2020-08-31 01:27:08 +08:00
*/
2021-02-20 17:33:28 +08:00
class Response extends HttpService
2020-08-31 01:27:08 +08:00
{
const JSON = 'json';
const XML = 'xml';
const HTML = 'html';
2020-10-29 18:17:25 +08:00
/** @var ?string */
public ?string $format = null;
2020-08-31 01:27:08 +08:00
/** @var int */
2020-10-29 18:17:25 +08:00
public int $statusCode = 200;
2020-08-31 01:27:08 +08:00
2020-10-30 01:11:24 +08:00
public ?SResponse $response = null;
2020-10-29 18:17:25 +08:00
public bool $isWebSocket = false;
public array $headers = [];
2020-08-31 01:27:08 +08:00
2020-10-30 01:27:29 +08:00
private float $startTime = 0;
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
private array $_format_maps = [
2020-08-31 01:27:08 +08:00
self::JSON => JsonFormatter::class,
self::XML => XmlFormatter::class,
self::HTML => HtmlFormatter::class
];
2020-11-16 14:22:38 +08:00
public int $fd = 0;
2020-08-31 01:27:08 +08:00
/**
* @param $format
* @return $this
*/
2020-12-25 15:57:52 +08:00
public function setFormat($format): static
2020-08-31 01:27:08 +08:00
{
$this->format = $format;
return $this;
}
/**
* 清理无用数据
*/
2020-12-25 15:57:52 +08:00
public function clear(): void
2020-08-31 01:27:08 +08:00
{
$this->fd = 0;
$this->isWebSocket = false;
$this->format = null;
}
/**
* @return string
*/
2020-12-25 15:57:52 +08:00
public function getContentType(): string
2020-08-31 01:27:08 +08:00
{
if ($this->format == null || $this->format == static::JSON) {
return 'application/json;charset=utf-8';
} else if ($this->format == static::XML) {
return 'application/xml;charset=utf-8';
} else {
return 'text/html;charset=utf-8';
}
}
2020-10-30 10:53:20 +08:00
/**
* @param $content
* @return mixed
*/
2020-12-25 15:57:52 +08:00
public function toHtml($content): mixed
2020-10-30 10:53:20 +08:00
{
$this->format = self::HTML;
return $content;
}
/**
* @param $content
* @return mixed
*/
2020-12-25 15:57:52 +08:00
public function toJson($content): mixed
2020-10-30 10:53:20 +08:00
{
$this->format = self::JSON;
return $content;
}
/**
* @param $content
* @return mixed
*/
2020-12-25 15:57:52 +08:00
public function toXml($content): mixed
2020-10-30 10:53:20 +08:00
{
$this->format = self::XML;
return $content;
}
2020-08-31 01:27:08 +08:00
/**
* @return mixed
* @throws Exception
*/
2020-12-25 15:57:52 +08:00
public function sender(): mixed
2020-08-31 01:27:08 +08:00
{
return $this->send(func_get_args());
}
/**
* @param $key
* @param $value
2020-09-17 14:26:38 +08:00
* @return Response
2020-08-31 01:27:08 +08:00
*/
2020-12-25 15:57:52 +08:00
public function addHeader($key, $value): static
2020-08-31 01:27:08 +08:00
{
2020-09-17 14:26:38 +08:00
$this->headers[$key] = $value;
return $this;
2020-08-31 01:27:08 +08:00
}
2020-09-16 20:49:22 +08:00
/**
* @return bool
*/
2020-12-25 15:57:52 +08:00
private function isClient(): bool
2020-09-16 20:49:22 +08:00
{
return !($this->response instanceof SResponse) && !($this->response instanceof S2Response);
}
2020-08-31 01:27:08 +08:00
/**
* @param string $context
* @param int $statusCode
* @param null $response
* @return bool
* @throws Exception
*/
2020-12-25 15:57:52 +08:00
public function send($context = '', $statusCode = 200, $response = null): mixed
2020-08-31 01:27:08 +08:00
{
$sendData = $this->parseData($context);
if ($response instanceof SResponse) {
$this->response = $response;
}
if ($this->response instanceof SResponse) {
2020-09-15 18:30:53 +08:00
return $this->sendData($sendData, $statusCode);
2020-08-31 01:27:08 +08:00
} else {
2021-03-05 13:58:40 +08:00
if (!empty(request()->fd)) {
return '';
}
2020-08-31 01:27:08 +08:00
return $this->printResult($sendData);
}
}
/**
* @param $context
* @return mixed
* @throws Exception
*/
2020-12-25 15:57:52 +08:00
private function parseData($context): mixed
2020-08-31 01:27:08 +08:00
{
2021-03-09 17:20:50 +08:00
if ($context === null) {
return '';
}
2020-08-31 01:27:08 +08:00
if (isset($this->_format_maps[$this->format])) {
$config['class'] = $this->_format_maps[$this->format];
} else {
$config['class'] = HtmlFormatter::class;
}
$formatter = Snowflake::createObject($config);
return $formatter->send($context)->getData();
}
/**
* @param $result
* @return string
* @throws Exception
*/
2020-12-25 15:57:52 +08:00
private function printResult($result): string
2020-08-31 01:27:08 +08:00
{
$result = Help::toString($result);
2020-09-08 11:29:45 +08:00
$string = PHP_EOL . 'Command Result: ' . PHP_EOL . PHP_EOL;
2020-09-11 18:22:32 +08:00
if (empty($result)) {
$string .= 'success!' . PHP_EOL . PHP_EOL;
} else {
$string .= $result . PHP_EOL . PHP_EOL;
}
2020-09-09 18:49:23 +08:00
$string .= 'Command End!' . PHP_EOL . PHP_EOL;
2021-02-22 13:37:02 +08:00
print_r($string);
2020-08-31 01:27:08 +08:00
2021-02-20 13:02:58 +08:00
$event = Snowflake::app()->getEvent();
2020-08-31 01:27:08 +08:00
$event->trigger('CONSOLE_END');
2020-09-09 11:26:25 +08:00
return $result;
2020-08-31 01:27:08 +08:00
}
/**
* @param $sendData
* @param $status
* @return mixed
*/
2020-12-18 16:53:11 +08:00
private function sendData($sendData, $status): mixed
2020-08-31 01:27:08 +08:00
{
2020-09-15 18:30:53 +08:00
$this->response->status($status);
$this->response->header('Content-Type', $this->getContentType());
$this->response->header('Run-Time', $this->getRuntime());
2021-03-09 17:18:03 +08:00
if (!empty($sendData)) {
$this->response->end($this->headers($sendData));
} else {
$this->response->end();
}
2020-12-18 16:53:11 +08:00
$this->response = null;
unset($this->response);
return $sendData;
}
/**
* @param $sendData
* @return string
*/
private function headers($sendData): string
{
2020-09-17 14:27:42 +08:00
if (!empty($this->headers) && is_array($this->headers)) {
2021-03-16 15:05:56 +08:00
var_dump($this->headers);
2020-09-17 14:26:38 +08:00
foreach ($this->headers as $key => $header) {
2021-03-16 15:05:56 +08:00
$this->response->header($key, $header, true);
2020-09-17 14:26:38 +08:00
}
$this->headers = [];
}
2020-12-18 16:53:11 +08:00
return $sendData == null ? '' : $sendData;
2020-08-31 01:27:08 +08:00
}
2020-12-18 16:53:11 +08:00
2020-08-31 01:27:08 +08:00
/**
* @param $url
* @param array $param
* @return int
*/
2020-12-18 16:53:11 +08:00
public function redirect($url, array $param = []): int
2020-08-31 01:27:08 +08:00
{
if (!empty($param)) {
$url .= '?' . http_build_query($param);
}
$url = ltrim($url, '/');
if (!preg_match('/^http/', $url)) {
$url = '/' . $url;
}
return $this->response->redirect($url);
}
/**
* @param null $response
* @return mixed
*/
2020-12-18 16:53:11 +08:00
public static function create($response = null): mixed
2020-08-31 01:27:08 +08:00
{
2020-09-16 20:23:23 +08:00
$ciResponse = Context::setContext('response', new Response());
2020-08-31 01:27:08 +08:00
$ciResponse->response = $response;
$ciResponse->startTime = microtime(true);
$ciResponse->format = self::JSON;
return $ciResponse;
}
2021-03-05 11:57:58 +08:00
/**
* @param string $path
* @param int $offset
* @param int $limit
2021-03-05 13:56:48 +08:00
* @param int $sleep
2021-03-05 13:58:40 +08:00
* @return string
2021-03-05 11:57:58 +08:00
*/
2021-03-05 13:58:40 +08:00
public function sendFile(string $path, $offset = 0, $limit = 1024000, $sleep = 0): string
2021-03-05 11:57:58 +08:00
{
$open = fopen($path, 'r');
$stat = fstat($open);
2021-03-05 13:58:40 +08:00
2021-03-09 19:20:36 +08:00
$this->headers(null);
while ($file = fread($open, $limit)) {
$this->response->write($file);
fseek($open, $offset);
if ($sleep > 0) {
sleep($sleep);
}
if ($offset >= $stat['size']) {
break;
}
$offset += $limit;
}
$this->response->end();
$this->response = null;
2021-03-05 13:58:40 +08:00
return '';
2021-03-05 11:57:58 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @throws Exception
*/
public function sendNotFind()
{
$this->format = static::HTML;
$this->send('', 404);
}
/**
* @return string
*/
2021-03-05 11:57:58 +08:00
#[Pure] public function getRuntime(): string
2020-08-31 01:27:08 +08:00
{
return sprintf('%.5f', microtime(TRUE) - $this->startTime);
}
}