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;
|
|
|
|
|
|
|
|
|
|
use HttpServer\Application;
|
|
|
|
|
use HttpServer\Http\Formatter\HtmlFormatter;
|
|
|
|
|
use HttpServer\Http\Formatter\JsonFormatter;
|
|
|
|
|
use HttpServer\Http\Formatter\XmlFormatter;
|
|
|
|
|
use Exception;
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
class Response extends Application
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
public function setFormat($format)
|
|
|
|
|
{
|
|
|
|
|
$this->format = $format;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清理无用数据
|
|
|
|
|
*/
|
|
|
|
|
public function clear()
|
|
|
|
|
{
|
|
|
|
|
$this->fd = 0;
|
|
|
|
|
$this->isWebSocket = false;
|
|
|
|
|
$this->format = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getContentType()
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
public function toHtml($content)
|
|
|
|
|
{
|
|
|
|
|
$this->format = self::HTML;
|
|
|
|
|
return $content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $content
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function toJson($content)
|
|
|
|
|
{
|
|
|
|
|
$this->format = self::JSON;
|
|
|
|
|
return $content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $content
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function toXml($content)
|
|
|
|
|
{
|
|
|
|
|
$this->format = self::XML;
|
|
|
|
|
return $content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function sender()
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
public function addHeader($key, $value)
|
|
|
|
|
{
|
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
|
|
|
|
|
*/
|
|
|
|
|
private function isClient()
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
public function send($context = '', $statusCode = 200, $response = null)
|
|
|
|
|
{
|
|
|
|
|
$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 {
|
|
|
|
|
return $this->printResult($sendData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $context
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function parseData($context)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
private function printResult($result)
|
|
|
|
|
{
|
|
|
|
|
$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;
|
2020-08-31 01:27:08 +08:00
|
|
|
echo $string;
|
|
|
|
|
|
2020-09-03 11:39:20 +08:00
|
|
|
$event = Snowflake::app()->event;
|
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-09-15 18:30:53 +08:00
|
|
|
private function sendData($sendData, $status)
|
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());
|
2020-09-17 14:27:42 +08:00
|
|
|
if (!empty($this->headers) && is_array($this->headers)) {
|
2020-09-17 14:26:38 +08:00
|
|
|
foreach ($this->headers as $key => $header) {
|
|
|
|
|
$this->response->header($key, $header);
|
|
|
|
|
}
|
|
|
|
|
$this->headers = [];
|
|
|
|
|
}
|
2020-09-16 20:33:25 +08:00
|
|
|
if (empty($sendData)) {
|
|
|
|
|
$sendData = '';
|
|
|
|
|
}
|
2020-09-15 18:30:53 +08:00
|
|
|
$this->response->end($sendData);
|
2020-09-16 20:43:51 +08:00
|
|
|
$this->response = null;
|
2020-09-17 13:56:57 +08:00
|
|
|
unset($this->response);
|
2020-09-09 11:26:25 +08:00
|
|
|
return $sendData;
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $url
|
|
|
|
|
* @param array $param
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function redirect($url, array $param = [])
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
public static function create($response = null)
|
|
|
|
|
{
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function sendNotFind()
|
|
|
|
|
{
|
|
|
|
|
$this->format = static::HTML;
|
|
|
|
|
$this->send('', 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getRuntime()
|
|
|
|
|
{
|
|
|
|
|
return sprintf('%.5f', microtime(TRUE) - $this->startTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|