This commit is contained in:
2020-12-25 15:57:52 +08:00
parent f18031c2c0
commit 2013129406
7 changed files with 91 additions and 89 deletions
+18 -17
View File
@@ -24,7 +24,7 @@ class Context extends BaseContext
* @param null $key * @param null $key
* @return mixed * @return mixed
*/ */
public static function setContext($id, $context, $key = null) public static function setContext($id, $context, $key = null): mixed
{ {
if (static::inCoroutine()) { if (static::inCoroutine()) {
return self::setCoroutine($id, $context, $key); return self::setCoroutine($id, $context, $key);
@@ -39,7 +39,7 @@ class Context extends BaseContext
* @param null $key * @param null $key
* @return mixed * @return mixed
*/ */
private static function setStatic($id, $context, $key = null) private static function setStatic($id, $context, $key = null): mixed
{ {
if (!empty($key)) { if (!empty($key)) {
if (!is_array(static::$_requests[$id])) { if (!is_array(static::$_requests[$id])) {
@@ -57,30 +57,31 @@ class Context extends BaseContext
* @param $id * @param $id
* @param $context * @param $context
* @param null $key * @param null $key
* @return * @return mixed
*/ */
private static function setCoroutine($id, $context, $key = null) private static function setCoroutine($id, $context, $key = null): mixed
{ {
if (!static::hasContext($id)) { if (!static::hasContext($id)) {
Coroutine::getContext()[$id] = []; Coroutine::getContext()[$id] = [];
} }
if (!empty($key)) { if (!empty($key)) {
if (!is_array(Coroutine::getContext()[$id])) { if (!is_array(Coroutine::getContext()[$id])) {
return Coroutine::getContext()[$id] = [$key => $context]; Coroutine::getContext()[$id] = [$key => $context];
} else { } else {
return Coroutine::getContext()[$id][$key] = $context; Coroutine::getContext()[$id][$key] = $context;
} }
} else { } else {
return Coroutine::getContext()[$id] = $context; Coroutine::getContext()[$id] = $context;
} }
return $context;
} }
/** /**
* @param $id * @param $id
* @param null $key * @param null $key
* @return false|mixed * @return mixed
*/ */
public static function autoIncr($id, $key = null) public static function autoIncr($id, $key = null): mixed
{ {
if (!static::inCoroutine()) { if (!static::inCoroutine()) {
return false; return false;
@@ -94,9 +95,9 @@ class Context extends BaseContext
/** /**
* @param $id * @param $id
* @param null $key * @param null $key
* @return false|mixed * @return mixed
*/ */
public static function autoDecr($id, $key = null) public static function autoDecr($id, $key = null): mixed
{ {
if (!static::inCoroutine() || !static::hasContext($id)) { if (!static::inCoroutine() || !static::hasContext($id)) {
return false; return false;
@@ -112,7 +113,7 @@ class Context extends BaseContext
* @param null $key * @param null $key
* @return mixed * @return mixed
*/ */
public static function getContext($id, $key = null) public static function getContext($id, $key = null): mixed
{ {
if (!static::hasContext($id)) { if (!static::hasContext($id)) {
return null; return null;
@@ -135,7 +136,7 @@ class Context extends BaseContext
/** /**
* @return mixed * @return mixed
*/ */
public static function getAllContext() public static function getAllContext(): mixed
{ {
if (static::inCoroutine()) { if (static::inCoroutine()) {
return Coroutine::getContext() ?? []; return Coroutine::getContext() ?? [];
@@ -148,7 +149,7 @@ class Context extends BaseContext
* @param string $id * @param string $id
* @param string|null $key * @param string|null $key
*/ */
public static function deleteId(string $id,string $key = null) public static function deleteId(string $id, string $key = null)
{ {
if (!static::hasContext($id, $key)) { if (!static::hasContext($id, $key)) {
return; return;
@@ -167,9 +168,9 @@ class Context extends BaseContext
/** /**
* @param $id * @param $id
* @param null $key * @param null $key
* @return mixed * @return bool
*/ */
public static function hasContext($id, $key = null) public static function hasContext($id, $key = null): bool
{ {
if (!static::inCoroutine()) { if (!static::inCoroutine()) {
return false; return false;
@@ -190,7 +191,7 @@ class Context extends BaseContext
/** /**
* @return bool * @return bool
*/ */
public static function inCoroutine() public static function inCoroutine(): bool
{ {
return Coroutine::getCid() > 0; return Coroutine::getCid() > 0;
} }
+5 -5
View File
@@ -35,7 +35,7 @@ class File
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function saveTo(string $path) public function saveTo(string $path): bool
{ {
if ($this->hasError()) { if ($this->hasError()) {
throw new Exception($this->getErrorInfo()); throw new Exception($this->getErrorInfo());
@@ -51,7 +51,7 @@ class File
/** /**
* @return string * @return string
*/ */
public function rename() public function rename(): string
{ {
if (!empty($this->newName)) { if (!empty($this->newName)) {
return $this->newName; return $this->newName;
@@ -64,7 +64,7 @@ class File
/** /**
* @return string * @return string
*/ */
public function getTmpPath() public function getTmpPath(): string
{ {
return $this->tmp_name; return $this->tmp_name;
} }
@@ -74,7 +74,7 @@ class File
* *
* check file have error * check file have error
*/ */
public function hasError() public function hasError(): bool
{ {
return $this->error !== 0; return $this->error !== 0;
} }
@@ -84,7 +84,7 @@ class File
* *
* get upload error info * get upload error info
*/ */
public function getErrorInfo() public function getErrorInfo(): mixed
{ {
if (!isset($this->errorInfo[$this->error])) { if (!isset($this->errorInfo[$this->error])) {
return 'Unknown upload error.'; return 'Unknown upload error.';
+2 -1
View File
@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace HttpServer\Http\Formatter; namespace HttpServer\Http\Formatter;
use Exception;
use HttpServer\Application; use HttpServer\Application;
use SimpleXMLElement; use SimpleXMLElement;
use Swoole\Http\Response; use Swoole\Http\Response;
@@ -33,7 +34,7 @@ class XmlFormatter extends Application implements IFormatter
/** /**
* @param $context * @param $context
* @return $this * @return $this
* @throws \Exception * @throws Exception
*/ */
public function send($context): static public function send($context): static
{ {
+9 -8
View File
@@ -6,6 +6,7 @@
* Time: 14:54 * Time: 14:54
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace HttpServer\Http; namespace HttpServer\Http;
/** /**
@@ -75,7 +76,7 @@ class HttpHeaders
* @param array $headers * @param array $headers
* @return $this * @return $this
*/ */
public function addHeaders(array $headers) public function addHeaders(array $headers): static
{ {
if (empty($headers)) { if (empty($headers)) {
return $this; return $this;
@@ -90,7 +91,7 @@ class HttpHeaders
/** /**
* @return array * @return array
*/ */
public function getResponseHeaders() public function getResponseHeaders(): array
{ {
return $this->response; return $this->response;
} }
@@ -98,7 +99,7 @@ class HttpHeaders
/** /**
* @return array * @return array
*/ */
public function toArray() public function toArray(): array
{ {
return $this->headers; return $this->headers;
} }
@@ -107,7 +108,7 @@ class HttpHeaders
* @param $name * @param $name
* @return mixed|null * @return mixed|null
*/ */
public function getHeader($name) public function getHeader($name): ?string
{ {
if (!isset($this->headers[$name])) { if (!isset($this->headers[$name])) {
return null; return null;
@@ -119,9 +120,9 @@ class HttpHeaders
/** /**
* @param $name * @param $name
* @param null $default * @param null $default
* @return mixed|string|null * @return mixed
*/ */
public function get($name, $default = null) public function get($name, $default = null): mixed
{ {
if (($value = $this->getHeader($name)) === null) { if (($value = $this->getHeader($name)) === null) {
return $default; return $default;
@@ -134,7 +135,7 @@ class HttpHeaders
* @param $name * @param $name
* @return bool * @return bool
*/ */
public function exists($name) public function exists($name): bool
{ {
return isset($this->headers[$name]) && $this->headers[$name] != null; return isset($this->headers[$name]) && $this->headers[$name] != null;
} }
@@ -143,7 +144,7 @@ class HttpHeaders
/** /**
* @return array * @return array
*/ */
public function getHeaders() public function getHeaders(): array
{ {
return $this->headers; return $this->headers;
} }
+44 -45
View File
@@ -11,8 +11,10 @@ namespace HttpServer\Http;
use Exception; use Exception;
use HttpServer\Exception\RequestException; use HttpServer\Exception\RequestException;
use ReflectionException;
use Snowflake\Core\Help; use Snowflake\Core\Help;
use Snowflake\Core\Json; use Snowflake\Core\Json;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
@@ -50,7 +52,7 @@ class HttpParams
/** /**
* @return int * @return int
*/ */
public function offset() public function offset(): int
{ {
return ($this->page() - 1) * $this->size(); return ($this->page() - 1) * $this->size();
} }
@@ -81,7 +83,7 @@ class HttpParams
/** /**
* @return int * @return int
*/ */
private function page() private function page(): int
{ {
return (int)$this->get('page', 1); return (int)$this->get('page', 1);
} }
@@ -89,7 +91,7 @@ class HttpParams
/** /**
* @return int * @return int
*/ */
public function size() public function size(): int
{ {
return (int)$this->get('size', 20); return (int)$this->get('size', 20);
} }
@@ -97,11 +99,11 @@ class HttpParams
/** /**
* @param $name * @param $name
* @param $defaultValue * @param null $defaultValue
* @param $call * @param null $call
* @return mixed|null * @return mixed
*/ */
public function get($name, $defaultValue = null, $call = null) public function get($name, $defaultValue = null, $call = null): mixed
{ {
return $this->gets[$name] ?? $defaultValue; return $this->gets[$name] ?? $defaultValue;
} }
@@ -109,10 +111,10 @@ class HttpParams
/** /**
* @param $name * @param $name
* @param null $defaultValue * @param null $defaultValue
* @param $call * @param null $call
* @return mixed|null * @return mixed
*/ */
public function post($name, $defaultValue = null, $call = null) public function post($name, $defaultValue = null, $call = null): mixed
{ {
$data = $this->body[$name] ?? $defaultValue; $data = $this->body[$name] ?? $defaultValue;
if ($call !== null) { if ($call !== null) {
@@ -123,10 +125,10 @@ class HttpParams
/** /**
* @param $name * @param $name
* @return false|string * @return bool|string
* @throws Exception * @throws Exception
*/ */
public function json($name) public function json($name): bool|string
{ {
$data = $this->array($name); $data = $this->array($name);
if (empty($data)) { if (empty($data)) {
@@ -140,7 +142,7 @@ class HttpParams
/** /**
* @return array * @return array
*/ */
public function gets() public function gets(): array
{ {
return $this->gets; return $this->gets;
} }
@@ -148,7 +150,7 @@ class HttpParams
/** /**
* @return array * @return array
*/ */
public function params() public function params(): array
{ {
return array_merge($this->body ?? [], $this->files ?? []); return array_merge($this->body ?? [], $this->files ?? []);
} }
@@ -156,7 +158,7 @@ class HttpParams
/** /**
* @return array * @return array
*/ */
public function load() public function load(): array
{ {
return array_merge($this->files, $this->body, $this->gets); return array_merge($this->files, $this->body, $this->gets);
} }
@@ -164,19 +166,20 @@ class HttpParams
/** /**
* @param $name * @param $name
* @param array $defaultValue * @param array $defaultValue
* @return array|mixed * @return mixed
*/ */
public function array($name, $defaultValue = []) public function array($name, $defaultValue = []): mixed
{ {
return $this->body[$name] ?? $defaultValue; return $this->body[$name] ?? $defaultValue;
} }
/** /**
* @param $name * @param $name
* @return mixed|File|null * @return File|null
* @throws Exception * @throws ReflectionException
* @throws NotFindClassException
*/ */
public function file($name) public function file($name): File|null
{ {
if (!isset($this->files[$name])) { if (!isset($this->files[$name])) {
return null; return null;
@@ -189,10 +192,10 @@ class HttpParams
/** /**
* @param $name * @param $name
* @param bool $isNeed * @param bool $isNeed
* @return mixed|null * @return mixed
* @throws RequestException * @throws RequestException
*/ */
private function required($name, $isNeed = false) private function required($name, $isNeed = false): mixed
{ {
$int = $this->body[$name] ?? NULL; $int = $this->body[$name] ?? NULL;
if (is_null($int) && $isNeed === true) { if (is_null($int) && $isNeed === true) {
@@ -206,21 +209,17 @@ class HttpParams
* @param bool $isNeed * @param bool $isNeed
* @param null $min * @param null $min
* @param null $max * @param null $max
* @return int * @return int|null
* @throws Exception * @throws RequestException
*/ */
public function int($name, $isNeed = FALSE, $min = NULL, $max = NULL) public function int($name, $isNeed = FALSE, $min = NULL, $max = NULL): ?int
{ {
$int = $this->required($name, $isNeed); $int = $this->required($name, $isNeed);
if ($int === null) return null; if (is_null($int)) return null;
if (is_array($min)) { if (is_array($min)) {
list($min, $max) = $min; list($min, $max) = $min;
} }
if (is_null($int)) { $length = strlen((string)$int);
$length = 0;
} else {
$length = strlen((string)$int);
}
if (!is_numeric($int) || intval($int) != $int) { if (!is_numeric($int) || intval($int) != $int) {
throw new RequestException("The request parameter $name must integer."); throw new RequestException("The request parameter $name must integer.");
} }
@@ -235,7 +234,7 @@ class HttpParams
* @return float * @return float
* @throws Exception * @throws Exception
*/ */
public function float($name, $isNeed = FALSE, $round = 0) public function float($name, $isNeed = FALSE, $round = 0): ?float
{ {
$int = $this->required($name, $isNeed); $int = $this->required($name, $isNeed);
if ($int === null) { if ($int === null) {
@@ -256,7 +255,7 @@ class HttpParams
* @return string * @return string
* @throws * @throws
*/ */
public function string($name, $isNeed = FALSE, $length = NULL) public function string($name, $isNeed = FALSE, $length = NULL): string
{ {
$string = $this->required($name, $isNeed); $string = $this->required($name, $isNeed);
if ($string === null || $length === null) { if ($string === null || $length === null) {
@@ -297,10 +296,10 @@ class HttpParams
* @param $name * @param $name
* @param bool $isNeed * @param bool $isNeed
* *
* @return string * @return string|null
* @throws RequestException * @throws RequestException
*/ */
public function email($name, $isNeed = FALSE) public function email($name, $isNeed = FALSE): ?string
{ {
$email = $this->required($name, $isNeed); $email = $this->required($name, $isNeed);
if ($email === null) { if ($email === null) {
@@ -317,10 +316,10 @@ class HttpParams
* @param $name * @param $name
* @param bool $isNeed * @param bool $isNeed
* *
* @return string * @return bool|string
* @throws RequestException * @throws RequestException
*/ */
public function bool($name, $isNeed = FALSE) public function bool($name, $isNeed = FALSE): bool|string
{ {
$email = $this->required($name, $isNeed); $email = $this->required($name, $isNeed);
if ($email === null) { if ($email === null) {
@@ -333,10 +332,10 @@ class HttpParams
* @param $name * @param $name
* @param null $default * @param null $default
* *
* @return mixed|null * @return int|string|null
* @throws RequestException * @throws RequestException
*/ */
public function timestamp($name, $default = NULL) public function timestamp($name, $default = NULL): null|int|string
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value === null) { if ($value === null) {
@@ -358,10 +357,10 @@ class HttpParams
* @param $name * @param $name
* @param null $default * @param null $default
* *
* @return mixed|null * @return mixed
* @throws RequestException * @throws RequestException
*/ */
public function datetime($name, $default = NULL) public function datetime($name, $default = NULL): mixed
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value === null) { if ($value === null) {
@@ -379,10 +378,10 @@ class HttpParams
/** /**
* @param $name * @param $name
* @param null $default * @param null $default
* @return mixed|null * @return mixed
* @throws RequestException * @throws RequestException
*/ */
public function ip($name, $default = NULL) public function ip($name, $default = NULL): mixed
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value == NULL) { if ($value == NULL) {
@@ -398,9 +397,9 @@ class HttpParams
/** /**
* @param $name * @param $name
* @return mixed|null * @return mixed
*/ */
public function __get($name) public function __get($name): mixed
{ {
$load = $this->load(); $load = $this->load();
+1 -1
View File
@@ -360,7 +360,7 @@ class Request extends Application
/** /**
* @return mixed|null * @return mixed|null
*/ */
public function getIp() public function getIp(): string|null
{ {
$headers = $this->headers->getHeaders(); $headers = $this->headers->getHeaders();
if (!empty($headers['x-forwarded-for'])) return $headers['x-forwarded-for']; if (!empty($headers['x-forwarded-for'])) return $headers['x-forwarded-for'];
+12 -12
View File
@@ -54,7 +54,7 @@ class Response extends Application
* @param $format * @param $format
* @return $this * @return $this
*/ */
public function setFormat($format) public function setFormat($format): static
{ {
$this->format = $format; $this->format = $format;
return $this; return $this;
@@ -63,7 +63,7 @@ class Response extends Application
/** /**
* 清理无用数据 * 清理无用数据
*/ */
public function clear() public function clear(): void
{ {
$this->fd = 0; $this->fd = 0;
$this->isWebSocket = false; $this->isWebSocket = false;
@@ -73,7 +73,7 @@ class Response extends Application
/** /**
* @return string * @return string
*/ */
public function getContentType() public function getContentType(): string
{ {
if ($this->format == null || $this->format == static::JSON) { if ($this->format == null || $this->format == static::JSON) {
return 'application/json;charset=utf-8'; return 'application/json;charset=utf-8';
@@ -89,7 +89,7 @@ class Response extends Application
* @param $content * @param $content
* @return mixed * @return mixed
*/ */
public function toHtml($content) public function toHtml($content): mixed
{ {
$this->format = self::HTML; $this->format = self::HTML;
return $content; return $content;
@@ -100,7 +100,7 @@ class Response extends Application
* @param $content * @param $content
* @return mixed * @return mixed
*/ */
public function toJson($content) public function toJson($content): mixed
{ {
$this->format = self::JSON; $this->format = self::JSON;
return $content; return $content;
@@ -111,7 +111,7 @@ class Response extends Application
* @param $content * @param $content
* @return mixed * @return mixed
*/ */
public function toXml($content) public function toXml($content): mixed
{ {
$this->format = self::XML; $this->format = self::XML;
return $content; return $content;
@@ -122,7 +122,7 @@ class Response extends Application
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function sender() public function sender(): mixed
{ {
return $this->send(func_get_args()); return $this->send(func_get_args());
} }
@@ -132,7 +132,7 @@ class Response extends Application
* @param $value * @param $value
* @return Response * @return Response
*/ */
public function addHeader($key, $value) public function addHeader($key, $value): static
{ {
$this->headers[$key] = $value; $this->headers[$key] = $value;
return $this; return $this;
@@ -141,7 +141,7 @@ class Response extends Application
/** /**
* @return bool * @return bool
*/ */
private function isClient() private function isClient(): bool
{ {
return !($this->response instanceof SResponse) && !($this->response instanceof S2Response); return !($this->response instanceof SResponse) && !($this->response instanceof S2Response);
} }
@@ -153,7 +153,7 @@ class Response extends Application
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function send($context = '', $statusCode = 200, $response = null) public function send($context = '', $statusCode = 200, $response = null): mixed
{ {
$sendData = $this->parseData($context); $sendData = $this->parseData($context);
if ($response instanceof SResponse) { if ($response instanceof SResponse) {
@@ -171,7 +171,7 @@ class Response extends Application
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
private function parseData($context) private function parseData($context): mixed
{ {
if (isset($this->_format_maps[$this->format])) { if (isset($this->_format_maps[$this->format])) {
$config['class'] = $this->_format_maps[$this->format]; $config['class'] = $this->_format_maps[$this->format];
@@ -187,7 +187,7 @@ class Response extends Application
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function printResult($result) private function printResult($result): string
{ {
$result = Help::toString($result); $result = Help::toString($result);
$string = PHP_EOL . 'Command Result: ' . PHP_EOL . PHP_EOL; $string = PHP_EOL . 'Command Result: ' . PHP_EOL . PHP_EOL;