This commit is contained in:
as2252258@163.com
2021-09-10 03:50:45 +08:00
parent a61e663cf0
commit 2cf472082f
4 changed files with 543 additions and 507 deletions
+59 -50
View File
@@ -13,7 +13,7 @@ use Psr\Http\Message\UriInterface;
abstract class Request implements RequestInterface abstract class Request implements RequestInterface
{ {
use Message; use Message;
/** /**
@@ -28,63 +28,72 @@ abstract class Request implements RequestInterface
protected string $method; protected string $method;
/**
/** * @return string
* @return string */
*/ public function getRequestTarget(): string
public function getRequestTarget(): string {
{ throw new BadMethodCallException('Not Accomplish Method.');
throw new BadMethodCallException('Not Accomplish Method.'); }
}
/** /**
* @param mixed $requestTarget * @param mixed $requestTarget
* @return static * @return static
*/ */
public function withRequestTarget($requestTarget): static public function withRequestTarget($requestTarget): static
{ {
throw new BadMethodCallException('Not Accomplish Method.'); throw new BadMethodCallException('Not Accomplish Method.');
} }
/** /**
* @return string * @return string
*/ */
public function getMethod(): string public function getMethod(): string
{ {
return $this->method; return $this->method;
} }
/** /**
* @param string $method * @param string $method
* @return RequestInterface * @return RequestInterface
*/ */
public function withMethod($method): RequestInterface public function withMethod($method): RequestInterface
{ {
$this->method = $method; $this->method = $method;
return $this; return $this;
} }
/** /**
* @return UriInterface * @param string $method
*/ * @return bool
public function getUri(): UriInterface */
{ public function isMethod(string $method): bool
return $this->uriInterface; {
} return $this->method == $method;
}
/** /**
* @param UriInterface $uri * @return UriInterface
* @param false $preserveHost */
* @return $this|Request public function getUri(): UriInterface
*/ {
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface return $this->uriInterface;
{ }
$this->uriInterface = $uri;
return $this;
} /**
* @param UriInterface $uri
* @param false $preserveHost
* @return $this|Request
*/
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
{
$this->uriInterface = $uri;
return $this;
}
} }
-40
View File
@@ -3,12 +3,8 @@
namespace Protocol\Message; namespace Protocol\Message;
use Http\Context\Context; 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\ServerRequestInterface;
use Psr\Http\Message\StreamInterface; 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 * @return null|array
*/ */
+481 -414
View File
@@ -5,12 +5,17 @@ namespace Server\Constrict;
use Http\Context\Context; use Http\Context\Context;
use Http\IInterface\AuthIdentity; use Http\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri\Core\Xml;
use Kiri\Kiri; 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\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Server\Message\Request as RequestMessage; use Server\Message\Request as RequestMessage;
use Server\Message\Response; use Server\Message\Response;
use Server\Message\Uploaded;
use Server\RequestInterface; use Server\RequestInterface;
use Server\ResponseInterface; use Server\ResponseInterface;
@@ -19,417 +24,479 @@ class Request implements RequestInterface
{ {
/** /**
* @return RequestMessage * @return ServerRequest
*/ */
private function __call__(): RequestMessage private function __call__(): ServerRequest
{ {
return Context::getContext(RequestInterface::class, new RequestMessage()); return Context::getContext(RequestInterface::class, new ServerRequest());
} }
/** /**
* @param $name * @param $name
* @return mixed * @return mixed
*/ */
public function __get($name): mixed public function __get($name): mixed
{ {
// TODO: Change the autogenerated stub // TODO: Change the autogenerated stub
return $this->__call__()->{$name}; return $this->__call__()->{$name};
} }
/** /**
* @param \Swoole\Http\Request $request * @param \Swoole\Http\Request $request
* @return array<RequestInterface, ResponseInterface> * @return array<RequestInterface, ResponseInterface>
*/ */
public static function create(\Swoole\Http\Request $request): array public static function create(\Swoole\Http\Request $request): array
{ {
Context::setContext(ResponseInterface::class, $response = new Response()); $serverRequest = ServerRequest::createServerRequest($request);
Context::setContext(RequestInterface::class, RequestMessage::parseRequest($request)); Context::setContext(ResponseInterface::class, $response = new Response());
return [Kiri::getDi()->get(Request::class), $response]; Context::setContext(RequestInterface::class, $serverRequest);
}
return [Kiri::getDi()->get(Request::class), $response];
}
/**
* @return string
*/ /**
public function getProtocolVersion(): string * @return string
{ */
return $this->__call__()->{__FUNCTION__}(); public function getProtocolVersion(): string
} {
return $this->__call__()->{__FUNCTION__}();
}
/**
* @param string $version
* @return Request /**
*/ * @param string $version
public function withProtocolVersion($version): RequestInterface * @return Request
{ */
return $this->__call__()->{__FUNCTION__}($version); public function withProtocolVersion($version): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($version);
}
/**
* @return \string[][]
*/ /**
public function getHeaders(): array * @return \string[][]
{ */
return $this->__call__()->{__FUNCTION__}(); public function getHeaders(): array
} {
return $this->__call__()->{__FUNCTION__}();
}
/**
* @param string $name
* @return bool /**
*/ * @param string $name
public function hasHeader($name): bool * @return bool
{ */
return $this->__call__()->{__FUNCTION__}($name); public function hasHeader($name): bool
} {
return $this->__call__()->{__FUNCTION__}($name);
}
/**
* @param string $name
* @return string[] /**
*/ * @param string $name
public function getHeader($name): array * @return string[]
{ */
return $this->__call__()->{__FUNCTION__}($name); public function getHeader($name): array
} {
return $this->__call__()->{__FUNCTION__}($name);
}
/**
* @param string $name
* @return string /**
*/ * @param string $name
public function getHeaderLine($name): string * @return string
{ */
return $this->__call__()->{__FUNCTION__}($name); public function getHeaderLine($name): string
} {
return $this->__call__()->{__FUNCTION__}($name);
}
/**
* @param string $name
* @param string|string[] $value /**
* @return Request * @param string $name
*/ * @param string|string[] $value
public function withHeader($name, $value): RequestInterface * @return Request
{ */
return $this->__call__()->{__FUNCTION__}($name, $value); public function withHeader($name, $value): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($name, $value);
}
/**
* @param string $name
* @param string|string[] $value /**
* @return Request * @param string $name
*/ * @param string|string[] $value
public function withAddedHeader($name, $value): RequestInterface * @return Request
{ */
return $this->__call__()->{__FUNCTION__}($name, $value); public function withAddedHeader($name, $value): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($name, $value);
}
/**
* @param string $name
* @return Request /**
*/ * @param string $name
public function withoutHeader($name): RequestInterface * @return Request
{ */
return $this->__call__()->{__FUNCTION__}($name); public function withoutHeader($name): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($name);
}
/**
* @return StreamInterface
*/ /**
public function getBody(): StreamInterface * @return StreamInterface
{ */
return $this->__call__()->{__FUNCTION__}(); public function getBody(): StreamInterface
} {
return $this->__call__()->{__FUNCTION__}();
}
/**
* @param StreamInterface $body
* @return Request /**
*/ * @param StreamInterface $body
public function withBody(StreamInterface $body): RequestInterface * @return Request
{ */
return $this->__call__()->{__FUNCTION__}($body); public function withBody(StreamInterface $body): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($body);
}
/**
* @return string
*/ /**
public function getRequestTarget(): string * @return string
{ */
return $this->__call__()->{__FUNCTION__}(); public function getRequestTarget(): string
} {
return $this->__call__()->{__FUNCTION__}();
}
/**
* @param mixed $requestTarget
* @return Request /**
*/ * @param mixed $requestTarget
public function withRequestTarget($requestTarget): RequestInterface * @return Request
{ */
return $this->__call__()->{__FUNCTION__}($requestTarget); public function withRequestTarget($requestTarget): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($requestTarget);
}
/**
* @return string
*/ /**
public function getMethod(): string * @return string
{ */
return $this->__call__()->{__FUNCTION__}(); public function getMethod(): string
} {
return $this->__call__()->{__FUNCTION__}();
}
/**
* @param string $method
* @return bool /**
*/ * @param string $method
public function isMethod(string $method): bool * @return bool
{ */
return $this->__call__()->{__FUNCTION__}($method); public function isMethod(string $method): bool
} {
return $this->__call__()->{__FUNCTION__}($method);
}
/**
* @param string $method
* @return Request /**
*/ * @param string $method
public function withMethod($method): RequestInterface * @return Request
{ */
return $this->__call__()->{__FUNCTION__}($method); public function withMethod($method): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($method);
}
/**
* @return UriInterface
*/ /**
public function getUri(): UriInterface * @return UriInterface
{ */
return $this->__call__()->{__FUNCTION__}(); public function getUri(): UriInterface
} {
return $this->__call__()->{__FUNCTION__}();
}
/**
* @param UriInterface $uri
* @param false $preserveHost /**
* @return Request * @param UriInterface $uri
*/ * @param false $preserveHost
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface * @return Request
{ */
return $this->__call__()->{__FUNCTION__}($uri, $preserveHost); public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
} {
return $this->__call__()->{__FUNCTION__}($uri, $preserveHost);
}
/**
* @param string $name
* @return Uploaded|null /**
*/ * @param string $name
public function file(string $name): ?Uploaded * @return UploadedFileInterface|null
{ */
return $this->__call__()->{__FUNCTION__}($name); public function file(string $name): ?UploadedFileInterface
} {
$files = $this->__call__()->getParsedBody();
if (!isset($files[$name])) {
/** return null;
* @return array }
*/ return new Uploaded($files['tmp_name'], $files['name'], $files['type'], $files['size'], $files['error']);
public function all(): array }
{
return $this->__call__()->{__FUNCTION__}();
} /**
* @param string|null $name
* @param mixed|null $default
/** * @return mixed
* @param string $name */
* @param bool|int|string|null $default private function _getParsedBody(string|null $name = null, mixed $default = null): mixed
* @return mixed {
*/ $body = $this->__call__()->getParsedBody();
public function query(string $name, bool|int|string|null $default = null): mixed if (empty($name)) {
{ return $body;
return $this->__call__()->{__FUNCTION__}($name, $default); }
} return $body[$name] ?? $default;
}
/**
* @param string $name /**
* @param int|bool|array|string|null $default * @return array
* @return mixed */
*/ public function all(): array
public function post(string $name, int|bool|array|string|null $default = null): mixed {
{ return $this->_getParsedBody();
return $this->__call__()->{__FUNCTION__}($name, $default); }
}
/**
/** * @param string $name
* @param string $name * @param bool|int|string|null $default
* @param bool $required * @return mixed
* @return int|null */
*/ public function query(string $name, bool|int|string|null $default = null): mixed
public function int(string $name, bool $required = false): ?int {
{ $files = $this->__call__()->getQueryParams();
return $this->__call__()->{__FUNCTION__}($name, $required);
} return $files[$name] ?? $default;
}
/**
* @param string $name /**
* @param bool $required * @param string $name
* @return float|null * @param int|bool|array|string|null $default
*/ * @return mixed
public function float(string $name, bool $required = false): ?float */
{ public function post(string $name, int|bool|array|string|null $default = null): mixed
return $this->__call__()->{__FUNCTION__}($name, $required); {
} return $this->_getParsedBody($name, $default);
}
/**
* @param string $name /**
* @param bool $required * @param string $name
* @return string|null * @param bool $required
*/ * @return int|null
public function date(string $name, bool $required = false): ?string * @throws \Exception
{ */
return $this->__call__()->{__FUNCTION__}($name, $required); public function int(string $name, bool $required = false): ?int
} {
$int = $this->_getParsedBody($name);
if (is_null($int) && $required) {
/** throw new \Exception('Required param "' . $name . '"');
* @param string $name }
* @param bool $required return (int)$int;
* @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 float|null
* @throws \Exception
/** */
* @param string $name public function float(string $name, bool $required = false): ?float
* @param bool $required {
* @return string|null $int = $this->_getParsedBody($name);
*/ if (is_null($int) && $required) {
public function string(string $name, bool $required = false): ?string throw new \Exception('Required param "' . $name . '"');
{ }
return $this->__call__()->{__FUNCTION__}($name, $required); return (float)$int;
} }
/** /**
* @param string $name * @param string $name
* @param array $default * @param bool $required
* @return array|null * @return string|null
*/ * @throws \Exception
public function array(string $name, array $default = []): ?array */
{ public function date(string $name, bool $required = false): ?string
return $this->__call__()->{__FUNCTION__}($name, $default); {
} $int = $this->_getParsedBody($name);
if (is_null($int) && $required) {
throw new \Exception('Required param "' . $name . '"');
/** }
* @return array|null return (string)$int;
*/ }
public function gets(): ?array
{
return $this->__call__()->{__FUNCTION__}(); /**
} * @param string $name
* @param bool $required
* @return int|null
/** * @throws \Exception
* @param string $field */
* @param string $sizeField public function timestamp(string $name, bool $required = false): ?int
* @param int $max {
* @return float|int $int = $this->_getParsedBody($name);
*/ if (is_null($int) && $required) {
public function offset(string $field = 'page', string $sizeField = 'size', int $max = 100): float|int throw new \Exception('Required param "' . $name . '"');
{ }
return $this->__call__()->{__FUNCTION__}($field, $sizeField, $max); return (int)$int;
} }
/** /**
* @param string $field * @param string $name
* @param int $max * @param bool $required
* @return int * @return string|null
*/ * @throws \Exception
public function size(string $field = 'size', int $max = 100): int */
{ public function string(string $name, bool $required = false): ?string
return $this->__call__()->{__FUNCTION__}($field, $max); {
} $int = $this->_getParsedBody($name);
if (is_null($int) && $required) {
throw new \Exception('Required param "' . $name . '"');
/** }
* @param $name return (string)$int;
* @param null $default }
* @return mixed
*/
public function input($name, $default = null): mixed /**
{ * @param string $name
return $this->__call__()->{__FUNCTION__}($name, $default); * @param array $default
} * @return array|null
*/
public function array(string $name, array $default = []): ?array
/** {
* @return float $int = $this->_getParsedBody($name);
*/ if (is_null($int)) {
#[Pure] public function getStartTime(): float return $default;
{ }
return $this->__call__()->{__FUNCTION__}(); return $int;
} }
/** /**
* @param AuthIdentity $authority * @return array|null
*/ */
public function setAuthority(AuthIdentity $authority): void public function gets(): ?array
{ {
$this->__call__()->{__FUNCTION__}($authority); return $this->__call__()->getQueryParams();
} }
/** /**
* @return int * @param string $field
*/ * @param string $sizeField
public function getClientId(): int * @param int $max
{ * @return float|int
return $this->__call__()->{__FUNCTION__}(); */
} public function offset(string $field = 'page', string $sizeField = 'size', int $max = 100): float|int
{
$page = $this->query($field);
/** $size = $this->size($sizeField, $max);
* @return string|null $offset = ($page - 1) * $size;
*/ if ($offset < 0) {
public function getAccessControlAllowOrigin(): ?string $offset = 0;
{ }
return $this->__call__()->{__FUNCTION__}(); return $offset;
} }
/** /**
* @return string|null * @param string $field
*/ * @param int $max
public function getAccessControlAllowHeaders(): ?string * @return int
{ */
return $this->__call__()->{__FUNCTION__}(); public function size(string $field = 'size', int $max = 100): int
} {
$size = $this->query($field);
if ($size > $max) {
/** $size = $max;
* @return string|null }
*/ return $size;
public function getAccessControlRequestMethod(): ?string }
{
return $this->__call__()->{__FUNCTION__}();
} /**
* @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__}();
}
} }
+3 -3
View File
@@ -5,8 +5,8 @@ namespace Server;
use Http\IInterface\AuthIdentity; use Http\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Psr\Http\Message\UploadedFileInterface;
use Server\Message\Request; use Server\Message\Request;
use Server\Message\Uploaded;
/** /**
* *
@@ -24,9 +24,9 @@ interface RequestInterface extends \Psr\Http\Message\RequestInterface
/** /**
* @param string $name * @param string $name
* @return Uploaded|null * @return UploadedFileInterface|null
*/ */
public function file(string $name): ?Uploaded; public function file(string $name): ?UploadedFileInterface;
/** /**