diff --git a/http-message/Request.php b/http-message/Request.php index ba4c4e99..a2b5eaf1 100644 --- a/http-message/Request.php +++ b/http-message/Request.php @@ -13,7 +13,7 @@ use Psr\Http\Message\UriInterface; abstract class Request implements RequestInterface { - use Message; + use Message; /** @@ -28,63 +28,72 @@ abstract class Request implements RequestInterface protected string $method; - - /** - * @return string - */ - public function getRequestTarget(): string - { - throw new BadMethodCallException('Not Accomplish Method.'); - } + /** + * @return string + */ + public function getRequestTarget(): string + { + throw new BadMethodCallException('Not Accomplish Method.'); + } - /** - * @param mixed $requestTarget - * @return static - */ - public function withRequestTarget($requestTarget): static - { - throw new BadMethodCallException('Not Accomplish Method.'); - } + /** + * @param mixed $requestTarget + * @return static + */ + public function withRequestTarget($requestTarget): static + { + throw new BadMethodCallException('Not Accomplish Method.'); + } - /** - * @return string - */ - public function getMethod(): string - { - return $this->method; - } + /** + * @return string + */ + public function getMethod(): string + { + return $this->method; + } - /** - * @param string $method - * @return RequestInterface - */ - public function withMethod($method): RequestInterface - { - $this->method = $method; - return $this; - } + /** + * @param string $method + * @return RequestInterface + */ + public function withMethod($method): RequestInterface + { + $this->method = $method; + return $this; + } - /** - * @return UriInterface - */ - public function getUri(): UriInterface - { - return $this->uriInterface; - } + /** + * @param string $method + * @return bool + */ + public function isMethod(string $method): bool + { + return $this->method == $method; + } - /** - * @param UriInterface $uri - * @param false $preserveHost - * @return $this|Request - */ - public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface - { - $this->uriInterface = $uri; - return $this; - } + /** + * @return UriInterface + */ + public function getUri(): UriInterface + { + return $this->uriInterface; + } + + + /** + * @param UriInterface $uri + * @param false $preserveHost + * @return $this|Request + */ + public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface + { + $this->uriInterface = $uri; + return $this; + } } diff --git a/http-message/ServerRequest.php b/http-message/ServerRequest.php index 73fe4dd6..7e8f04bb 100644 --- a/http-message/ServerRequest.php +++ b/http-message/ServerRequest.php @@ -3,12 +3,8 @@ namespace Protocol\Message; use Http\Context\Context; -use Kiri\Core\Xml; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamInterface; -use Psr\Http\Message\UploadedFileInterface; /** @@ -98,42 +94,6 @@ class ServerRequest extends Request implements ServerRequestInterface } - /** - * @param string $name - * @param mixed|null $default - * @return mixed - */ - public function post(string $name, mixed $default = null): mixed - { - return $this->parsedBody[$name] ?? $default; - } - - - /** - * @param string $name - * @param string|null $default - * @return string|null - */ - public function query(string $name, ?string $default = null): ?string - { - return $this->queryParams[$name] ?? $default; - } - - - /** - * @param string $name - * @return \Psr\Http\Message\UploadedFileInterface|null - */ - public function file(string $name): ?UploadedFileInterface - { - if (isset($this->parsedBody[$name])) { - $files = $this->parsedBody[$name]; - return new Uploaded($files['tmp_name'], $files['name'], $files['type'], $files['size'], $files['error']); - } - return null; - } - - /** * @return null|array */ diff --git a/http-server/Constrict/Request.php b/http-server/Constrict/Request.php index d60b2791..59c13514 100644 --- a/http-server/Constrict/Request.php +++ b/http-server/Constrict/Request.php @@ -5,12 +5,17 @@ namespace Server\Constrict; use Http\Context\Context; use Http\IInterface\AuthIdentity; use JetBrains\PhpStorm\Pure; +use Kiri\Core\Xml; use Kiri\Kiri; +use Protocol\Message\ServerRequest; +use Protocol\Message\Stream; +use Protocol\Message\Uri; +use Protocol\Message\Uploaded; use Psr\Http\Message\StreamInterface; +use Psr\Http\Message\UploadedFileInterface; use Psr\Http\Message\UriInterface; use Server\Message\Request as RequestMessage; use Server\Message\Response; -use Server\Message\Uploaded; use Server\RequestInterface; use Server\ResponseInterface; @@ -19,417 +24,479 @@ class Request implements RequestInterface { - /** - * @return RequestMessage - */ - private function __call__(): RequestMessage - { - return Context::getContext(RequestInterface::class, new RequestMessage()); - } - - - /** - * @param $name - * @return mixed - */ - public function __get($name): mixed - { - // TODO: Change the autogenerated stub - return $this->__call__()->{$name}; - } - - - /** - * @param \Swoole\Http\Request $request - * @return array - */ - public static function create(\Swoole\Http\Request $request): array - { - Context::setContext(ResponseInterface::class, $response = new Response()); - - Context::setContext(RequestInterface::class, RequestMessage::parseRequest($request)); - - return [Kiri::getDi()->get(Request::class), $response]; - } - - - /** - * @return string - */ - public function getProtocolVersion(): string - { - return $this->__call__()->{__FUNCTION__}(); - } - - - /** - * @param string $version - * @return Request - */ - public function withProtocolVersion($version): RequestInterface - { - return $this->__call__()->{__FUNCTION__}($version); - } - - - /** - * @return \string[][] - */ - public function getHeaders(): array - { - return $this->__call__()->{__FUNCTION__}(); - } - - - /** - * @param string $name - * @return bool - */ - public function hasHeader($name): bool - { - return $this->__call__()->{__FUNCTION__}($name); - } - - - /** - * @param string $name - * @return string[] - */ - public function getHeader($name): array - { - return $this->__call__()->{__FUNCTION__}($name); - } - - - /** - * @param string $name - * @return string - */ - public function getHeaderLine($name): string - { - return $this->__call__()->{__FUNCTION__}($name); - } - - - /** - * @param string $name - * @param string|string[] $value - * @return Request - */ - public function withHeader($name, $value): RequestInterface - { - return $this->__call__()->{__FUNCTION__}($name, $value); - } - - - /** - * @param string $name - * @param string|string[] $value - * @return Request - */ - public function withAddedHeader($name, $value): RequestInterface - { - return $this->__call__()->{__FUNCTION__}($name, $value); - } - - - /** - * @param string $name - * @return Request - */ - public function withoutHeader($name): RequestInterface - { - return $this->__call__()->{__FUNCTION__}($name); - } - - - /** - * @return StreamInterface - */ - public function getBody(): StreamInterface - { - return $this->__call__()->{__FUNCTION__}(); - } - - - /** - * @param StreamInterface $body - * @return Request - */ - public function withBody(StreamInterface $body): RequestInterface - { - return $this->__call__()->{__FUNCTION__}($body); - } - - - /** - * @return string - */ - public function getRequestTarget(): string - { - return $this->__call__()->{__FUNCTION__}(); - } - - - /** - * @param mixed $requestTarget - * @return Request - */ - public function withRequestTarget($requestTarget): RequestInterface - { - return $this->__call__()->{__FUNCTION__}($requestTarget); - } - - - /** - * @return string - */ - public function getMethod(): string - { - return $this->__call__()->{__FUNCTION__}(); - } - - - /** - * @param string $method - * @return bool - */ - public function isMethod(string $method): bool - { - return $this->__call__()->{__FUNCTION__}($method); - } - - - /** - * @param string $method - * @return Request - */ - public function withMethod($method): RequestInterface - { - return $this->__call__()->{__FUNCTION__}($method); - } - - - /** - * @return UriInterface - */ - public function getUri(): UriInterface - { - return $this->__call__()->{__FUNCTION__}(); - } - - - /** - * @param UriInterface $uri - * @param false $preserveHost - * @return Request - */ - public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface - { - return $this->__call__()->{__FUNCTION__}($uri, $preserveHost); - } - - - /** - * @param string $name - * @return Uploaded|null - */ - public function file(string $name): ?Uploaded - { - return $this->__call__()->{__FUNCTION__}($name); - } - - - /** - * @return array - */ - public function all(): array - { - return $this->__call__()->{__FUNCTION__}(); - } - - - /** - * @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__()->{__FUNCTION__}($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__()->{__FUNCTION__}($name, $default); - } - - - /** - * @param string $name - * @param bool $required - * @return int|null - */ - public function int(string $name, bool $required = false): ?int - { - return $this->__call__()->{__FUNCTION__}($name, $required); - } - - - /** - * @param string $name - * @param bool $required - * @return float|null - */ - public function float(string $name, bool $required = false): ?float - { - return $this->__call__()->{__FUNCTION__}($name, $required); - } - - - /** - * @param string $name - * @param bool $required - * @return string|null - */ - public function date(string $name, bool $required = false): ?string - { - return $this->__call__()->{__FUNCTION__}($name, $required); - } - - - /** - * @param string $name - * @param bool $required - * @return int|null - */ - public function timestamp(string $name, bool $required = false): ?int - { - return $this->__call__()->{__FUNCTION__}($name, $required); - } - - - /** - * @param string $name - * @param bool $required - * @return string|null - */ - public function string(string $name, bool $required = false): ?string - { - return $this->__call__()->{__FUNCTION__}($name, $required); - } - - - /** - * @param string $name - * @param array $default - * @return array|null - */ - public function array(string $name, array $default = []): ?array - { - return $this->__call__()->{__FUNCTION__}($name, $default); - } - - - /** - * @return array|null - */ - public function gets(): ?array - { - return $this->__call__()->{__FUNCTION__}(); - } - - - /** - * @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__()->{__FUNCTION__}($field, $sizeField, $max); - } - - - /** - * @param string $field - * @param int $max - * @return int - */ - public function size(string $field = 'size', int $max = 100): int - { - return $this->__call__()->{__FUNCTION__}($field, $max); - } - - - /** - * @param $name - * @param null $default - * @return mixed - */ - public function input($name, $default = null): mixed - { - return $this->__call__()->{__FUNCTION__}($name, $default); - } - - - /** - * @return float - */ - #[Pure] public function getStartTime(): float - { - return $this->__call__()->{__FUNCTION__}(); - } - - - /** - * @param AuthIdentity $authority - */ - public function setAuthority(AuthIdentity $authority): void - { - $this->__call__()->{__FUNCTION__}($authority); - } - - - /** - * @return int - */ - public function getClientId(): int - { - return $this->__call__()->{__FUNCTION__}(); - } - - - /** - * @return string|null - */ - public function getAccessControlAllowOrigin(): ?string - { - return $this->__call__()->{__FUNCTION__}(); - } - - - /** - * @return string|null - */ - public function getAccessControlAllowHeaders(): ?string - { - return $this->__call__()->{__FUNCTION__}(); - } - - - /** - * @return string|null - */ - public function getAccessControlRequestMethod(): ?string - { - return $this->__call__()->{__FUNCTION__}(); - } + /** + * @return ServerRequest + */ + private function __call__(): ServerRequest + { + return Context::getContext(RequestInterface::class, new ServerRequest()); + } + + + /** + * @param $name + * @return mixed + */ + public function __get($name): mixed + { + // TODO: Change the autogenerated stub + return $this->__call__()->{$name}; + } + + + /** + * @param \Swoole\Http\Request $request + * @return array + */ + public static function create(\Swoole\Http\Request $request): array + { + $serverRequest = ServerRequest::createServerRequest($request); + + Context::setContext(ResponseInterface::class, $response = new Response()); + + Context::setContext(RequestInterface::class, $serverRequest); + + return [Kiri::getDi()->get(Request::class), $response]; + } + + + /** + * @return string + */ + public function getProtocolVersion(): string + { + return $this->__call__()->{__FUNCTION__}(); + } + + + /** + * @param string $version + * @return Request + */ + public function withProtocolVersion($version): RequestInterface + { + return $this->__call__()->{__FUNCTION__}($version); + } + + + /** + * @return \string[][] + */ + public function getHeaders(): array + { + return $this->__call__()->{__FUNCTION__}(); + } + + + /** + * @param string $name + * @return bool + */ + public function hasHeader($name): bool + { + return $this->__call__()->{__FUNCTION__}($name); + } + + + /** + * @param string $name + * @return string[] + */ + public function getHeader($name): array + { + return $this->__call__()->{__FUNCTION__}($name); + } + + + /** + * @param string $name + * @return string + */ + public function getHeaderLine($name): string + { + return $this->__call__()->{__FUNCTION__}($name); + } + + + /** + * @param string $name + * @param string|string[] $value + * @return Request + */ + public function withHeader($name, $value): RequestInterface + { + return $this->__call__()->{__FUNCTION__}($name, $value); + } + + + /** + * @param string $name + * @param string|string[] $value + * @return Request + */ + public function withAddedHeader($name, $value): RequestInterface + { + return $this->__call__()->{__FUNCTION__}($name, $value); + } + + + /** + * @param string $name + * @return Request + */ + public function withoutHeader($name): RequestInterface + { + return $this->__call__()->{__FUNCTION__}($name); + } + + + /** + * @return StreamInterface + */ + public function getBody(): StreamInterface + { + return $this->__call__()->{__FUNCTION__}(); + } + + + /** + * @param StreamInterface $body + * @return Request + */ + public function withBody(StreamInterface $body): RequestInterface + { + return $this->__call__()->{__FUNCTION__}($body); + } + + + /** + * @return string + */ + public function getRequestTarget(): string + { + return $this->__call__()->{__FUNCTION__}(); + } + + + /** + * @param mixed $requestTarget + * @return Request + */ + public function withRequestTarget($requestTarget): RequestInterface + { + return $this->__call__()->{__FUNCTION__}($requestTarget); + } + + + /** + * @return string + */ + public function getMethod(): string + { + return $this->__call__()->{__FUNCTION__}(); + } + + + /** + * @param string $method + * @return bool + */ + public function isMethod(string $method): bool + { + return $this->__call__()->{__FUNCTION__}($method); + } + + + /** + * @param string $method + * @return Request + */ + public function withMethod($method): RequestInterface + { + return $this->__call__()->{__FUNCTION__}($method); + } + + + /** + * @return UriInterface + */ + public function getUri(): UriInterface + { + return $this->__call__()->{__FUNCTION__}(); + } + + + /** + * @param UriInterface $uri + * @param false $preserveHost + * @return Request + */ + public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface + { + return $this->__call__()->{__FUNCTION__}($uri, $preserveHost); + } + + + /** + * @param string $name + * @return UploadedFileInterface|null + */ + public function file(string $name): ?UploadedFileInterface + { + $files = $this->__call__()->getParsedBody(); + if (!isset($files[$name])) { + return null; + } + return new Uploaded($files['tmp_name'], $files['name'], $files['type'], $files['size'], $files['error']); + } + + + /** + * @param string|null $name + * @param mixed|null $default + * @return mixed + */ + private function _getParsedBody(string|null $name = null, mixed $default = null): mixed + { + $body = $this->__call__()->getParsedBody(); + if (empty($name)) { + return $body; + } + return $body[$name] ?? $default; + } + + + /** + * @return array + */ + public function all(): array + { + return $this->_getParsedBody(); + } + + + /** + * @param string $name + * @param bool|int|string|null $default + * @return mixed + */ + public function query(string $name, bool|int|string|null $default = null): mixed + { + $files = $this->__call__()->getQueryParams(); + + return $files[$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->_getParsedBody($name, $default); + } + + + /** + * @param string $name + * @param bool $required + * @return int|null + * @throws \Exception + */ + public function int(string $name, bool $required = false): ?int + { + $int = $this->_getParsedBody($name); + if (is_null($int) && $required) { + throw new \Exception('Required param "' . $name . '"'); + } + return (int)$int; + } + + + /** + * @param string $name + * @param bool $required + * @return float|null + * @throws \Exception + */ + public function float(string $name, bool $required = false): ?float + { + $int = $this->_getParsedBody($name); + if (is_null($int) && $required) { + throw new \Exception('Required param "' . $name . '"'); + } + return (float)$int; + } + + + /** + * @param string $name + * @param bool $required + * @return string|null + * @throws \Exception + */ + public function date(string $name, bool $required = false): ?string + { + $int = $this->_getParsedBody($name); + if (is_null($int) && $required) { + throw new \Exception('Required param "' . $name . '"'); + } + return (string)$int; + } + + + /** + * @param string $name + * @param bool $required + * @return int|null + * @throws \Exception + */ + public function timestamp(string $name, bool $required = false): ?int + { + $int = $this->_getParsedBody($name); + if (is_null($int) && $required) { + throw new \Exception('Required param "' . $name . '"'); + } + return (int)$int; + } + + + /** + * @param string $name + * @param bool $required + * @return string|null + * @throws \Exception + */ + public function string(string $name, bool $required = false): ?string + { + $int = $this->_getParsedBody($name); + if (is_null($int) && $required) { + throw new \Exception('Required param "' . $name . '"'); + } + return (string)$int; + } + + + /** + * @param string $name + * @param array $default + * @return array|null + */ + public function array(string $name, array $default = []): ?array + { + $int = $this->_getParsedBody($name); + if (is_null($int)) { + return $default; + } + return $int; + } + + + /** + * @return array|null + */ + public function gets(): ?array + { + return $this->__call__()->getQueryParams(); + } + + + /** + * @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 + { + $page = $this->query($field); + $size = $this->size($sizeField, $max); + $offset = ($page - 1) * $size; + if ($offset < 0) { + $offset = 0; + } + return $offset; + } + + + /** + * @param string $field + * @param int $max + * @return int + */ + public function size(string $field = 'size', int $max = 100): int + { + $size = $this->query($field); + if ($size > $max) { + $size = $max; + } + return $size; + } + + + /** + * @param $name + * @param null $default + * @return mixed + */ + public function input($name, $default = null): mixed + { + return $this->_getParsedBody($name, $default); + } + + + /** + * @return float + */ + #[Pure] public function getStartTime(): float + { + return $this->__call__()->{__FUNCTION__}(); + } + + + /** + * @param AuthIdentity $authority + */ + public function setAuthority(AuthIdentity $authority): void + { + $this->__call__()->{__FUNCTION__}($authority); + } + + + /** + * @return int + */ + public function getClientId(): int + { + return $this->__call__()->{__FUNCTION__}(); + } + + + /** + * @return string|null + */ + #[Pure] public function getAccessControlAllowOrigin(): ?string + { + return $this->__call__()->{__FUNCTION__}(); + } + + + /** + * @return string|null + */ + #[Pure] public function getAccessControlAllowHeaders(): ?string + { + return $this->__call__()->{__FUNCTION__}(); + } + + + /** + * @return string|null + */ + #[Pure] public function getAccessControlRequestMethod(): ?string + { + return $this->__call__()->{__FUNCTION__}(); + } } diff --git a/http-server/RequestInterface.php b/http-server/RequestInterface.php index fbcf4dfe..46ea8397 100644 --- a/http-server/RequestInterface.php +++ b/http-server/RequestInterface.php @@ -5,8 +5,8 @@ namespace Server; use Http\IInterface\AuthIdentity; use JetBrains\PhpStorm\Pure; +use Psr\Http\Message\UploadedFileInterface; use Server\Message\Request; -use Server\Message\Uploaded; /** * @@ -24,9 +24,9 @@ interface RequestInterface extends \Psr\Http\Message\RequestInterface /** * @param string $name - * @return Uploaded|null + * @return UploadedFileInterface|null */ - public function file(string $name): ?Uploaded; + public function file(string $name): ?UploadedFileInterface; /**