2021-08-04 02:43:28 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
2021-08-04 14:05:00 +08:00
|
|
|
namespace Server\Constrict;
|
2021-08-04 02:43:28 +08:00
|
|
|
|
|
|
|
|
|
2021-09-18 16:54:39 +08:00
|
|
|
use Http\Handler\Context;
|
2021-09-21 02:46:21 +08:00
|
|
|
use Http\Message\ContentType;
|
2021-09-09 17:01:52 +08:00
|
|
|
use JetBrains\PhpStorm\Pure;
|
2021-09-21 02:46:21 +08:00
|
|
|
use Kiri\Abstracts\Config;
|
2021-09-22 11:36:29 +08:00
|
|
|
use Kiri\Exception\ConfigException;
|
2021-09-09 17:25:52 +08:00
|
|
|
use Kiri\Kiri;
|
2021-09-09 16:38:54 +08:00
|
|
|
use Psr\Http\Message\StreamInterface;
|
2021-09-16 14:53:36 +08:00
|
|
|
use Http\Message\ServerRequest as RequestMessage;
|
|
|
|
|
use Http\Message\Response as Psr7Response;
|
2021-09-09 17:25:52 +08:00
|
|
|
use Server\ServerManager;
|
2021-09-24 02:23:24 +08:00
|
|
|
use Server\SInterface\OnDownloadInterface;
|
2021-08-04 02:43:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Response
|
|
|
|
|
* @package Server
|
|
|
|
|
*/
|
2021-08-04 17:13:26 +08:00
|
|
|
class Response implements ResponseInterface
|
2021-08-04 02:43:28 +08:00
|
|
|
{
|
|
|
|
|
|
2021-08-04 10:16:03 +08:00
|
|
|
const JSON = 'json';
|
|
|
|
|
const XML = 'xml';
|
|
|
|
|
const HTML = 'html';
|
2021-08-10 16:40:01 +08:00
|
|
|
const FILE = 'file';
|
2021-08-04 10:16:03 +08:00
|
|
|
|
2021-09-06 17:46:03 +08:00
|
|
|
|
2021-09-21 02:50:38 +08:00
|
|
|
/**
|
2021-09-22 11:36:29 +08:00
|
|
|
* @throws ConfigException
|
2021-09-21 02:50:38 +08:00
|
|
|
*/
|
2021-09-21 02:46:21 +08:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
2021-09-21 02:50:38 +08:00
|
|
|
$contentType = Config::get('response',[
|
|
|
|
|
'format' => ContentType::JSON,
|
|
|
|
|
'charset' => 'utf-8'
|
|
|
|
|
]);
|
2021-09-21 02:51:09 +08:00
|
|
|
$this->withContentType($contentType['format'] ?? ContentType::JSON)
|
|
|
|
|
->withCharset($contentType['charset'] ?? 'utf-8');
|
2021-09-21 02:46:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-06 17:46:03 +08:00
|
|
|
* @param string $name
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function __get(string $name)
|
|
|
|
|
{
|
|
|
|
|
return $this->__call__()->{$name};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Psr7Response
|
|
|
|
|
*/
|
|
|
|
|
public function __call__(): Psr7Response
|
|
|
|
|
{
|
2021-09-06 17:59:12 +08:00
|
|
|
return Context::getContext(ResponseInterface::class, new Psr7Response());
|
2021-09-06 17:46:03 +08:00
|
|
|
}
|
2021-09-09 16:38:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getProtocolVersion(): string
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $version
|
2021-09-10 11:15:04 +08:00
|
|
|
* @return ResponseInterface|Psr7Response
|
2021-09-09 16:38:54 +08:00
|
|
|
*/
|
2021-09-10 11:15:04 +08:00
|
|
|
public function withProtocolVersion($version): ResponseInterface|Psr7Response
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($version);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getHeaders(): array
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function hasHeader($name): bool
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($name);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getHeader($name): string
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($name);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getHeaderLine($name): string
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($name);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string|string[] $value
|
2021-09-10 11:15:04 +08:00
|
|
|
* @return ResponseInterface|Psr7Response
|
2021-09-09 16:38:54 +08:00
|
|
|
*/
|
2021-09-10 11:15:04 +08:00
|
|
|
public function withHeader($name, $value): ResponseInterface|Psr7Response
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($name, $value);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string|string[] $value
|
2021-09-10 11:15:04 +08:00
|
|
|
* @return ResponseInterface|Psr7Response
|
2021-09-09 16:38:54 +08:00
|
|
|
*/
|
2021-09-10 11:15:04 +08:00
|
|
|
public function withAddedHeader($name, $value): ResponseInterface|Psr7Response
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($name, $value);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
2021-09-10 11:15:04 +08:00
|
|
|
* @return ResponseInterface|Psr7Response
|
2021-09-09 16:38:54 +08:00
|
|
|
*/
|
2021-09-10 11:15:04 +08:00
|
|
|
public function withoutHeader($name): ResponseInterface|Psr7Response
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($name);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return StreamInterface
|
|
|
|
|
*/
|
|
|
|
|
public function getBody(): StreamInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param StreamInterface $body
|
2021-09-10 11:15:04 +08:00
|
|
|
* @return ResponseInterface|Psr7Response
|
2021-09-09 16:38:54 +08:00
|
|
|
*/
|
2021-09-10 11:15:04 +08:00
|
|
|
public function withBody(StreamInterface $body): ResponseInterface|Psr7Response
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($body);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getStatusCode(): int
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $code
|
|
|
|
|
* @param string $reasonPhrase
|
2021-09-10 11:15:04 +08:00
|
|
|
* @return ResponseInterface|Psr7Response
|
2021-09-09 16:38:54 +08:00
|
|
|
*/
|
2021-09-10 11:15:04 +08:00
|
|
|
public function withStatus($code, $reasonPhrase = ''): ResponseInterface|Psr7Response
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($code, $reasonPhrase);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getReasonPhrase(): string
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
2021-09-24 02:23:24 +08:00
|
|
|
* @return OnDownloadInterface
|
2021-09-09 16:38:54 +08:00
|
|
|
*/
|
2021-09-24 02:23:24 +08:00
|
|
|
public function file(string $path): OnDownloadInterface
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($path);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $responseData
|
|
|
|
|
* @return string|array|bool|int|null
|
|
|
|
|
*/
|
|
|
|
|
public function _toArray($responseData): string|array|null|bool|int
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($responseData);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function xml($data): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($data);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function html($data): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($data);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function json($data): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($data);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getContentType(): string
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function hasContentType(): bool
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
|
|
|
|
public function withContentType(string $type): ResponseInterface
|
|
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($type);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-09 17:03:29 +08:00
|
|
|
* @param string|null $value
|
2021-09-09 16:38:54 +08:00
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
2021-09-09 17:03:29 +08:00
|
|
|
public function withAccessControlAllowOrigin(?string $value): ResponseInterface
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($value);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-09 17:03:29 +08:00
|
|
|
* @param string|null $value
|
2021-09-09 16:38:54 +08:00
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
2021-09-09 17:03:29 +08:00
|
|
|
public function withAccessControlRequestMethod(?string $value): ResponseInterface
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($value);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-09 17:03:29 +08:00
|
|
|
* @param string|null $value
|
2021-09-09 16:38:54 +08:00
|
|
|
* @return ResponseInterface
|
|
|
|
|
*/
|
2021-09-09 17:03:29 +08:00
|
|
|
public function withAccessControlAllowHeaders(?string $value): ResponseInterface
|
2021-09-09 16:38:54 +08:00
|
|
|
{
|
2021-09-09 16:58:15 +08:00
|
|
|
return $this->__call__()->{__FUNCTION__}($value);
|
2021-09-09 16:38:54 +08:00
|
|
|
}
|
2021-09-09 17:01:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
2021-09-09 17:08:51 +08:00
|
|
|
#[Pure] public function getAccessControlAllowOrigin(): ?string
|
2021-09-09 17:01:52 +08:00
|
|
|
{
|
|
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
2021-09-09 17:08:51 +08:00
|
|
|
#[Pure] public function getAccessControlAllowHeaders(): ?string
|
2021-09-09 17:01:52 +08:00
|
|
|
{
|
|
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
2021-09-09 17:08:51 +08:00
|
|
|
#[Pure] public function getAccessControlRequestMethod(): ?string
|
2021-09-09 17:01:52 +08:00
|
|
|
{
|
|
|
|
|
return $this->__call__()->{__FUNCTION__}();
|
|
|
|
|
}
|
2021-09-09 17:17:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getClientId(): int
|
|
|
|
|
{
|
2021-09-09 17:25:52 +08:00
|
|
|
if (!Context::hasContext('client.id.property')) {
|
|
|
|
|
$request = Context::getContext(RequestInterface::class, new RequestMessage());
|
|
|
|
|
return Context::setContext('client.id.property', $request->getClientId());
|
|
|
|
|
}
|
|
|
|
|
return (int)Context::getContext('client.id.property');
|
2021-09-09 17:17:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getClientInfo(): array
|
|
|
|
|
{
|
2021-09-09 17:25:52 +08:00
|
|
|
if (!Context::hasContext('client.info.property')) {
|
|
|
|
|
$request = Context::getContext(RequestInterface::class, new RequestMessage());
|
|
|
|
|
|
|
|
|
|
$server = Kiri::getDi()->get(ServerManager::class)->getServer();
|
|
|
|
|
|
|
|
|
|
$clientInfo = $server->getClientInfo($request->getClientId());
|
|
|
|
|
|
|
|
|
|
return Context::setContext('client.info.property', $clientInfo);
|
|
|
|
|
}
|
|
|
|
|
return Context::getContext('client.info.property');
|
2021-09-09 17:17:44 +08:00
|
|
|
}
|
2021-09-09 17:25:52 +08:00
|
|
|
|
2021-08-04 02:43:28 +08:00
|
|
|
}
|