This commit is contained in:
2021-09-09 16:38:54 +08:00
parent aacda0bef9
commit d5b65f0d89
6 changed files with 845 additions and 37 deletions
+360 -15
View File
@@ -3,16 +3,18 @@
namespace Server\Constrict; namespace Server\Constrict;
use Http\Context\Context; use Http\Context\Context;
use Http\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure;
use Kiri\Kiri; use Kiri\Kiri;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use Server\Message\Request as RequestMessage; use Server\Message\Request as RequestMessage;
use Server\RequestInterface;
use Server\Message\Response; use Server\Message\Response;
use Server\Message\Uploaded;
use Server\RequestInterface;
use Server\ResponseInterface; use Server\ResponseInterface;
/**
* @mixin RequestMessage
*/
class Request implements RequestInterface class Request implements RequestInterface
{ {
@@ -26,17 +28,6 @@ class Request implements RequestInterface
} }
/**
* @param $name
* @param $args
* @return mixed
*/
public function __call($name, $args)
{
return $this->__call__()->{$name}(...$args);
}
/** /**
* @param $name * @param $name
* @return mixed * @return mixed
@@ -60,4 +51,358 @@ class Request implements RequestInterface
return [Kiri::getDi()->get(Request::class), $response]; return [Kiri::getDi()->get(Request::class), $response];
} }
/**
* @return string
*/
public function getProtocolVersion(): string
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param string $version
* @return Request
*/
public function withProtocolVersion($version): RequestInterface
{
return $this->__call__()->{__METHOD__}($version);
}
/**
* @return \string[][]
*/
public function getHeaders(): array
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param string $name
* @return bool
*/
public function hasHeader($name): bool
{
return $this->__call__()->{__METHOD__}($name);
}
/**
* @param string $name
* @return string[]
*/
public function getHeader($name): array
{
return $this->__call__()->{__METHOD__}($name);
}
/**
* @param string $name
* @return string
*/
public function getHeaderLine($name): string
{
return $this->__call__()->{__METHOD__}($name);
}
/**
* @param string $name
* @param string|string[] $value
* @return Request
*/
public function withHeader($name, $value): RequestInterface
{
return $this->__call__()->{__METHOD__}($name, $value);
}
/**
* @param string $name
* @param string|string[] $value
* @return Request
*/
public function withAddedHeader($name, $value): RequestInterface
{
return $this->__call__()->{__METHOD__}($name, $value);
}
/**
* @param string $name
* @return Request
*/
public function withoutHeader($name): RequestInterface
{
return $this->__call__()->{__METHOD__}($name);
}
/**
* @return StreamInterface
*/
public function getBody(): StreamInterface
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param StreamInterface $body
* @return Request
*/
public function withBody(StreamInterface $body): RequestInterface
{
return $this->__call__()->{__METHOD__}($body);
}
/**
* @return string
*/
public function getRequestTarget(): string
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param mixed $requestTarget
* @return Request
*/
public function withRequestTarget($requestTarget): RequestInterface
{
return $this->__call__()->{__METHOD__}($requestTarget);
}
/**
* @return string
*/
public function getMethod(): string
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param string $method
* @return bool
*/
public function isMethod(string $method): bool
{
return $this->__call__()->{__METHOD__}($method);
}
/**
* @param string $method
* @return Request
*/
public function withMethod($method): RequestInterface
{
return $this->__call__()->{__METHOD__}($method);
}
/**
* @return UriInterface
*/
public function getUri(): UriInterface
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param UriInterface $uri
* @param false $preserveHost
* @return Request
*/
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
{
return $this->__call__()->{__METHOD__}($uri, $preserveHost);
}
/**
* @param string $name
* @return Uploaded|null
*/
public function file(string $name): ?Uploaded
{
return $this->__call__()->{__METHOD__}($name);
}
/**
* @return array
*/
public function all(): array
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param string $name
* @param bool|int|string|null $default
* @return mixed
*/
public function query(string $name, bool|int|string|null $default = null): mixed
{
return $this->__call__()->{__METHOD__}($name, $default);
}
/**
* @param string $name
* @param int|bool|array|string|null $default
* @return mixed
*/
public function post(string $name, int|bool|array|string|null $default = null): mixed
{
return $this->__call__()->{__METHOD__}($name, $default);
}
/**
* @param string $name
* @param bool $required
* @return int|null
*/
public function int(string $name, bool $required = false): ?int
{
return $this->__call__()->{__METHOD__}($name, $required);
}
/**
* @param string $name
* @param bool $required
* @return float|null
*/
public function float(string $name, bool $required = false): ?float
{
return $this->__call__()->{__METHOD__}($name, $required);
}
/**
* @param string $name
* @param bool $required
* @return string|null
*/
public function date(string $name, bool $required = false): ?string
{
return $this->__call__()->{__METHOD__}($name, $required);
}
/**
* @param string $name
* @param bool $required
* @return int|null
*/
public function timestamp(string $name, bool $required = false): ?int
{
return $this->__call__()->{__METHOD__}($name, $required);
}
/**
* @param string $name
* @param bool $required
* @return string|null
*/
public function string(string $name, bool $required = false): ?string
{
return $this->__call__()->{__METHOD__}($name, $required);
}
/**
* @param string $name
* @param array $default
* @return array|null
*/
public function array(string $name, array $default = []): ?array
{
return $this->__call__()->{__METHOD__}($name, $default);
}
/**
* @return array|null
*/
public function gets(): ?array
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param string $field
* @param string $sizeField
* @param int $max
* @return float|int
*/
public function offset(string $field = 'page', string $sizeField = 'size', int $max = 100): float|int
{
return $this->__call__()->{__METHOD__}($field, $sizeField, $max);
}
/**
* @param string $field
* @param int $max
* @return int
*/
public function size(string $field = 'size', int $max = 100): int
{
return $this->__call__()->{__METHOD__}($field, $max);
}
/**
* @param $name
* @param null $default
* @return mixed
*/
public function input($name, $default = null): mixed
{
return $this->__call__()->{__METHOD__}($name, $default);
}
/**
* @return float
*/
#[Pure] public function getStartTime(): float
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param AuthIdentity $authority
*/
public function setAuthority(AuthIdentity $authority): void
{
$this->__call__()->{__METHOD__}($authority);
}
/**
* @return int
*/
public function getClientId(): int
{
return $this->__call__()->{__METHOD__}();
}
} }
+240 -10
View File
@@ -5,14 +5,15 @@ namespace Server\Constrict;
use Http\Context\Context; use Http\Context\Context;
use Psr\Http\Message\StreamInterface;
use Server\Message\Response as Psr7Response; use Server\Message\Response as Psr7Response;
use Server\ResponseInterface; use Server\ResponseInterface;
use Server\SInterface\DownloadInterface;
/** /**
* Class Response * Class Response
* @package Server * @package Server
* @mixin Psr7Response
*/ */
class Response implements ResponseInterface class Response implements ResponseInterface
{ {
@@ -22,15 +23,6 @@ class Response implements ResponseInterface
const HTML = 'html'; const HTML = 'html';
const FILE = 'file'; const FILE = 'file';
/**
* @param $name
* @param $args
* @return mixed
*/
public function __call($name, $args)
{
return $this->__call__()->{$name}(...$args);
}
/** /**
* @param string $name * @param string $name
@@ -49,4 +41,242 @@ class Response implements ResponseInterface
{ {
return Context::getContext(ResponseInterface::class, new Psr7Response()); return Context::getContext(ResponseInterface::class, new Psr7Response());
} }
/**
* @return string
*/
public function getProtocolVersion(): string
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param string $version
* @return ResponseInterface
*/
public function withProtocolVersion($version): ResponseInterface
{
return $this->__call__()->{__METHOD__}($version);
}
/**
* @return array
*/
public function getHeaders(): array
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param string $name
* @return bool
*/
public function hasHeader($name): bool
{
return $this->__call__()->{__METHOD__}($name);
}
/**
* @param string $name
* @return string
*/
public function getHeader($name): string
{
return $this->__call__()->{__METHOD__}($name);
}
/**
* @param string $name
* @return string
*/
public function getHeaderLine($name): string
{
return $this->__call__()->{__METHOD__}($name);
}
/**
* @param string $name
* @param string|string[] $value
* @return ResponseInterface
*/
public function withHeader($name, $value): ResponseInterface
{
return $this->__call__()->{__METHOD__}($name, $value);
}
/**
* @param string $name
* @param string|string[] $value
* @return ResponseInterface
*/
public function withAddedHeader($name, $value): ResponseInterface
{
return $this->__call__()->{__METHOD__}($name, $value);
}
/**
* @param string $name
* @return ResponseInterface
*/
public function withoutHeader($name): ResponseInterface
{
return $this->__call__()->{__METHOD__}($name);
}
/**
* @return StreamInterface
*/
public function getBody(): StreamInterface
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param StreamInterface $body
* @return ResponseInterface
*/
public function withBody(StreamInterface $body): ResponseInterface
{
return $this->__call__()->{__METHOD__}($body);
}
/**
* @return int
*/
public function getStatusCode(): int
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param int $code
* @param string $reasonPhrase
* @return ResponseInterface
*/
public function withStatus($code, $reasonPhrase = ''): ResponseInterface
{
return $this->__call__()->{__METHOD__}($code, $reasonPhrase);
}
/**
* @return string
*/
public function getReasonPhrase(): string
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param string $path
* @return DownloadInterface
*/
public function file(string $path): DownloadInterface
{
return $this->__call__()->{__METHOD__}($path);
}
/**
* @param $responseData
* @return string|array|bool|int|null
*/
public function _toArray($responseData): string|array|null|bool|int
{
return $this->__call__()->{__METHOD__}($responseData);
}
/**
* @param $data
* @return ResponseInterface
*/
public function xml($data): ResponseInterface
{
return $this->__call__()->{__METHOD__}($data);
}
/**
* @param $data
* @return ResponseInterface
*/
public function html($data): ResponseInterface
{
return $this->__call__()->{__METHOD__}($data);
}
/**
* @param $data
* @return ResponseInterface
*/
public function json($data): ResponseInterface
{
return $this->__call__()->{__METHOD__}($data);
}
/**
* @return string
*/
public function getContentType(): string
{
return $this->__call__()->{__METHOD__}();
}
/**
* @return bool
*/
public function hasContentType(): bool
{
return $this->__call__()->{__METHOD__}();
}
/**
* @param string $type
* @return ResponseInterface
*/
public function withContentType(string $type): ResponseInterface
{
return $this->__call__()->{__METHOD__}($type);
}
/**
* @param string $data
* @return ResponseInterface
*/
public function withAccessControlAllowOrigin(string $data): ResponseInterface
{
return $this->__call__()->{__METHOD__}($data);
}
/**
* @param string $data
* @return ResponseInterface
*/
public function withAccessControlRequestMethod(string $data): ResponseInterface
{
return $this->__call__()->{__METHOD__}($data);
}
/**
* @param string $data
* @return ResponseInterface
*/
public function withAccessControlAllowHeaders(string $data): ResponseInterface
{
return $this->__call__()->{__METHOD__}($data);
}
} }
+41 -3
View File
@@ -6,8 +6,8 @@ use BadMethodCallException;
use Exception; use Exception;
use Http\IInterface\AuthIdentity; use Http\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Server\RequestInterface;
/** /**
@@ -57,7 +57,7 @@ class Request implements RequestInterface
/** /**
* @param AuthIdentity $authority * @param AuthIdentity $authority
*/ */
public function setAuthority(AuthIdentity $authority) public function setAuthority(AuthIdentity $authority): void
{ {
$this->authority = $authority; $this->authority = $authority;
} }
@@ -321,7 +321,7 @@ class Request implements RequestInterface
/** /**
* @return \Server\Message\Uri|\Psr\Http\Message\UriInterface * @return Uri|UriInterface
*/ */
public function getUri(): Uri|UriInterface public function getUri(): Uri|UriInterface
{ {
@@ -340,4 +340,42 @@ class Request implements RequestInterface
$class->uri = $uri; $class->uri = $uri;
return $class; return $class;
} }
/**
* @param string $name
* @param bool $required
* @return string|null
* @throws Exception
*/
public function date(string $name, bool $required = false): ?string
{
$param = $this->post($name, null);
if (empty($param)) {
if ($required) {
throw new Exception('Required ' . $name . ' is must.');
}
return $param;
}
return $param;
}
/**
* @param string $name
* @param bool $required
* @return int|null
* @throws Exception
*/
public function timestamp(string $name, bool $required = false): ?int
{
$param = $this->post($name, null);
if (empty($param)) {
if ($required) {
throw new Exception('Required ' . $name . ' is must.');
}
return $param;
}
return (int)$param;
}
} }
+6 -6
View File
@@ -99,30 +99,30 @@ class Response implements ResponseInterface, \Server\ResponseInterface
/** /**
* @param $value * @param string $value
* @return Response * @return Response
*/ */
public function withAccessControlAllowHeaders($value): static public function withAccessControlAllowHeaders(string $value): static
{ {
return $this->withHeader('Access-Control-Allow-Headers', $value); return $this->withHeader('Access-Control-Allow-Headers', $value);
} }
/** /**
* @param $value * @param string $value
* @return Response * @return Response
*/ */
public function withAccessControlRequestMethod($value): static public function withAccessControlRequestMethod(string $value): static
{ {
return $this->withHeader('Access-Control-Request-Method', $value); return $this->withHeader('Access-Control-Request-Method', $value);
} }
/** /**
* @param $value * @param string $value
* @return Response * @return Response
*/ */
public function withAccessControlAllowOrigin($value): static public function withAccessControlAllowOrigin(string $value): static
{ {
return $this->withHeader('Access-Control-Allow-Origin', $value); return $this->withHeader('Access-Control-Allow-Origin', $value);
} }
+121 -2
View File
@@ -3,14 +3,133 @@
namespace Server; namespace Server;
use Http\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure;
use Server\Message\Request; use Server\Message\Request;
use Server\Message\Uploaded;
/** /**
* *
* @mixin Request * @mixin Request
*/ */
interface RequestInterface interface RequestInterface extends \Psr\Http\Message\RequestInterface
{ {
/**
* @param string $method
* @return bool
*/
public function isMethod(string $method): bool;
/**
* @param string $name
* @return Uploaded|null
*/
public function file(string $name): ?Uploaded;
/**
* @return array
*/
public function all(): array;
/**
* @param string $name
* @param int|string|bool|null $default
* @return mixed
*/
public function query(string $name, int|string|bool|null $default = null): mixed;
/**
* @param string $name
* @param int|string|bool|array|null $default
* @return mixed
*/
public function post(string $name, int|string|bool|null|array $default = null): mixed;
/**
* @param string $name
* @param bool $required
* @return int|null
*/
public function int(string $name, bool $required = false): ?int;
/**
* @param string $name
* @param bool $required
* @return float|null
*/
public function float(string $name, bool $required = false): ?float;
/**
* @param string $name
* @param bool $required
* @return string|null
*/
public function date(string $name, bool $required = false): ?string;
/**
* @param string $name
* @param bool $required
* @return int|null
*/
public function timestamp(string $name, bool $required = false): ?int;
/**
* @param string $name
* @param bool $required
* @return string|null
*/
public function string(string $name, bool $required = false): ?string;
/**
* @param string $name
* @param array $default
* @return array|null
*/
public function array(string $name, array $default = []): ?array;
/**
* @return array|null
*/
public function gets(): ?array;
/**
* @param string $field
* @param string $sizeField
* @param int $max
* @return float|int
*/
public function offset(string $field = 'page', string $sizeField = 'size', int $max = 100): float|int;
/**
* @param string $field
* @param int $max
* @return int
*/
public function size(string $field = 'size', int $max = 100): int;
/**
* @param $name
* @param null $default
* @return mixed
*/
public function input($name, $default = null): mixed;
/**
* @return float
*/
#[Pure] public function getStartTime(): float;
/**
* @param AuthIdentity $authority
*/
public function setAuthority(AuthIdentity $authority): void;
/**
* @return int
*/
public function getClientId(): int;
} }
+77 -1
View File
@@ -4,11 +4,87 @@ namespace Server;
use Server\Message\Response; use Server\Message\Response;
use Server\SInterface\DownloadInterface;
/** /**
* @mixin Response * @mixin Response
*/ */
interface ResponseInterface interface ResponseInterface extends \Psr\Http\Message\ResponseInterface
{ {
/**
* @param string $path
* @return DownloadInterface
*/
public function file(string $path): DownloadInterface;
/**
* @param $responseData
* @return string|array|bool|int|null
*/
public function _toArray($responseData): string|array|null|bool|int;
/**
* @param $data
* @return ResponseInterface
*/
public function xml($data): ResponseInterface;
/**
* @param $data
* @return ResponseInterface
*/
public function html($data): ResponseInterface;
/**
* @param $data
* @return ResponseInterface
*/
public function json($data): ResponseInterface;
/**
* @return string
*/
public function getContentType(): string;
/**
* @return bool
*/
public function hasContentType(): bool;
/**
* @param string $type
* @return ResponseInterface
*/
public function withContentType(string $type): ResponseInterface;
/**
* @param string $value
* @return ResponseInterface
*/
public function withAccessControlAllowOrigin(string $value): ResponseInterface;
/**
* @param string $value
* @return ResponseInterface
*/
public function withAccessControlRequestMethod(string $value): ResponseInterface;
/**
* @param string $value
* @return ResponseInterface
*/
public function withAccessControlAllowHeaders(string $value): ResponseInterface;
} }