diff --git a/http-message/Message.php b/http-message/Message.php index 0c30d07e..cb3cb8cf 100644 --- a/http-message/Message.php +++ b/http-message/Message.php @@ -3,7 +3,6 @@ namespace Protocol\Message; use JetBrains\PhpStorm\Pure; -use Kiri\Core\Xml; use Psr\Http\Message\StreamInterface; @@ -13,211 +12,210 @@ use Psr\Http\Message\StreamInterface; trait Message { - /** - * @var string - */ - protected string $version; + /** + * @var string + */ + protected string $version; - /** - * @var \Psr\Http\Message\StreamInterface - */ - protected StreamInterface $stream; + /** + * @var StreamInterface + */ + protected StreamInterface $stream; - /** - * @var array - */ - protected array $headers = []; + /** + * @var array + */ + protected array $headers = []; - /** - * @return string - */ - public function getProtocolVersion(): string - { - return $this->version; - } + /** + * @return string + */ + public function getProtocolVersion(): string + { + return $this->version; + } - /** - * @param $version - * @return static - */ - public function withProtocolVersion($version): static - { - $this->version = $version; - return $this; - } + /** + * @param $version + * @return static + */ + public function withProtocolVersion($version): static + { + $this->version = $version; + return $this; + } - /** - * @return array - */ - public function getHeaders(): array - { - return $this->headers; - } + /** + * @return array + */ + public function getHeaders(): array + { + return $this->headers; + } - /** - * @param $name - * @return bool - */ - public function hasHeader($name): bool - { - return array_key_exists($name, $this->headers); - } + /** + * @param $name + * @return bool + */ + public function hasHeader($name): bool + { + return array_key_exists($name, $this->headers); + } - /** - * @param $name - * @return string|array|null - */ - #[Pure] public function getHeader($name): string|null|array - { - if (!$this->hasHeader($name)) { - return null; - } - return $this->headers[$name]; - } + /** + * @param $name + * @return string|array|null + */ + #[Pure] public function getHeader($name): string|null|array + { + if (!$this->hasHeader($name)) { + return null; + } + return $this->headers[$name]; + } - /** - * @param \Swoole\Http\Request $request - * @return static - */ - public function parseRequestHeaders(\Swoole\Http\Request $request): static - { - $index = strpos($request->getData(), "\r\n\r\n"); - $headers = explode("\r\n", substr($request->getData(), 0, $index)); - array_shift($headers); - foreach ($headers as $header) { - [$key, $value] = explode(': ', $header); - $this->addRequestHeader($key, $value); - } - return $this; - } + /** + * @param \Swoole\Http\Request $request + * @return static + */ + public function parseRequestHeaders(\Swoole\Http\Request $request): static + { + $index = strpos($request->getData(), "\r\n\r\n"); + $headers = explode("\r\n", substr($request->getData(), 0, $index)); + array_shift($headers); + foreach ($headers as $header) { + [$key, $value] = explode(': ', $header); + $this->addRequestHeader($key, $value); + } + return $this; + } - /** - * @param $key - * @param $value - */ - private function addRequestHeader($key, $value) - { - $this->headers[$key] = [$value]; - } + /** + * @param $key + * @param $value + */ + private function addRequestHeader($key, $value) + { + $this->headers[$key] = [$value]; + } - /** - * @param $name - * @return string|null - */ - #[Pure] public function getHeaderLine($name): string|null - { - if ($this->hasHeader($name)) { - return implode(';', $this->headers[$name]); - } - return null; - } + /** + * @param $name + * @return string|null + */ + #[Pure] public function getHeaderLine($name): string|null + { + if ($this->hasHeader($name)) { + return implode(';', $this->headers[$name]); + } + return null; + } - /** - * @return string|null - */ - #[Pure] public function getContentType(): ?string - { - return $this->getHeaderLine('Content-Type'); - } + /** + * @return string|null + */ + #[Pure] public function getContentType(): ?string + { + return $this->getHeaderLine('Content-Type'); + } - /** - * @param $name - * @param $value - * @return static - */ - public function withHeader($name, $value): static - { - if (!is_array($value)) { - $value = [$value]; - } - $this->headers[$name] = $value; - return $this; - } + /** + * @param $name + * @param $value + * @return static + */ + public function withHeader($name, $value): static + { + if (!is_array($value)) { + $value = [$value]; + } + $this->headers[$name] = $value; + return $this; + } - /** - * @param $name - * @param $value - * @return static - * @throws - */ - public function withAddedHeader($name, $value): static - { - if (!array_key_exists($name, $this->headers)) { - throw new \Exception('Headers `' . $name . '` not exists.'); - } - $this->headers[$name][] = $value; - return $this; - } + /** + * @param $name + * @param $value + * @return static + * @throws + */ + public function withAddedHeader($name, $value): static + { + if (!array_key_exists($name, $this->headers)) { + throw new \Exception('Headers `' . $name . '` not exists.'); + } + $this->headers[$name][] = $value; + return $this; + } - /** - * @param $name - * @return static - */ - public function withoutHeader($name): static - { - unset($this->headers[$name]); - return $this; - } + /** + * @param $name + * @return static + */ + public function withoutHeader($name): static + { + unset($this->headers[$name]); + return $this; + } - /** - * @return string - */ - public function getBody(): string - { - return $this->stream->getContents(); - } + /** + * @return string + */ + public function getBody(): string + { + return $this->stream->getContents(); + } - /** - * @param StreamInterface $body - * @return static - */ - public function withBody(StreamInterface $body): static - { - $this->stream = $body; - return $this; - } + /** + * @param StreamInterface $body + * @return static + */ + public function withBody(StreamInterface $body): static + { + $this->stream = $body; + return $this; + } - - /** - * @return string|null - */ - #[Pure] public function getAccessControlAllowOrigin(): ?string - { - return $this->getHeaderLine('Access-Control-Allow-Origin'); - } + /** + * @return string|null + */ + #[Pure] public function getAccessControlAllowOrigin(): ?string + { + return $this->getHeaderLine('Access-Control-Allow-Origin'); + } - /** - * @return string|null - */ - #[Pure] public function getAccessControlAllowHeaders(): ?string - { - return $this->getHeaderLine('Access-Control-Allow-Headers'); - } + /** + * @return string|null + */ + #[Pure] public function getAccessControlAllowHeaders(): ?string + { + return $this->getHeaderLine('Access-Control-Allow-Headers'); + } - /** - * @return string|null - */ - #[Pure] public function getAccessControlRequestMethod(): ?string - { - return $this->getHeaderLine('Access-Control-Request-Method'); - } + /** + * @return string|null + */ + #[Pure] public function getAccessControlRequestMethod(): ?string + { + return $this->getHeaderLine('Access-Control-Request-Method'); + } } diff --git a/http-message/Parse.php b/http-message/Parse.php index 6692e4ce..96a013bd 100644 --- a/http-message/Parse.php +++ b/http-message/Parse.php @@ -8,27 +8,27 @@ class Parse { - /** - * @param $content - * @param $contentType - * @return mixed - */ - public static function data($content, $contentType): mixed - { - if (str_contains($contentType, 'json')) { - return json_encode($contentType); - } - if (str_contains($contentType, 'xml')) { - return Xml::toArray($contentType); - } - if (str_contains($contentType, 'x-www-form-urlencoded')) { - parse_str($content, $array); - return $array; - } - if (str_contains($contentType, 'serialize')) { - return unserialize($content); - } - return $content; - } + /** + * @param $content + * @param $contentType + * @return mixed + */ + public static function data($content, $contentType): mixed + { + if (str_contains($contentType, 'json')) { + return json_encode($contentType); + } + if (str_contains($contentType, 'xml')) { + return Xml::toArray($contentType); + } + if (str_contains($contentType, 'x-www-form-urlencoded')) { + parse_str($content, $array); + return $array; + } + if (str_contains($contentType, 'serialize')) { + return unserialize($content); + } + return $content; + } } diff --git a/http-message/Request.php b/http-message/Request.php index 69ed02cc..68e19e07 100644 --- a/http-message/Request.php +++ b/http-message/Request.php @@ -14,102 +14,102 @@ use Psr\Http\Message\UriInterface; abstract class Request implements RequestInterface { - use Message; + use Message; - /** - * @var \Psr\Http\Message\UriInterface - */ - protected UriInterface $uriInterface; + /** + * @var UriInterface + */ + protected UriInterface $uriInterface; - /** - * @var string - */ - protected string $method; + /** + * @var string + */ + protected string $method; - /** - * @var \Http\IInterface\AuthIdentity - */ - public AuthIdentity $authority; + /** + * @var AuthIdentity|null + */ + public ?AuthIdentity $authority = null; - /** - * @param \Http\IInterface\AuthIdentity $authIdentity - */ - public function setAuthority(AuthIdentity $authIdentity): void - { - $this->authority = $authIdentity; - } + /** + * @param AuthIdentity|null $authIdentity + */ + public function setAuthority(?AuthIdentity $authIdentity): void + { + $this->authority = $authIdentity; + } - /** - * @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; + } - /** - * @param string $method - * @return bool - */ - public function isMethod(string $method): bool - { - return $this->method == $method; - } + /** + * @param string $method + * @return bool + */ + public function isMethod(string $method): bool + { + return $this->method == $method; + } - /** - * @return UriInterface - */ - public function getUri(): UriInterface - { - return $this->uriInterface; - } + /** + * @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; - } + /** + * @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/Response.php b/http-message/Response.php index f4d8e0ce..2d5f660a 100644 --- a/http-message/Response.php +++ b/http-message/Response.php @@ -10,69 +10,69 @@ class Response implements ResponseInterface { - use Message; + use Message; - protected int $statusCode = 200; + protected int $statusCode = 200; - protected string $reasonPhrase = ''; + protected string $reasonPhrase = ''; - /** - * @return int - */ - public function getStatusCode(): int - { - return $this->statusCode; - } + /** + * @return int + */ + public function getStatusCode(): int + { + return $this->statusCode; + } - /** - * @param int $code - * @param string $reasonPhrase - * @return $this|\Protocol\Message\Response - */ - public function withStatus($code, $reasonPhrase = ''): static - { - $this->statusCode = $code; - $this->reasonPhrase = $reasonPhrase; - return $this; - } + /** + * @param int $code + * @param string $reasonPhrase + * @return $this|Response + */ + public function withStatus($code, $reasonPhrase = ''): static + { + $this->statusCode = $code; + $this->reasonPhrase = $reasonPhrase; + return $this; + } - /** - * @return string - */ - public function getReasonPhrase(): string - { - return $this->reasonPhrase; - } + /** + * @return string + */ + public function getReasonPhrase(): string + { + return $this->reasonPhrase; + } - /** - * @return string|null - */ - #[Pure] public function getAccessControlAllowOrigin(): ?string - { - return $this->getHeaderLine('Access-Control-Allow-Origin'); - } + /** + * @return string|null + */ + #[Pure] public function getAccessControlAllowOrigin(): ?string + { + return $this->getHeaderLine('Access-Control-Allow-Origin'); + } - /** - * @return string|null - */ - #[Pure] public function getAccessControlAllowHeaders(): ?string - { - return $this->getHeaderLine('Access-Control-Allow-Headers'); - } + /** + * @return string|null + */ + #[Pure] public function getAccessControlAllowHeaders(): ?string + { + return $this->getHeaderLine('Access-Control-Allow-Headers'); + } - /** - * @return string|null - */ - #[Pure] public function getAccessControlRequestMethod(): ?string - { - return $this->getHeaderLine('Access-Control-Request-Method'); - } + /** + * @return string|null + */ + #[Pure] public function getAccessControlRequestMethod(): ?string + { + return $this->getHeaderLine('Access-Control-Request-Method'); + } } diff --git a/http-message/ServerRequest.php b/http-message/ServerRequest.php index 5d7922d0..44c50b8f 100644 --- a/http-message/ServerRequest.php +++ b/http-message/ServerRequest.php @@ -14,217 +14,217 @@ class ServerRequest extends Request implements ServerRequestInterface { - /** - * @var mixed - */ - protected ?array $parsedBody = null; + /** + * @var mixed + */ + protected ?array $parsedBody = null; - /** - * @var array|null - */ - protected ?array $serverParams; + /** + * @var array|null + */ + protected ?array $serverParams; - /** - * @var array|null - */ - protected ?array $cookieParams = []; + /** + * @var array|null + */ + protected ?array $cookieParams = []; - /** - * @var array|null - */ - protected ?array $queryParams; + /** + * @var array|null + */ + protected ?array $queryParams; - /** - * @var array|null - */ - protected ?array $uploadedFiles; + /** + * @var array|null + */ + protected ?array $uploadedFiles; - protected \Swoole\Http\Request $serverTarget; + protected \Swoole\Http\Request $serverTarget; - /** - * @param array $server - * @return static - */ - public function withServerParams(array $server): static - { - $this->serverParams = $server; - return $this; - } + /** + * @param array $server + * @return static + */ + public function withServerParams(array $server): static + { + $this->serverParams = $server; + return $this; + } - /** - * @param \Swoole\Http\Request $server - * @return static - */ - public function withServerTarget(\Swoole\Http\Request $server): static - { - $this->serverTarget = $server; - return $this; - } + /** + * @param \Swoole\Http\Request $server + * @return static + */ + public function withServerTarget(\Swoole\Http\Request $server): static + { + $this->serverTarget = $server; + return $this; + } - /** - * @param \Swoole\Http\Request $request - * @return static - */ - public static function createServerRequest(\Swoole\Http\Request $request): static - { - return (new static())->withServerParams($request->server) - ->withServerTarget($request) - ->withCookieParams($request->cookie) - ->withUri(Uri::parseUri($request)) - ->withQueryParams($request->get ?? []) - ->withUploadedFiles($request->files ?? []) - ->withMethod($request->getMethod()) - ->parseRequestHeaders($request) - ->withParsedBody(function (StreamInterface $stream, ?array $posts) { - try { - $content = $stream->getContents(); - if (!empty($content)) { - return Parse::data($content, $this->getContentType()); - } - return $posts; - } catch (\Throwable $throwable) { - return $posts; - } - }); - } + /** + * @param \Swoole\Http\Request $request + * @return static + */ + public static function createServerRequest(\Swoole\Http\Request $request): static + { + return (new static())->withServerParams($request->server) + ->withServerTarget($request) + ->withCookieParams($request->cookie) + ->withUri(Uri::parseUri($request)) + ->withQueryParams($request->get ?? []) + ->withUploadedFiles($request->files ?? []) + ->withMethod($request->getMethod()) + ->parseRequestHeaders($request) + ->withParsedBody(function (StreamInterface $stream, ?array $posts) { + try { + $content = $stream->getContents(); + if (!empty($content)) { + return Parse::data($content, $this->getContentType()); + } + return $posts; + } catch (\Throwable $throwable) { + return $posts; + } + }); + } - /** - * @return null|array - */ - public function getServerParams(): ?array - { - return $this->serverParams; - } + /** + * @return null|array + */ + public function getServerParams(): ?array + { + return $this->serverParams; + } - /** - * @return null|array - */ - public function getCookieParams(): ?array - { - return $this->cookieParams; - } + /** + * @return null|array + */ + public function getCookieParams(): ?array + { + return $this->cookieParams; + } - /** - * @param array|null $cookies - * @return $this|ServerRequest - */ - public function withCookieParams(?array $cookies): ServerRequestInterface - { - $this->cookieParams = $cookies; - return $this; - } + /** + * @param array|null $cookies + * @return $this|ServerRequest + */ + public function withCookieParams(?array $cookies): ServerRequestInterface + { + $this->cookieParams = $cookies; + return $this; + } - /** - * @return array|null - */ - public function getQueryParams(): ?array - { - return $this->queryParams; - } + /** + * @return array|null + */ + public function getQueryParams(): ?array + { + return $this->queryParams; + } - /** - * @param array $query - * @return \Psr\Http\Message\ServerRequestInterface - */ - public function withQueryParams(array $query): ServerRequestInterface - { - $this->queryParams = $query; - return $this; - } + /** + * @param array $query + * @return \Psr\Http\Message\ServerRequestInterface + */ + public function withQueryParams(array $query): ServerRequestInterface + { + $this->queryParams = $query; + return $this; + } - /** - * @return array|null - */ - public function getUploadedFiles(): ?array - { - return $this->uploadedFiles; - } + /** + * @return array|null + */ + public function getUploadedFiles(): ?array + { + return $this->uploadedFiles; + } - /** - * @param array $uploadedFiles - * @return \Psr\Http\Message\ServerRequestInterface - */ - public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface - { - $this->uploadedFiles = $uploadedFiles; - return $this; - } + /** + * @param array $uploadedFiles + * @return \Psr\Http\Message\ServerRequestInterface + */ + public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface + { + $this->uploadedFiles = $uploadedFiles; + return $this; + } - /** - * @return array|object|null - */ - public function getParsedBody(): object|array|null - { - if (empty($this->parsedBody)) { - $callback = Context::getContext('with.parsed.body.callback'); + /** + * @return array|object|null + */ + public function getParsedBody(): object|array|null + { + if (empty($this->parsedBody)) { + $callback = Context::getContext('with.parsed.body.callback'); - $this->parsedBody = $callback($this->getBody()); - } - return $this->parsedBody; - } + $this->parsedBody = $callback($this->getBody()); + } + return $this->parsedBody; + } - /** - * @param array|object|null $data - * @return ServerRequestInterface - */ - public function withParsedBody($data): ServerRequestInterface - { - Context::setContext('with.parsed.body.callback', $data); - return $this; - } + /** + * @param array|object|null $data + * @return ServerRequestInterface + */ + public function withParsedBody($data): ServerRequestInterface + { + Context::setContext('with.parsed.body.callback', $data); + return $this; + } - /** - * @return array - */ - public function getAttributes(): array - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * @return array + */ + public function getAttributes(): array + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } - /** - * @param string $name - * @param null $default - * @return mixed - */ - public function getAttribute($name, $default = null): mixed - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * @param string $name + * @param null $default + * @return mixed + */ + public function getAttribute($name, $default = null): mixed + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } - /** - * @param string $name - * @param mixed $value - * @return ServerRequestInterface - */ - public function withAttribute($name, $value): ServerRequestInterface - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * @param string $name + * @param mixed $value + * @return ServerRequestInterface + */ + public function withAttribute($name, $value): ServerRequestInterface + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } - /** - * @param string $name - * @return ServerRequestInterface - */ - public function withoutAttribute($name): ServerRequestInterface - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * @param string $name + * @return ServerRequestInterface + */ + public function withoutAttribute($name): ServerRequestInterface + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } } diff --git a/http-message/Stream.php b/http-message/Stream.php index bdac939b..9b3bcbdf 100644 --- a/http-message/Stream.php +++ b/http-message/Stream.php @@ -10,228 +10,228 @@ class Stream implements StreamInterface { - /** - * @var string|resource - */ - private mixed $content = ''; + /** + * @var string|resource + */ + private mixed $content = ''; - /** - * @var int - */ - private int $size = 0; + /** + * @var int + */ + private int $size = 0; - /** - * @param mixed $stream - */ - public function __construct(mixed $stream = '') - { - $this->content = $stream; - if (!is_resource($stream)) { - $this->size = strlen($stream); - } else { - $state = fstat($this->content); - if ($state) { - $this->size = $state['size']; - } - } - } + /** + * @param mixed $stream + */ + public function __construct(mixed $stream = '') + { + $this->content = $stream; + if (!is_resource($stream)) { + $this->size = strlen($stream); + } else { + $state = fstat($this->content); + if ($state) { + $this->size = $state['size']; + } + } + } - /** - * @return string - */ - public function __toString() - { - return $this->content; - } + /** + * @return string + */ + public function __toString() + { + return $this->content; + } - /** - * - */ - public function close() - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * + */ + public function close() + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } - /** - * @return resource|null - */ - public function detach() - { - if (!is_resource($this->content)) { - throw new \BadMethodCallException('Not Accomplish Method.'); - } - $steam = stream_context_create(); - stream_copy_to_stream($this->content, $steam); - return $steam; - } + /** + * @return resource|null + */ + public function detach() + { + if (!is_resource($this->content)) { + throw new \BadMethodCallException('Not Accomplish Method.'); + } + $steam = stream_context_create(); + stream_copy_to_stream($this->content, $steam); + return $steam; + } - /** - * @return int - */ - public function getSize(): int - { - return $this->size; - } + /** + * @return int + */ + public function getSize(): int + { + return $this->size; + } - /** - * @return bool|int - */ - public function tell(): bool|int - { - if (!is_resource($this->content)) { - throw new \BadMethodCallException('Not Accomplish Method.'); - } - return ftell($this->content); - } + /** + * @return bool|int + */ + public function tell(): bool|int + { + if (!is_resource($this->content)) { + throw new \BadMethodCallException('Not Accomplish Method.'); + } + return ftell($this->content); + } - /** - * @return bool - */ - public function eof(): bool - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * @return bool + */ + public function eof(): bool + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } - /** - * @return bool - */ - public function isSeekable(): bool - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * @return bool + */ + public function isSeekable(): bool + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } - /** - * @param int $offset - * @param int $whence - */ - public function seek($offset, $whence = SEEK_SET) - { - if (!is_resource($this->content)) { - throw new \BadMethodCallException('Not Accomplish Method.'); - } - fseek($this->content, $offset, $whence); - } + /** + * @param int $offset + * @param int $whence + */ + public function seek($offset, $whence = SEEK_SET) + { + if (!is_resource($this->content)) { + throw new \BadMethodCallException('Not Accomplish Method.'); + } + fseek($this->content, $offset, $whence); + } - /** - * - */ - public function rewind() - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * + */ + public function rewind() + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } - /** - * @return bool - */ - public function isWritable(): bool - { - if (!is_resource($this->content)) { - return true; - } - if (is_writable($this->content)) { - return true; - } - return false; - } + /** + * @return bool + */ + public function isWritable(): bool + { + if (!is_resource($this->content)) { + return true; + } + if (is_writable($this->content)) { + return true; + } + return false; + } - /** - * @param string $string - * @return int - */ - public function write($string): int - { - if (is_resource($this->content)) { - fwrite($this->content, $string); - $state = fstat($this->content); - if ($state) { - $this->size = $state['size']; - } - } else { - $this->content = $string; - $this->size = strlen($string); - } - return $this->size; - } + /** + * @param string $string + * @return int + */ + public function write($string): int + { + if (is_resource($this->content)) { + fwrite($this->content, $string); + $state = fstat($this->content); + if ($state) { + $this->size = $state['size']; + } + } else { + $this->content = $string; + $this->size = strlen($string); + } + return $this->size; + } - /** - * @return bool - */ - public function isReadable(): bool - { - if (!is_resource($this->content)) { - return true; - } - if (is_readable($this->content)) { - return true; - } - return false; - } + /** + * @return bool + */ + public function isReadable(): bool + { + if (!is_resource($this->content)) { + return true; + } + if (is_readable($this->content)) { + return true; + } + return false; + } - /** - * @param int $length - * @return false|string - */ - public function read($length): bool|string - { - if (is_resource($this->content)) { - return fread($this->content, $length); - } else { - return $this->content; - } - } + /** + * @param int $length + * @return false|string + */ + public function read($length): bool|string + { + if (is_resource($this->content)) { + return fread($this->content, $length); + } else { + return $this->content; + } + } - /** - * @return string|bool - */ - public function getContents(): string|bool - { - if (is_resource($this->content)) { - return stream_get_contents($this->content); - } else { - return $this->content; - } - } + /** + * @return string|bool + */ + public function getContents(): string|bool + { + if (is_resource($this->content)) { + return stream_get_contents($this->content); + } else { + return $this->content; + } + } - /** - * @param null $key - * @return array - */ - #[ArrayShape([ - "timed_out" => "bool", - "blocked" => "bool", - "eof" => "bool", - "unread_bytes" => "int", - "stream_type" => "string", - "wrapper_type" => "string", - "wrapper_data" => "mixed", - "mode" => "string", - "seekable" => "bool", - "uri" => "string", - "crypto" => "array", - "mediatype" => "string", - ])] - public function getMetadata($key = null): array - { - if (is_resource($this->content)) { - return stream_get_meta_data($this->content); - } - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * @param null $key + * @return array + */ + #[ArrayShape([ + "timed_out" => "bool", + "blocked" => "bool", + "eof" => "bool", + "unread_bytes" => "int", + "stream_type" => "string", + "wrapper_type" => "string", + "wrapper_data" => "mixed", + "mode" => "string", + "seekable" => "bool", + "uri" => "string", + "crypto" => "array", + "mediatype" => "string", + ])] + public function getMetadata($key = null): array + { + if (is_resource($this->content)) { + return stream_get_meta_data($this->content); + } + throw new \BadMethodCallException('Not Accomplish Method.'); + } } diff --git a/http-message/Uploaded.php b/http-message/Uploaded.php index 010a4eb8..4b717251 100644 --- a/http-message/Uploaded.php +++ b/http-message/Uploaded.php @@ -9,94 +9,94 @@ use Psr\Http\Message\UploadedFileInterface; class Uploaded implements UploadedFileInterface { - const ERROR = [ - 0 => "There is no error, the file uploaded with success", - 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini", - 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", - 3 => "The uploaded file was only partially uploaded", - 4 => "No file was uploaded", - 6 => "Missing a temporary folder" - ]; + const ERROR = [ + 0 => "There is no error, the file uploaded with success", + 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini", + 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", + 3 => "The uploaded file was only partially uploaded", + 4 => "No file was uploaded", + 6 => "Missing a temporary folder" + ]; - /** - * @param string $tmp_name - * @param string $name - * @param string $type - * @param int $size - * @param int $error - */ - public function __construct( - public string $tmp_name, - public string $name, - public string $type, - public int $size, - public int $error - ) - { - } + /** + * @param string $tmp_name + * @param string $name + * @param string $type + * @param int $size + * @param int $error + */ + public function __construct( + public string $tmp_name, + public string $name, + public string $type, + public int $size, + public int $error + ) + { + } - /** - * @return StreamInterface - * @throws \Exception - */ - public function getStream(): StreamInterface - { - if (!fopen($this->tmp_name, 'r')) { - throw new \Exception('The file "' . $this->name . '" con\'t readable.'); - } - return new Stream(fopen($this->tmp_name, 'r')); - } + /** + * @return StreamInterface + * @throws \Exception + */ + public function getStream(): StreamInterface + { + if (!fopen($this->tmp_name, 'r')) { + throw new \Exception('The file "' . $this->name . '" con\'t readable.'); + } + return new Stream(fopen($this->tmp_name, 'r')); + } - /** - * @param string $targetPath - * @return \Psr\Http\Message\StreamInterface - * @throws \Exception - */ - public function moveTo($targetPath): StreamInterface - { - @move_uploaded_file($this->tmp_name, $targetPath); - if (!file_exists($targetPath)) { - throw new \Exception('File save fail.'); - } - $this->tmp_name = $targetPath; - return $this->getStream(); - } + /** + * @param string $targetPath + * @return \Psr\Http\Message\StreamInterface + * @throws \Exception + */ + public function moveTo($targetPath): StreamInterface + { + @move_uploaded_file($this->tmp_name, $targetPath); + if (!file_exists($targetPath)) { + throw new \Exception('File save fail.'); + } + $this->tmp_name = $targetPath; + return $this->getStream(); + } - /** - * @return int - */ - public function getSize(): int - { - return $this->size; - } + /** + * @return int + */ + public function getSize(): int + { + return $this->size; + } - /** - * @return int - */ - public function getError(): int - { - return $this->error; - } + /** + * @return int + */ + public function getError(): int + { + return $this->error; + } - /** - * @return string - */ - public function getClientFilename(): string - { - return $this->name; - } + /** + * @return string + */ + public function getClientFilename(): string + { + return $this->name; + } - /** - * @return string - */ - public function getClientMediaType(): string - { - return $this->type; - } + /** + * @return string + */ + public function getClientMediaType(): string + { + return $this->type; + } } diff --git a/http-message/Uri.php b/http-message/Uri.php index 7d53a7a5..ccead081 100644 --- a/http-message/Uri.php +++ b/http-message/Uri.php @@ -10,246 +10,246 @@ class Uri implements UriInterface { - protected string $scheme = ''; - protected string $host = ''; - protected string $path = ''; - protected string $fragment = ''; - protected int $port = 80; + protected string $scheme = ''; + protected string $host = ''; + protected string $path = ''; + protected string $fragment = ''; + protected int $port = 80; - protected string $queryString = ''; + protected string $queryString = ''; - /** - * @return string - */ - public function getScheme(): string - { - return $this->scheme; - } + /** + * @return string + */ + public function getScheme(): string + { + return $this->scheme; + } - /** - * @return string - */ - public function getAuthority(): string - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * @return string + */ + public function getAuthority(): string + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } - /** - * @return string - */ - public function getUserInfo(): string - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * @return string + */ + public function getUserInfo(): string + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } - /** - * @return string - */ - public function getHost(): string - { - return $this->host; - } + /** + * @return string + */ + public function getHost(): string + { + return $this->host; + } - /** - * @return int|null - */ - public function getPort(): ?int - { - return $this->port; - } + /** + * @return int|null + */ + public function getPort(): ?int + { + return $this->port; + } - /** - * @return string - */ - public function getPath(): string - { - return $this->path; - } + /** + * @return string + */ + public function getPath(): string + { + return $this->path; + } - /** - * @return string - */ - public function getQuery(): string - { - return $this->queryString; - } + /** + * @return string + */ + public function getQuery(): string + { + return $this->queryString; + } - /** - * @return string - */ - public function getFragment(): string - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * @return string + */ + public function getFragment(): string + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } - /** - * @param string $scheme - * @return $this|\Protocol\Message\Uri - */ - public function withScheme($scheme): UriInterface - { - $this->scheme = $scheme; - return $this; - } + /** + * @param string $scheme + * @return $this|Uri + */ + public function withScheme($scheme): UriInterface + { + $this->scheme = $scheme; + return $this; + } - /** - * @param string $user - * @param null $password - * @return \Protocol\Message\Uri - */ - public function withUserInfo($user, $password = null): UriInterface - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * @param string $user + * @param null $password + * @return Uri + */ + public function withUserInfo($user, $password = null): UriInterface + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } - /** - * @param string $host - * @return $this|\Protocol\Message\Uri - */ - public function withHost($host): UriInterface - { - $this->host = $host; - return $this; - } + /** + * @param string $host + * @return $this|Uri + */ + public function withHost($host): UriInterface + { + $this->host = $host; + return $this; + } - /** - * @param int|null $port - * @return $this|\Protocol\Message\Uri - */ - public function withPort($port): UriInterface - { - $this->port = $port; - return $this; - } + /** + * @param int|null $port + * @return $this|Uri + */ + public function withPort($port): UriInterface + { + $this->port = $port; + return $this; + } - /** - * @param string $path - * @return $this|\Protocol\Message\Uri - */ - public function withPath($path): UriInterface - { - $this->path = $path; - return $this; - } + /** + * @param string $path + * @return $this|Uri + */ + public function withPath($path): UriInterface + { + $this->path = $path; + return $this; + } - /** - * @param string $query - * @return $this|\Protocol\Message\Uri - */ - public function withQuery($query): UriInterface - { - $this->queryString = $query; - return $this; - } + /** + * @param string $query + * @return $this|Uri + */ + public function withQuery($query): UriInterface + { + $this->queryString = $query; + return $this; + } - /** - * @param string $fragment - * @return \Protocol\Message\Uri - */ - public function withFragment($fragment): UriInterface - { - throw new \BadMethodCallException('Not Accomplish Method.'); - } + /** + * @param string $fragment + * @return Uri + */ + public function withFragment($fragment): UriInterface + { + throw new \BadMethodCallException('Not Accomplish Method.'); + } - /** - * @return string - */ - public function __toString(): string - { - $domain = sprintf('%s://%s', $this->scheme, $this->host); - if (!in_array($this->port, [80, 443])) { - $domain .= ':' . $this->port; - } - if (empty($this->query) && empty($this->fragment)) { - return $domain . $this->path; - } - return sprintf('%s?%s#%s', $domain . $this->path, - $this->queryString, $this->fragment); - } + /** + * @return string + */ + public function __toString(): string + { + $domain = sprintf('%s://%s', $this->scheme, $this->host); + if (!in_array($this->port, [80, 443])) { + $domain .= ':' . $this->port; + } + if (empty($this->query) && empty($this->fragment)) { + return $domain . $this->path; + } + return sprintf('%s?%s#%s', $domain . $this->path, + $this->queryString, $this->fragment); + } - /** - * @return int - */ - public function getDefaultPort(): int - { - return $this->scheme == 'https' ? 443 : 80; - } + /** + * @return int + */ + public function getDefaultPort(): int + { + return $this->scheme == 'https' ? 443 : 80; + } - /** - * @param \Swoole\Http\Request $request - * @return UriInterface - */ - public static function parseUri(Request $request): UriInterface - { - $server = $request->server; - $header = $request->header; - $uri = new static(); - $uri = $uri->withScheme(!empty($server['https']) && $server['https'] !== 'off' ? 'https' : 'http'); - if (isset($request->header['x-forwarded-proto'])) { - $uri->withScheme($request->header['x-forwarded-proto'])->withPort(443); - } + /** + * @param Request $request + * @return UriInterface + */ + public static function parseUri(Request $request): UriInterface + { + $server = $request->server; + $header = $request->header; + $uri = new static(); + $uri = $uri->withScheme(!empty($server['https']) && $server['https'] !== 'off' ? 'https' : 'http'); + if (isset($request->header['x-forwarded-proto'])) { + $uri->withScheme($request->header['x-forwarded-proto'])->withPort(443); + } - $hasPort = false; - if (isset($server['http_host'])) { - $hostHeaderParts = explode(':', $server['http_host']); - $uri = $uri->withHost($hostHeaderParts[0]); - if (isset($hostHeaderParts[1])) { - $hasPort = true; - $uri = $uri->withPort($hostHeaderParts[1]); - } - } elseif (isset($server['server_name'])) { - $uri = $uri->withHost($server['server_name']); - } elseif (isset($server['server_addr'])) { - $uri = $uri->withHost($server['server_addr']); - } elseif (isset($header['host'])) { - $hasPort = true; - if (strpos($header['host'], ':')) { - [$host, $port] = explode(':', $header['host'], 2); - if ($port != $uri->getDefaultPort()) { - $uri = $uri->withPort($port); - } - } else { - $host = $header['host']; - } + $hasPort = false; + if (isset($server['http_host'])) { + $hostHeaderParts = explode(':', $server['http_host']); + $uri = $uri->withHost($hostHeaderParts[0]); + if (isset($hostHeaderParts[1])) { + $hasPort = true; + $uri = $uri->withPort($hostHeaderParts[1]); + } + } elseif (isset($server['server_name'])) { + $uri = $uri->withHost($server['server_name']); + } elseif (isset($server['server_addr'])) { + $uri = $uri->withHost($server['server_addr']); + } elseif (isset($header['host'])) { + $hasPort = true; + if (strpos($header['host'], ':')) { + [$host, $port] = explode(':', $header['host'], 2); + if ($port != $uri->getDefaultPort()) { + $uri = $uri->withPort($port); + } + } else { + $host = $header['host']; + } - $uri = $uri->withHost($host); - } + $uri = $uri->withHost($host); + } - if (!$hasPort && isset($server['server_port'])) { - $uri = $uri->withPort($server['server_port']); - } + if (!$hasPort && isset($server['server_port'])) { + $uri = $uri->withPort($server['server_port']); + } - $hasQuery = false; - if (isset($server['request_uri'])) { - $requestUriParts = explode('?', $server['request_uri']); - $uri = $uri->withPath($requestUriParts[0]); - if (isset($requestUriParts[1])) { - $hasQuery = true; - $uri = $uri->withQuery($requestUriParts[1]); - } - } + $hasQuery = false; + if (isset($server['request_uri'])) { + $requestUriParts = explode('?', $server['request_uri']); + $uri = $uri->withPath($requestUriParts[0]); + if (isset($requestUriParts[1])) { + $hasQuery = true; + $uri = $uri->withQuery($requestUriParts[1]); + } + } - if (!$hasQuery && isset($server['query_string'])) { - $uri = $uri->withQuery($server['query_string']); - } + if (!$hasQuery && isset($server['query_string'])) { + $uri = $uri->withQuery($server['query_string']); + } - return $uri; - } + return $uri; + } }