From d5b65f0d89a5094afae44396df19a8efc268fc31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 9 Sep 2021 16:38:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http-server/Constrict/Request.php | 375 +++++++++++++++++++++++++++-- http-server/Constrict/Response.php | 250 ++++++++++++++++++- http-server/Message/Request.php | 44 +++- http-server/Message/Response.php | 12 +- http-server/RequestInterface.php | 123 +++++++++- http-server/ResponseInterface.php | 78 +++++- 6 files changed, 845 insertions(+), 37 deletions(-) diff --git a/http-server/Constrict/Request.php b/http-server/Constrict/Request.php index 295edd23..5174167d 100644 --- a/http-server/Constrict/Request.php +++ b/http-server/Constrict/Request.php @@ -3,16 +3,18 @@ namespace Server\Constrict; use Http\Context\Context; +use Http\IInterface\AuthIdentity; +use JetBrains\PhpStorm\Pure; use Kiri\Kiri; +use Psr\Http\Message\StreamInterface; +use Psr\Http\Message\UriInterface; use Server\Message\Request as RequestMessage; -use Server\RequestInterface; use Server\Message\Response; +use Server\Message\Uploaded; +use Server\RequestInterface; use Server\ResponseInterface; -/** - * @mixin RequestMessage - */ 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 * @return mixed @@ -60,4 +51,358 @@ class Request implements RequestInterface 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__}(); + } } diff --git a/http-server/Constrict/Response.php b/http-server/Constrict/Response.php index f95c2cad..cf19f964 100644 --- a/http-server/Constrict/Response.php +++ b/http-server/Constrict/Response.php @@ -5,14 +5,15 @@ namespace Server\Constrict; use Http\Context\Context; +use Psr\Http\Message\StreamInterface; use Server\Message\Response as Psr7Response; use Server\ResponseInterface; +use Server\SInterface\DownloadInterface; /** * Class Response * @package Server - * @mixin Psr7Response */ class Response implements ResponseInterface { @@ -22,15 +23,6 @@ class Response implements ResponseInterface const HTML = 'html'; const FILE = 'file'; - /** - * @param $name - * @param $args - * @return mixed - */ - public function __call($name, $args) - { - return $this->__call__()->{$name}(...$args); - } /** * @param string $name @@ -49,4 +41,242 @@ class Response implements ResponseInterface { 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); + } } diff --git a/http-server/Message/Request.php b/http-server/Message/Request.php index 69f1b7b5..06fb0c48 100644 --- a/http-server/Message/Request.php +++ b/http-server/Message/Request.php @@ -6,8 +6,8 @@ use BadMethodCallException; use Exception; use Http\IInterface\AuthIdentity; use JetBrains\PhpStorm\Pure; -use Psr\Http\Message\RequestInterface; use Psr\Http\Message\UriInterface; +use Server\RequestInterface; /** @@ -57,7 +57,7 @@ class Request implements RequestInterface /** * @param AuthIdentity $authority */ - public function setAuthority(AuthIdentity $authority) + public function setAuthority(AuthIdentity $authority): void { $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 { @@ -340,4 +340,42 @@ class Request implements RequestInterface $class->uri = $uri; 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; + } } diff --git a/http-server/Message/Response.php b/http-server/Message/Response.php index 733b145e..c068ca9b 100644 --- a/http-server/Message/Response.php +++ b/http-server/Message/Response.php @@ -99,30 +99,30 @@ class Response implements ResponseInterface, \Server\ResponseInterface /** - * @param $value + * @param string $value * @return Response */ - public function withAccessControlAllowHeaders($value): static + public function withAccessControlAllowHeaders(string $value): static { return $this->withHeader('Access-Control-Allow-Headers', $value); } /** - * @param $value + * @param string $value * @return Response */ - public function withAccessControlRequestMethod($value): static + public function withAccessControlRequestMethod(string $value): static { return $this->withHeader('Access-Control-Request-Method', $value); } /** - * @param $value + * @param string $value * @return Response */ - public function withAccessControlAllowOrigin($value): static + public function withAccessControlAllowOrigin(string $value): static { return $this->withHeader('Access-Control-Allow-Origin', $value); } diff --git a/http-server/RequestInterface.php b/http-server/RequestInterface.php index c06d465c..d5de61ed 100644 --- a/http-server/RequestInterface.php +++ b/http-server/RequestInterface.php @@ -3,14 +3,133 @@ namespace Server; - +use Http\IInterface\AuthIdentity; +use JetBrains\PhpStorm\Pure; use Server\Message\Request; +use Server\Message\Uploaded; /** * * @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; } diff --git a/http-server/ResponseInterface.php b/http-server/ResponseInterface.php index f5e47191..56269b7b 100644 --- a/http-server/ResponseInterface.php +++ b/http-server/ResponseInterface.php @@ -4,11 +4,87 @@ namespace Server; use Server\Message\Response; +use Server\SInterface\DownloadInterface; /** * @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; + }