This commit is contained in:
2020-09-17 14:12:14 +08:00
parent 28669399b5
commit 57db5fe495
4 changed files with 370 additions and 375 deletions
+354 -349
View File
@@ -10,6 +10,7 @@ namespace HttpServer\Http;
use Exception; use Exception;
use HttpServer\Exception\RequestException; use HttpServer\Exception\RequestException;
use Snowflake\Core\Help;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
@@ -19,386 +20,390 @@ use Snowflake\Snowflake;
class HttpParams class HttpParams
{ {
/** @var array */ /** @var array */
private $body = []; private $body = [];
/** @var array */ /** @var array */
private $gets = []; private $gets = [];
/** @var array */ /** @var array */
private $files = []; private $files = [];
/** /**
* HttpParams constructor. * HttpParams constructor.
* @param $body * @param $body
* @param $get * @param $get
* @param $files * @param $files
*/ */
public function __construct($body, $get, $files) public function __construct($body, $get, $files)
{ {
$this->body = $body; $this->body = $body;
$this->gets = $get ?? []; $this->gets = $get ?? [];
$this->files = $files ?? []; $this->files = $files ?? [];
} if (!is_array($this->body)) {
$this->body = Help::toArray($this->body);
}
/** }
* @return int
*/
public function offset()
{
return ($this->page() - 1) * $this->size();
}
/** /**
* @param array $data * @return int
* 批量添加数据 */
*/ public function offset()
public function setPosts($data) {
{ return ($this->page() - 1) * $this->size();
if (!is_array($data)) { }
return;
}
foreach ($data as $key => $vla) {
$this->body[$key] = $vla;
}
}
/** /**
* @param string $key * @param array $data
* @param string $value * 批量添加数据
*/ */
public function addGetParam(string $key, string $value) public function setPosts(array $data)
{ {
$this->gets[$key] = $value; if (!is_array($data)) {
} return;
}
foreach ($data as $key => $vla) {
$this->body[$key] = $vla;
}
}
/** /**
* @return int * @param string $key
*/ * @param string $value
private function page() */
{ public function addGetParam(string $key, string $value)
return (int)$this->get('page', 1); {
} $this->gets[$key] = $value;
}
/** /**
* @return int * @return int
*/ */
public function size() private function page()
{ {
return (int)$this->get('size', 20); return (int)$this->get('page', 1);
} }
/**
* @return int
*/
public function size()
{
return (int)$this->get('size', 20);
}
/** /**
* @param $name * @param $name
* @param $defaultValue * @param $defaultValue
* @param $call * @param $call
* @return mixed|null * @return mixed|null
*/ */
public function get($name, $defaultValue = null, $call = null) public function get($name, $defaultValue = null, $call = null)
{ {
return $this->gets[$name] ?? $defaultValue; return $this->gets[$name] ?? $defaultValue;
} }
/** /**
* @param $name * @param $name
* @param null $defaultValue * @param null $defaultValue
* @param $call * @param $call
* @return mixed|null * @return mixed|null
*/ */
public function post($name, $defaultValue = null, $call = null) public function post($name, $defaultValue = null, $call = null)
{ {
$data = $this->body[$name] ?? $defaultValue; $data = $this->body[$name] ?? $defaultValue;
if ($call !== null) { if ($call !== null) {
$data = call_user_func($call, $data); $data = call_user_func($call, $data);
} }
return $data; return $data;
} }
/** /**
* @param $name * @param $name
* @return false|string * @return false|string
* @throws Exception * @throws Exception
*/ */
public function json($name) public function json($name)
{ {
$data = $this->array($name); $data = $this->array($name);
if (empty($data)) { if (empty($data)) {
return JSON::encode([]); return JSON::encode([]);
} else if (!is_array($data)) { } else if (!is_array($data)) {
return JSON::encode([]); return JSON::encode([]);
} }
return JSON::encode($data); return JSON::encode($data);
} }
/** /**
* @return array * @return array
*/ */
public function gets() public function gets()
{ {
return $this->gets; return $this->gets;
} }
/** /**
* @return array * @return array
*/ */
public function params() public function params()
{ {
return array_merge($this->body ?? [], $this->files ?? []); return array_merge($this->body ?? [], $this->files ?? []);
} }
/** /**
* @return array * @return array
*/ */
public function load() public function load()
{ {
return array_merge($this->files, $this->body, $this->gets); return array_merge($this->files, $this->body, $this->gets);
} }
/** /**
* @param $name * @param $name
* @param array $defaultValue * @param array $defaultValue
* @return array|mixed * @return array|mixed
*/ */
public function array($name, $defaultValue = []) public function array($name, $defaultValue = [])
{ {
return $this->body[$name] ?? $defaultValue; return $this->body[$name] ?? $defaultValue;
} }
/** /**
* @param $name * @param $name
* @return mixed|File|null * @return mixed|File|null
* @throws Exception * @throws Exception
*/ */
public function file($name) public function file($name)
{ {
if (!isset($this->files[$name])) { if (!isset($this->files[$name])) {
return null; return null;
} }
$param = $this->files[$name]; $param = $this->files[$name];
$param['class'] = File::class; $param['class'] = File::class;
return Snowflake::createObject($param); return Snowflake::createObject($param);
} }
/** /**
* @param $name * @param $name
* @param bool $isNeed * @param bool $isNeed
* @return mixed|null * @return mixed|null
* @throws RequestException * @throws RequestException
*/ */
private function required($name, $isNeed = false) private function required($name, $isNeed = false)
{ {
$int = $this->body[$name] ?? NULL; $int = $this->body[$name] ?? NULL;
if (is_null($int) && $isNeed === true) { if (is_null($int) && $isNeed === true) {
throw new RequestException("You need to add request parameter $name"); throw new RequestException("You need to add request parameter $name");
} }
return $int; return $int;
} }
/** /**
* @param $name * @param $name
* @param bool $isNeed * @param bool $isNeed
* @param null $min * @param null $min
* @param null $max * @param null $max
* @return int * @return int
* @throws Exception * @throws Exception
*/ */
public function int($name, $isNeed = FALSE, $min = NULL, $max = NULL) public function int($name, $isNeed = FALSE, $min = NULL, $max = NULL)
{ {
$int = $this->required($name, $isNeed); $int = $this->required($name, $isNeed);
if ($int === null) return null; if ($int === null) return null;
if (is_array($min)) { if (is_array($min)) {
list($min, $max) = $min; list($min, $max) = $min;
} }
if (is_null($int)) { if (is_null($int)) {
$length = 0; $length = 0;
} else { } else {
$length = strlen(floatval($int)); $length = strlen(floatval($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.");
} }
$this->between($length, $min, $max); $this->between($length, $min, $max);
return (int)$int; return (int)$int;
} }
/** /**
* @param $name * @param $name
* @param bool $isNeed * @param bool $isNeed
* @param int $round * @param int $round
* @return float * @return float
* @throws Exception * @throws Exception
*/ */
public function float($name, $isNeed = FALSE, $round = 0) public function float($name, $isNeed = FALSE, $round = 0)
{ {
$int = $this->required($name, $isNeed); $int = $this->required($name, $isNeed);
if ($int === null) { if ($int === null) {
return null; return null;
} }
if ($round > 0) { if ($round > 0) {
return round(floatval($int), $round); return round(floatval($int), $round);
} else { } else {
return floatval($int); return floatval($int);
} }
} }
/** /**
* @param $name * @param $name
* @param bool $isNeed * @param bool $isNeed
* @param null $length * @param null $length
* *
* @return string * @return string
* @throws * @throws
*/ */
public function string($name, $isNeed = FALSE, $length = NULL) public function string($name, $isNeed = FALSE, $length = NULL)
{ {
$string = $this->required($name, $isNeed); $string = $this->required($name, $isNeed);
if ($string === null || $length === null) { if ($string === null || $length === null) {
return $string; return $string;
} }
if (!is_string($string)) { if (!is_string($string)) {
$string = json_encode($string, JSON_UNESCAPED_UNICODE); $string = json_encode($string, JSON_UNESCAPED_UNICODE);
} }
$_length = strlen($string); $_length = strlen($string);
if (is_array($length)) { if (is_array($length)) {
if (count($length) < 2) { if (count($length) < 2) {
array_unshift($length, 0); array_unshift($length, 0);
} }
$this->between($_length, ...$length); $this->between($_length, ...$length);
} else if (is_numeric($length) && $_length != $length) { } else if (is_numeric($length) && $_length != $length) {
throw new RequestException("The length of the string must be $length characters"); throw new RequestException("The length of the string must be $length characters");
} }
return $string; return $string;
} }
/** /**
* @param $_length * @param $_length
* @param $min * @param $min
* @param $max * @param $max
* @throws RequestException * @throws RequestException
*/ */
private function between($_length, $min, $max) private function between($_length, $min, $max)
{ {
if ($min !== NULL && $_length < $min) { if ($min !== NULL && $_length < $min) {
throw new RequestException("The minimum value cannot be lower than $min"); throw new RequestException("The minimum value cannot be lower than $min");
} }
if ($max !== NULL && $_length > $max) { if ($max !== NULL && $_length > $max) {
throw new RequestException("Maximum cannot exceed $max, has length " . $_length); throw new RequestException("Maximum cannot exceed $max, has length " . $_length);
} }
} }
/** /**
* @param $name * @param $name
* @param bool $isNeed * @param bool $isNeed
* *
* @return string * @return string
* @throws RequestException * @throws RequestException
*/ */
public function email($name, $isNeed = FALSE) public function email($name, $isNeed = FALSE)
{ {
$email = $this->required($name, $isNeed); $email = $this->required($name, $isNeed);
if ($email === null) { if ($email === null) {
return null; return null;
} }
if (!preg_match('/^\w+([.-_]\w+)+@\w+(\.\w+)+$/', $email)) { if (!preg_match('/^\w+([.-_]\w+)+@\w+(\.\w+)+$/', $email)) {
throw new RequestException("Request parameter $name is in the wrong format", 4001); throw new RequestException("Request parameter $name is in the wrong format", 4001);
} }
return $email; return $email;
} }
/** /**
* @param $name * @param $name
* @param bool $isNeed * @param bool $isNeed
* *
* @return string * @return string
* @throws RequestException * @throws RequestException
*/ */
public function bool($name, $isNeed = FALSE) public function bool($name, $isNeed = FALSE)
{ {
$email = $this->required($name, $isNeed); $email = $this->required($name, $isNeed);
if ($email === null) { if ($email === null) {
return false; return false;
} }
return (bool)$email; return (bool)$email;
} }
/** /**
* @param $name * @param $name
* @param null $default * @param null $default
* *
* @return mixed|null * @return mixed|null
* @throws RequestException * @throws RequestException
*/ */
public function timestamp($name, $default = NULL) public function timestamp($name, $default = NULL)
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value === null) { if ($value === null) {
return $default; return $default;
} }
if (!is_numeric($value)) { if (!is_numeric($value)) {
throw new RequestException('The request param :attribute not is a timestamp value'); throw new RequestException('The request param :attribute not is a timestamp value');
} }
if (strlen((string)$value) != 10) { if (strlen((string)$value) != 10) {
throw new RequestException('The request param :attribute not is a timestamp value'); throw new RequestException('The request param :attribute not is a timestamp value');
} }
if (!date('YmdHis', $value)) { if (!date('YmdHis', $value)) {
throw new RequestException('The request param :attribute format error', 4001); throw new RequestException('The request param :attribute format error', 4001);
} }
return $value; return $value;
} }
/** /**
* @param $name * @param $name
* @param null $default * @param null $default
* *
* @return mixed|null * @return mixed|null
* @throws RequestException * @throws RequestException
*/ */
public function datetime($name, $default = NULL) public function datetime($name, $default = NULL)
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value === null) { if ($value === null) {
return $default; return $default;
} }
$match = '/^\d{4}.*?([1-12]).*([1-31]).*?[0-23].*?[0-59].*?[0-59].*?$/'; $match = '/^\d{4}.*?([1-12]).*([1-31]).*?[0-23].*?[0-59].*?[0-59].*?$/';
$match = preg_match($match, $value, $result); $match = preg_match($match, $value, $result);
if (!$match || $result[0] != $value) { if (!$match || $result[0] != $value) {
throw new RequestException('The request param :attribute format error', 4001); throw new RequestException('The request param :attribute format error', 4001);
} }
return $value; return $value;
} }
/** /**
* @param $name * @param $name
* @param null $default * @param null $default
* @return mixed|null * @return mixed|null
* @throws RequestException * @throws RequestException
*/ */
public function ip($name, $default = NULL) public function ip($name, $default = NULL)
{ {
$value = $this->required($name, false); $value = $this->required($name, false);
if ($value == NULL) { if ($value == NULL) {
return $default; return $default;
} }
$match = preg_match('/^\d{1,3}(\.\d{1,3}){3}$/', $value, $result); $match = preg_match('/^\d{1,3}(\.\d{1,3}){3}$/', $value, $result);
if (!$match || $result[0] != $value) { if (!$match || $result[0] != $value) {
throw new RequestException('The request param :attribute format error', 4001); throw new RequestException('The request param :attribute format error', 4001);
} }
return $value; return $value;
} }
/** /**
* @param $name * @param $name
* @return mixed|null * @return mixed|null
*/ */
public function __get($name) public function __get($name)
{ {
$load = $this->load(); $load = $this->load();
return $load[$name] ?? null; return $load[$name] ?? null;
} }
} }
+1 -1
View File
@@ -424,7 +424,7 @@ class Request extends Application
$sRequest->startTime = microtime(true); $sRequest->startTime = microtime(true);
$sRequest->uri = $request->server['request_uri'] ?? $request->header['request_uri']; $sRequest->uri = $request->server['request_uri'] ?? $request->header['request_uri'];
$sRequest->params = Snowflake::createObject(HttpParams::class,[Help::toArray($request->rawContent()), $request->get, $request->files]); $sRequest->params = new HttpParams($request->rawContent(), $request->get, $request->files);
if (!empty($request->post)) { if (!empty($request->post)) {
$sRequest->params->setPosts($request->post ?? []); $sRequest->params->setPosts($request->post ?? []);
} }
+9 -9
View File
@@ -105,7 +105,7 @@ class Connection extends Pool
*/ */
public function inTransaction($cds) public function inTransaction($cds)
{ {
[$coroutineId, $coroutineName] = $this->getIndex($cds, true); $coroutineName = $this->name($cds, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (!Context::hasContext('begin_' . $coroutineName)) {
return false; return false;
} }
@@ -117,7 +117,7 @@ class Connection extends Pool
*/ */
public function beginTransaction($coroutineName) public function beginTransaction($coroutineName)
{ {
[$coroutineId, $coroutineName] = $this->getIndex($coroutineName, true); $coroutineName = $this->name($coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (!Context::hasContext('begin_' . $coroutineName)) {
Context::setContext('begin_' . $coroutineName, 0); Context::setContext('begin_' . $coroutineName, 0);
} }
@@ -136,7 +136,7 @@ class Connection extends Pool
*/ */
public function commit($coroutineName) public function commit($coroutineName)
{ {
[$coroutineId, $coroutineName] = $this->getIndex($coroutineName, true); $coroutineName = $this->name($coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (!Context::hasContext('begin_' . $coroutineName)) {
return; return;
} }
@@ -170,7 +170,7 @@ class Connection extends Pool
*/ */
public function rollback($coroutineName) public function rollback($coroutineName)
{ {
[$coroutineId, $coroutineName] = $this->getIndex($coroutineName, true); $coroutineName = $this->name($coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) { if (!Context::hasContext('begin_' . $coroutineName)) {
return; return;
} }
@@ -200,7 +200,7 @@ class Connection extends Pool
if ($this->creates === 0) { if ($this->creates === 0) {
$this->creates = Timer::tick(10000, [$this, 'Heartbeat_detection']); $this->creates = Timer::tick(10000, [$this, 'Heartbeat_detection']);
} }
[$coroutineId, $coroutineName] = $this->getIndex($config['cds'], $isMaster); $coroutineName = $this->name($config['cds'], $isMaster);
if (!isset($this->hasCreate[$coroutineName])) { if (!isset($this->hasCreate[$coroutineName])) {
$this->hasCreate[$coroutineName] = 0; $this->hasCreate[$coroutineName] = 0;
} }
@@ -210,9 +210,9 @@ class Connection extends Pool
if ($this->size($coroutineName) < 1 && $this->hasCreate[$coroutineName] < $this->max) { if ($this->size($coroutineName) < 1 && $this->hasCreate[$coroutineName] < $this->max) {
return $this->saveClient($coroutineName, $this->nowClient($coroutineName, $config)); return $this->saveClient($coroutineName, $this->nowClient($coroutineName, $config));
} }
[$timeout, $connection] = $client = $this->get($coroutineName); $connections = $client = $this->get($coroutineName);
if ($connection instanceof PDO) { if ($connections[1] instanceof PDO) {
return $this->saveClient($coroutineName, $connection); return $this->saveClient($coroutineName, $connections[1]);
} }
return $this->saveClient($coroutineName, $this->nowClient($coroutineName, $config)); return $this->saveClient($coroutineName, $this->nowClient($coroutineName, $config));
} }
@@ -252,7 +252,7 @@ class Connection extends Pool
*/ */
public function release($coroutineName, $isMaster) public function release($coroutineName, $isMaster)
{ {
[$coroutineId, $coroutineName] = $this->getIndex($coroutineName, $isMaster); $coroutineName = $this->name($coroutineName, $isMaster);
if (!$this->hasClient($coroutineName)) { if (!$this->hasClient($coroutineName)) {
return; return;
} }
+6 -16
View File
@@ -36,7 +36,7 @@ class Redis extends Pool
public function getConnection(array $config, $isMaster = false) public function getConnection(array $config, $isMaster = false)
{ {
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
[$coroutineId, $coroutineName] = $this->getIndex('redis:' . $name, $isMaster); $coroutineName = $this->name('redis:' . $name, $isMaster);
if (Context::hasContext($coroutineName)) { if (Context::hasContext($coroutineName)) {
return Context::getContext($coroutineName); return Context::getContext($coroutineName);
} else if (!$this->hasItem($coroutineName)) { } else if (!$this->hasItem($coroutineName)) {
@@ -59,11 +59,11 @@ class Redis extends Pool
$this->success('create redis client -> ' . $config['host'] . ':' . $this->size($coroutineName)); $this->success('create redis client -> ' . $config['host'] . ':' . $this->size($coroutineName));
return $this->saveClient($coroutineName, $this->createConnect($config)); return $this->saveClient($coroutineName, $this->createConnect($config));
} }
[$time, $client] = $this->get($coroutineName); $clients = $this->get($coroutineName);
if ($client === null) { if ($clients[1] === null) {
return $this->getByChannel($coroutineName, $config); return $this->getByChannel($coroutineName, $config);
} }
return $this->saveClient($coroutineName, $client); return $this->saveClient($coroutineName, $clients[1]);
} }
@@ -109,7 +109,7 @@ class Redis extends Pool
public function release(array $config, $isMaster = false) public function release(array $config, $isMaster = false)
{ {
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
[$coroutineId, $coroutineName] = $this->getIndex('redis:' . $name, $isMaster); $coroutineName = $this->name('redis:' . $name, $isMaster);
if (!Context::hasContext($coroutineName)) { if (!Context::hasContext($coroutineName)) {
return; return;
} }
@@ -127,7 +127,7 @@ class Redis extends Pool
public function destroy(array $config, $isMaster = false) public function destroy(array $config, $isMaster = false)
{ {
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
[$coroutineId, $coroutineName] = $this->getIndex('redis:' . $name, $isMaster); $coroutineName = $this->name('redis:' . $name, $isMaster);
if (!Context::hasContext($coroutineName)) { if (!Context::hasContext($coroutineName)) {
return; return;
} }
@@ -177,15 +177,5 @@ class Redis extends Pool
// TODO: Implement desc() method. // TODO: Implement desc() method.
} }
/**
* @param $name
* @param false $isMaster
* @return array
*/
private function getIndex($name, $isMaster = false)
{
return [Coroutine::getCid(), $this->name($name, $isMaster)];
}
} }