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