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

184 lines
3.6 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-04-23 11:38:29 +08:00
use Exception;
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;
2021-07-17 02:16:49 +08:00
use HttpServer\IInterface\IFormatter;
2020-08-31 01:27:08 +08:00
use Snowflake\Core\Help;
use Snowflake\Snowflake;
2021-08-04 02:02:21 +08:00
use Swoole\Coroutine;
2020-08-31 01:27:08 +08:00
use Swoole\Http\Response as SResponse;
/**
* 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
{
2021-07-17 02:16:49 +08:00
const JSON = 'json';
const XML = 'xml';
const HTML = 'html';
/** @var ?string */
public ?string $format = null;
/** @var int */
public int $statusCode = 200;
public array $headers = [];
public array $cookies = [];
private float $startTime = 0;
private array $_format_maps = [
self::JSON => JsonFormatter::class,
self::XML => XmlFormatter::class,
self::HTML => HtmlFormatter::class
];
public int $fd = 0;
/**
* @param $format
* @return $this
*/
public function setFormat($format): static
{
2021-08-04 02:02:21 +08:00
$this->__coroutine__call(__METHOD__, func_get_args());
2021-08-04 01:47:26 +08:00
return $this;
2021-07-17 02:16:49 +08:00
}
/**
* @param $content
* @return string
*/
public function toHtml($content): string
{
2021-08-04 02:02:21 +08:00
$this->__coroutine__call(__METHOD__, func_get_args());
2021-08-03 10:50:07 +08:00
return (string)$content;
2021-07-17 02:16:49 +08:00
}
/**
* @param $content
* @return string|bool
*/
public function toJson($content): string|bool
{
2021-08-04 02:02:21 +08:00
$this->__coroutine__call(__METHOD__, func_get_args());
2021-08-03 10:50:07 +08:00
return json_encode($content, JSON_UNESCAPED_UNICODE);
2021-07-17 02:16:49 +08:00
}
/**
* @param $content
* @return mixed
*/
public function toXml($content): mixed
{
2021-08-04 02:02:21 +08:00
$this->__coroutine__call(__METHOD__, func_get_args());
2021-08-03 10:50:07 +08:00
return $content;
2021-07-17 02:16:49 +08:00
}
/**
* @param $key
* @param $value
* @return Response
*/
public function addHeader($key, $value): static
{
2021-08-04 02:02:21 +08:00
$this->__coroutine__call(__METHOD__, func_get_args());
2021-07-17 02:16:49 +08:00
return $this;
}
2021-08-04 02:02:21 +08:00
/**
* @param $name
* @param $value
*/
private function __coroutine__call($name, $value)
{
/** @var \HttpServer\Http\CoroutineResponse $handler */
$handler = Context::getContext(CoroutineResponse::class);
call_user_func([$handler, $name], ...$value);
}
2021-07-17 02:16:49 +08:00
/**
* @param $name
* @param null $value
* @param null $expires
* @param null $path
* @param null $domain
* @param null $secure
* @param null $httponly
* @param null $samesite
* @param null $priority
* @return Response
*/
public function addCookie($name, $value = null, $expires = null, $path = null, $domain = null, $secure = null, $httponly = null, $samesite = null, $priority = null): static
{
2021-08-04 02:02:21 +08:00
$this->__coroutine__call(__METHOD__, func_get_args());
2021-07-17 02:16:49 +08:00
return $this;
}
2021-07-03 13:05:17 +08:00
2021-08-04 01:47:26 +08:00
/**
* @param $statusCode
*/
public function setStatusCode($statusCode)
{
2021-08-04 02:02:21 +08:00
$this->__coroutine__call(__METHOD__, func_get_args());
2021-08-04 01:47:26 +08:00
}
2021-07-06 18:07:41 +08:00
/**
2021-07-07 17:51:02 +08:00
* @param mixed $context
2021-07-06 18:07:41 +08:00
* @param int $statusCode
* @throws Exception
*/
2021-08-04 02:02:21 +08:00
public function send(mixed $context, SResponse $response): void
2021-07-17 02:16:49 +08:00
{
2021-08-04 02:02:21 +08:00
$this->__coroutine__call(__METHOD__, func_get_args());
2021-07-17 02:16:49 +08:00
}
/**
* @param $url
* @param array $param
* @return int
*/
2021-08-04 02:02:21 +08:00
public function redirect($url, array $param = []): void
2021-07-17 02:16:49 +08:00
{
2021-08-04 02:02:21 +08:00
$this->__coroutine__call(__METHOD__, func_get_args());
}
2021-07-17 02:16:49 +08:00
/**
* @param string $path
* @param int $offset
* @param int $limit
* @param int $sleep
* @return string
*/
2021-08-04 02:02:21 +08:00
public function sendFile(string $path, int $offset = 0, int $limit = 1024000, int $sleep = 0): void
2021-07-17 02:16:49 +08:00
{
2021-08-04 02:02:21 +08:00
$this->__coroutine__call(__METHOD__, func_get_args());
}
2020-08-31 01:27:08 +08:00
}